Java Study Notes-HttpServletResponse (22), java Study Notes
1. garbled Processing
A GET request is a request that uses the get data submitted directly through a carriage return or hyperlink click in the address bar and the method in the form. Is the request and the way to pass USER Parameters http://www.jnb.com? Name = jack & age = 36. Therefore, the request parameters of the get method are limited (less than 1 K ). The transmitted data is directly visible in the address bar. For example, Baidu's search keyword.
1.1 compile a registry ticket to submit get data.
<Form action = "/day07/regist" method = "get"> <table align = "center" border = "1"> <tr> <td> User: </td> <input type = "text" name = "uname"/> </td> </tr> <td> address: </td> <input type = "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 to process the user's Get request data.
Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// response data processing format and encoding response. setCharacterEncoding ("UTF-8"); response. setContentType ("text/html; charset = UTF-8"); // gets the output character stream object PrintWriter out = response. getWriter (); // obtain the request parameter String name = request. getParameter ("uname"); String address = request. getParameter ("address"); // output data out. println ("name =" + name); out. println ("<br/>"); out. println ("address =" + address );}
The running result is garbled after the input is submitted in Chinese:
3. analyze the cause of garbled data
Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// response data processing format and encoding response. setCharacterEncoding ("UTF-8"); response. setContentType ("text/html; charset = UTF-8"); // gets the output character stream object PrintWriter out = response. getWriter (); // obtain the request parameter String name = request. getParameter ("uname"); String address = request. getParameter ("address"); // get raw data for the ISO8859-1 byte [] bs = name. getBytes ("ISO8859-1"); name = new String (bs, "UTF-8"); bs = address. getBytes ("ISO8859-1"); address = new String (bs, "UTF-8"); // output data out. println ("name =" + name); out. println ("<br/>"); out. println ("address =" + address );}
If you use the above Code to solve the GET Garbled text, it is too cumbersome to re-Decode each request parameter. Therefore, you can directly use the server to notify the browser to encode the character data in a specified way.
5. Use URIEnoding to solve the GET garbled Problem
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="utf-8"/>
URIEncoding mainly specifies the data decoding method of % E5 % 8D % A1 % E5 % 8D % A1, if not specified, the default is ISO8859-1, if this parameter is specified, the specified code table is used for decoding. Therefore, there is no need for the server to use two encoding methods for transcoding.
Note: In the actual production environment, the garbled code problem cannot be solved by the server. Therefore, the first transcoding method is recommended. Later, we can directly implement a garbled filter to handle garbled data.
POST requests are mainly sent using form method = "post. The requested data is in the HTTP Request body. Data of any size can be transferred. Therefore, it is suitable for uploading files.
Modify the submission method of the above registration page to post, then get the data directly in doPost () and output the same garbled code.
Public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// response data processing format and encoding response. setCharacterEncoding ("UTF-8"); response. setContentType ("text/html; charset = UTF-8"); // handle garbled requests with post request parameters. setCharacterEncoding ("UTF-8"); // gets the output object PrintWriter out = response. getWriter (); // obtain the request parameter String name = request. getParameter ("uname"); String address = request. getParameter ("address"); // output data out. println ("name =" + name); out. println ("<br/>"); out. println ("address =" + address );}
Conclusion: In the future development, Chinese data and Chinese parameters cannot be output to the page. In the subsequent doGet and doPost methods for processing user requests, we should write the following three sentences:
response.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=utf-8");request.setCharacterEncoding("utf-8");