1 garbled handling
A GET request uses a click directly through the address bar for a return or hyperlink and a request submitted by the get data of method in form form, which is the way that the request and the user parameters are passed http://www.jnb.com?name=jack&age=36 Therefore, there is a limited request parameter for the Get mode (less than 1K). And the data passed is visible directly in the address bar. Such as: Baidu's search keywords.
1.1 Write a registration form that submits get data
<form action="/day07/regist"Method="Get"> <table align="Center"Border="1"> <tr> <td> users:</td> <td><input type="text"Name="uname"/></td> </tr> <tr> <td> address:</td> <td><input t Ype="text"Name="Address"/></td> </tr> <tr align="Center"> <TD colspan="2"> <input type="Submit"Value="Register"/> <input type="Reset"Value=" Reset"/> </td> </tr> </table> </form>
2 Write a registservlet processing the user's GET request data
Public voiddoget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {//processing the format and encoding of response dataResponse.setcharacterencoding ("Utf-8"); Response.setcontenttype ("Text/html;charset=utf-8"); //get character output stream objectPrintWriter out=Response.getwriter (); //GET Request ParametersString name = Request.getparameter ("uname"); String Address= Request.getparameter ("Address"); //Output Data out. println ("name="+name); out. println ("<br/>"); out. println ("address="+address); }
Run results found in Chinese submitted after the display result is garbled:
3 reasons for parsing garbled characters
4 using code to solve garbled problems
Public voiddoget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {//processing the format and encoding of response dataResponse.setcharacterencoding ("Utf-8"); Response.setcontenttype ("Text/html;charset=utf-8"); //get character output stream objectPrintWriter out=Response.getwriter (); //GET Request ParametersString name = Request.getparameter ("uname"); String Address= Request.getparameter ("Address"); //get raw data for iso8859-1 byte[] bs = Name.getbytes ("iso8859-1"); Name=NewString (BS,"UTF-8"); BS= Address.getbytes ("iso8859-1"); Address=NewString (BS,"UTF-8"); //Output Data out. println ("name="+name); out. println ("<br/>"); out. println ("address="+address); }
If you use the above code to troubleshoot get garbled each request parameter needs to be re-decoded too cumbersome, so you can directly use the server to inform the browser to encode character data in a specified manner.
5 solving get garbled problems with urienoding method
<connector port="8080" protocol="http/1.1" ConnectionTimeout="20000" redirectport="8443 " uriencoding="utf-8"/>
Uriencoding mainly specifies the decoding method of the%E5%8D%A1%E5%8D%A1 data, if not specified then the default is Iso8859-1, if specified then use the specified code table for decoding. Therefore, there is no need for the server side to use two encoding method for transcoding.
Note: In the actual production environment our code garbled problem is not possible since the server to help us solve, so we recommend the use of the first transcoding method. Late can be directly implemented a garbled filter to implement the problem of processing garbled.
- POST request
The POST request is primarily a request that is sent as method= "post" in form form. The requested data is in the request body of the HTTP protocol. You can pass data of any size. Therefore, it is more suitable to upload files.
Modify the above registration page submission method for post, then the Dopost () directly get the data and output the same garbled.
Using code to troubleshoot garbled characters
Public voidDoPost (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {//processing the format and encoding of response dataResponse.setcharacterencoding ("Utf-8"); Response.setcontenttype ("Text/html;charset=utf-8"); //processing POST request parameter garbledRequest.setcharacterencoding ("Utf-8"); //get character output stream objectPrintWriter out=Response.getwriter (); //GET Request ParametersString name = Request.getparameter ("uname"); String Address= Request.getparameter ("Address"); //Output Data out. println ("name="+name); out. println ("<br/>"); out. println ("address="+address);}
Summary: In the future in the actual development to avoid the page output Chinese data and Chinese parameters. Then we should write the following three sentences directly in the Doget and Dopost methods for handling user requests:
Response.setcharacterencoding ("Utf-8"); Response.setcontenttype (" Text/html;charset=utf-8 " ); Request.setcharacterencoding ("utf-8");
Java Learning Note-httpservletresponse (22)