java - Get letters to servlet the right way -
i have html jsp form users can copy text press submit , pdf created using itext . on other side servlet gets data . happens when use non standard letters gives me #^& these letters . did use in servler , both jsp charset=utf-8 , using post method send data . using netbeans , tomcat . not itext since when system.out.println(variable) not print correctly .
package servlets; import com.lowagie.text.chunk; import com.lowagie.text.document; import com.lowagie.text.documentexception; import com.lowagie.text.pagesize; import com.lowagie.text.paragraph; import com.lowagie.text.rectangle; import com.lowagie.text.pdf.basefont; import com.lowagie.text.pdf.pdfannotation; import com.lowagie.text.pdf.pdfappearance; import com.lowagie.text.pdf.pdfformfield; import com.lowagie.text.pdf.pdfwriter; import com.lowagie.text.pdf.draw.lineseparator; import java.io.ioexception; import java.sql.sqlexception; import java.util.logging.level; import java.util.logging.logger; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; /** * * @author jovo */ @webservlet(name = "generatebrokerspdf", urlpatterns = {"/generatebrokerspdf"}) public class generatebrokerspdf extends httpservlet { /* public static final string font = "c:/windows/fonts/segoeui.ttf"; bf = basefont.createfont(font, basefont.identity_h, basefont.embedded); document.add(new paragraph(title,new font(bf, 12))); */ protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception, sqlexception, documentexception { response.setcontenttype("application/pdf;charset=utf-8"); request.setcharacterencoding("utf-8"); response.setcharacterencoding("uft-8"); basefont bf; try { httpsession session = request.getsession(true); if (session.getattribute("taxnumber") == null || session.getattribute("email") == null || session.getattribute("password") == null) { request.setattribute("message", "the user not exist in our database"); request.getrequestdispatcher("/login.jsp").forward(request, response); } string title = request.getparameter("doctitle"); string date = request.getparameter("docdate"); string text = request.getparameter("brokerstext"); //string text = urldecoder.decode(new string(request.getparameter("brokerstext").getbytes("iso-8859-1")), "utf-8"); // system.out.println(text); string[] newdate = date.split("/"); document document = new document(pagesize.a4); pdfwriter writer = pdfwriter.getinstance(document, response.getoutputstream()); document.open(); document.add(new paragraph(newdate[1] + "/" + newdate[0] + "/" + newdate[2])); document.add(new paragraph(title)); document.add(new paragraph(text)); document.add(chunk.newline); document.add(chunk.newline); document.add(chunk.newline); document.add(new lineseparator()); document.add(chunk.newline); document.add(new lineseparator()); document.add(chunk.newline); document.add(chunk.newline); document.add(new paragraph(" brokers's digital signature clients's digital signature")); //potpis 1 pdfformfield field1 = pdfformfield.createsignature(writer); field1.setwidget(new rectangle(72, 100, 172, 200), pdfannotation.highlight_invert); field1.setfieldname("mysig1"); field1.settitle("klijent1"); field1.setflags(pdfannotation.flags_print); field1.setpage(); pdfappearance tp1 = pdfappearance.createappearance(writer, 72, 48); tp1.rectangle(0.5f, 0.5f, 71.5f, 47.5f); tp1.stroke(); field1.setappearance( pdfannotation.appearance_normal, tp1); writer.addannotation(field1); //potpis //potpis 2 pdfformfield field2 = pdfformfield.createsignature(writer); field2.setwidget(new rectangle(372, 100, 472, 200), pdfannotation.highlight_invert); field2.setfieldname("mysig2"); field2.settitle("klijent2"); field2.setflags(pdfannotation.flags_print); field2.setpage(); pdfappearance tp2 = pdfappearance.createappearance(writer, 72, 48); tp2.rectangle(0.5f, 0.5f, 71.5f, 47.5f); tp2.stroke(); field2.setappearance( pdfannotation.appearance_normal, tp2); writer.addannotation(field2); //potpis document.close(); /* todo output page here. may use following sample code. */ } { } } // <editor-fold defaultstate="collapsed" desc="httpservlet methods. click on + sign on left edit code."> /** * handles http * <code>get</code> method. * * @param request servlet request * @param response servlet response * @throws servletexception if servlet-specific error occurs * @throws ioexception if i/o error occurs */ @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { try { try { processrequest(request, response); } catch (documentexception ex) { logger.getlogger(generatebrokerspdf.class.getname()).log(level.severe, null, ex); } } catch (sqlexception ex) { logger.getlogger(generatebrokerspdf.class.getname()).log(level.severe, null, ex); } } /** * handles http * <code>post</code> method. * * @param request servlet request * @param response servlet response * @throws servletexception if servlet-specific error occurs * @throws ioexception if i/o error occurs */ @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { try { try { processrequest(request, response); } catch (documentexception ex) { logger.getlogger(generatebrokerspdf.class.getname()).log(level.severe, null, ex); } } catch (sqlexception ex) { logger.getlogger(generatebrokerspdf.class.getname()).log(level.severe, null, ex); } } /** * returns short description of servlet. * * @return string containing servlet description */ @override public string getservletinfo() { return "short description"; }// </editor-fold> }
this jsp start code
<%@page language="java" contenttype="text/html;charset=utf-8" pageencoding="utf-8" %> <% request.setcharacterencoding("utf-8");%> <% if(session.getattribute("taxnumber")==null || session.getattribute("email")==null || session.getattribute("password")==null ) { request.setattribute("message", "the user not exist in our database"); request.getrequestdispatcher("/login.jsp").forward(request, response); } %> <!doctype html> <html> <head> <meta charset="utf-8"> <meta content="ie=edge,chrome=1" http-equiv="x-ua-compatible"> <meta content="width=device-width" name="viewport"> ...
this html form(created jsp) source code
<!doctype html> <html> <head> <meta charset="utf-8"> <meta content="ie=edge,chrome=1" http-equiv="x-ua-compatible"> <meta content="width=device-width" name="viewport"> <link type="text/css" href="nodegraph/css/base.css" rel="stylesheet" /> <link type="text/css" href="nodegraph/css/spacetree.css" rel="stylesheet" /> <script language="javascript" type="text/javascript" src="jit.js"></script> <link type="text/css" rel="stylesheet" href="http://fonts.googleapis.com/css?family=open+sans:300,400,600,700"> <link href="content/css/bootstrap.css" type="text/css" rel="stylesheet"> <link href="content/css/bootstrap-responsive.css" type="text/css" rel="stylesheet"> <link href="content/css/bootmetro.css" type="text/css" rel="stylesheet"> <link href="content/css/bootmetro-tiles.css" type="text/css" rel="stylesheet"> <link href="content/css/bootmetro-charms.css" type="text/css" rel="stylesheet"> <link href="content/css/metro-ui-light.css" type="text/css" rel="stylesheet"> <link href="content/css/icomoon.css" type="text/css" rel="stylesheet"> <link href="content/css/datepicker.css" type="text/css" rel="stylesheet"> <link href="content/css/daterangepicker.css" type="text/css" rel="stylesheet"> <link href="content/css/demo.css" type="text/css" rel="stylesheet"> <link href="scripts/google-code-prettify/prettify.css" type="text/css" rel="stylesheet"> <link href="content/ico/favicon.ico" rel="shortcut icon"> <link href="content/ico/apple-touch-icon-144-precomposed.png" sizes="144x144" rel="apple-touch-icon-precomposed"> <link href="content/ico/apple-touch-icon-114-precomposed.png" sizes="114x114" rel="apple-touch-icon-precomposed"> <link href="content/ico/apple-touch-icon-72-precomposed.png" sizes="72x72" rel="apple-touch-icon-precomposed"> <link href="content/ico/apple-touch-icon-57-precomposed.png" rel="apple-touch-icon-precomposed"> <script> function showchat() { if (document.getelementbyid("charms").style.display=="block") { document.getelementbyid("charms").style.display="none"; } else { document.getelementbyid("charms").style.display="block"; } } </script> </head> <body > <div class="container-fluid"> <div class="row-fluid"> <div class="span2" > <a id="backbutton" class="win-backbutton" href="#" onclick="history.go(-1)"></a> <a style="margin-top: 45px;position: absolute;width: 170px;" class="thumbnail" href="debtsolutions.jsp"> <img alt="" src="deptsolutionic.jpg"> <label>debt solutions</label> </a> <a style="margin-top: 215px;position: absolute;width: 170px;" class="thumbnail" href="synergy.jsp"> <img alt="" src="synergyic.jpg"> <label>company synergy</label> </a> <a style="margin-top: 385px;position: absolute;width: 170px;" class="thumbnail" href="publicbid.jsp"> <img alt="" src="publicinc.jpg"> <label>public procurements</label> </a> <a style="margin-top: 555px;position: absolute;width: 170px;" class="thumbnail" href="companysearch.jsp"> <img alt="" src="searchic.jpg"> <label>company search</label> </a> <a style="margin-top: 725px;position: absolute; width: 170px;"class="thumbnail" href="companynetworking.jsp"> <img alt="" src="networkingic.jpg"> <label>company networking</label> </a> <a style="margin-top: 895px;position: absolute; width: 170px;padding-bottom: 30px;"class="thumbnail" href="brokers.jsp"> <img alt="" src="brokers.jpg"> <label>brokers accounts</label> </a> </div> <div class="span10" style="text-align:justify;margin-right:200px;margin-left:200px;"> <hr class="win-command"> <button class="win-command" onclick="parent.location='start.jsp'"> <span class="win-commandimage win-commandring"></span> <span class="win-label">start</span> </button> <hr class="win-command"> <button class="win-command" onclick="parent.location='statistics.jsp'"> <span class="win-commandimage win-commandring"></span> <span class="win-label">statistics</span> </button> <hr class="win-command"> <button class="win-command" onclick="showchat();"> <span class="win-commandimage win-commandring">]</span> <span class="win-label">chat</span> </button> <hr class="win-command"> <button class="win-command" onclick="parent.location='setup.jsp'"> <span class="win-commandimage win-commandring"></span> <span class="win-label">setup</span> </button> <hr class="win-command"> <button class="win-command" onclick="parent.location='home.jsp'"> <span class="win-commandimage win-commandring"></span> <span class="win-label">logout</span> </button> <hr class="win-command"> <div id="top-info" class="pull-right"> <a class="pull-left" href="#"> <div class="top-info-block"> <h3>coca cola hellenic company serbia doo </h3> </div> <div class="top-info-block"> <b class="icon-user"></b> </div> </a> </div> <span class="label label-success" style="display: block;background-color: #aa40ff;"> <h1>brokers accounts</h1></span> <p> <h2>create purchase documents clients </h2></p> <p><h4>your clients need not come office sign document purchase of stocks made. can generate these documents online , send them clients signing via smart card . generate pdf document , send clients email signing . </h4></p> <form onsubmit="return sendformdata();" style="margin: 30px;" target="_blank" action="generatebrokerspdf" method="post"> <label>document title</label> <input type="text" name="doctitle" id="doctitles" class="input-xxlarge" placeholder="enter document title"/> <label>document creation date</label> <input type="text" id="datepicker" name="docdate" class="input-xxlarge" placeholder="click enter document creation date"/> <label>set document text</label> <textarea name="brokerstext" id="brokerstext" maxlength="999999" placeholder="the document text content" rows="20" cols="50" class="input-xxlarge" ></textarea> <p><button type="submit" class="btn" style="margin-left: 25px;" >generate pdf document </button></p> </form> <script type="text/javascript"> function sendformdata() { if (document.getelementbyid("doctitles").value=="") { document.getelementbyid("mymessagesbody").innerhtml="<p><h2>you must input document title</h2><p>" ; $('#mymodal2').modal("toggle"); return false; } if (document.getelementbyid("datepicker").value=="") { document.getelementbyid("mymessagesbody").innerhtml="<p><h2>you must input document date</h2><p>" ; $('#mymodal2').modal("toggle"); return false; } if (document.getelementbyid("brokerstext").value=="") { document.getelementbyid("mymessagesbody").innerhtml="<p><h2>you must input document content text</h2><p>" ; $('#mymodal2').modal("toggle"); return false; } } </script> </div> </div> </div> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <script> window.jquery || document.write("<script src='scripts/jquery-1.8.2.min.js'>\x3c/script>") </script> <script src="scripts/google-code-prettify/prettify.js" type="text/javascript"></script> <script src="scripts/jquery.mousewheel.js" type="text/javascript"></script> <script src="scripts/jquery.scrollto.js" type="text/javascript"></script> <script src="scripts/jquery.blockui.js" type="text/javascript"></script> <script src="scripts/jquery.form.js" type="text/javascript"></script> <script src="scripts/jquery.validate.js" type="text/javascript"></script> <script src="scripts/jquery.validate.unobtrusive.js" type="text/javascript"></script> <script src="scripts/modernizr-2.6.1.min.js" type="text/javascript"></script> <script src="scripts/mustache.js" type="text/javascript"></script> <script src="scripts/bootstrap.min.js" type="text/javascript"></script> <script src="scripts/bootstrap.js" type="text/javascript"></script> <script src="scripts/bootmetro.js" type="text/javascript"></script> <script src="scripts/bootmetro-charms.js" type="text/javascript"></script> <script src="scripts/demo.js" type="text/javascript"></script> <script src="scripts/holder.js" type="text/javascript"></script> <script src="scripts/bootstrap-datepicker.js" type="text/javascript"></script> <script src="scripts/daterangepicker.js" type="text/javascript"></script> <script src="scripts/holder.js" type="text/javascript"></script> <script type="text/javascript"> $(".metro").metro(); </script> <div id="charms" class="win-ui-dark in" style="display: none;"> <div id="theme-charms-section" class="charms-section"> <div class="charms-header"> <a class="close-charms win-command" href="#" onclick="showchat();"> <span class="win-commandimage win-commandring"></span> </a> <h2>chat</h2> </div> <div class="row-fluid"> <div class="span12"> </div> </div> </div> </div> <script type="text/javascript"> $(function() { $( "#datepicker" ).datepicker(); }); </script> <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" /> <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script> <div id="mymodal2" class="modal message hide fade in" aria-hidden="false" aria-labelledby="mymodallabel" role="dialog" tabindex="-1" style="display: none;"> <div class="modal-header"> <button class="close" aria-hidden="true" data-dismiss="modal" type="button"></button> <h3>message</h3> </div> <div class="modal-body" id="mymessagesbody"> <p></p> </div> <div class="modal-footer"> <button class="btn" data-dismiss="modal">close</button> </div> </div> <footer ><p style="margin-left:25px;"> © 2013 compensatemeonline.com </p></footer> </body> </html>
1) need set data content type utf-8
before submitting data. because receiver(in case, servlet) needs know encoding set(in jsp) data before servlet decodes bytes.
ie: request.setcharacterencoding("utf-8");
2) <%@page pageencoding="utf-8"%>
not used java data. line used tell language jsp file written in. ie: english, chinese. think totally misunderstood line.
answer updated:
3) remove line >> string(request.getparameter("brokerstext").getbytes("iso-8859-1")), "utf-8");
don't need convert received utf-8 bytes iso.
note: please post jsp code
Comments
Post a Comment