Summary of Chinese garbled data submitted by form, and garbled data submitted by form

Source: Internet
Author: User

Summary of Chinese garbled data submitted by form, and garbled data submitted by form

1. form submits data in post mode on the foreground:

When the browser sends data (assuming "China") to the server, it must check the code table when it turns the data into 0101 binary data (assuming 98 99, if the browser opens the webpage with a code table, the browser submits data with the code table. After the data arrives at the server, the data (98 99) will be encapsulated in the request. The getParameter method called in the servlet will return a string ("China "), the internal method to get the number to be converted into characters, be sure to check the code table, because the request designer is a foreigner, so the default query is their commonly used ISO8859-1, this is the source of request data generated garbled.

Package com. yyz. request; import java. io. IOException; import java. io. printWriter; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; // submit the form public class RequestDemo extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// Chinese garbled request for request data. setCharacterEncoding ("UTF-8"); // client web page we control as UTF-8 String username = request. getParameter ("username"); // The retrieved data is normal. You can view the response of different code tables when outputting data. setCharacterEncoding ("gb2312"); // The response code table that notifies the server to view when sending data. setContentType ("text/html; charset = gb2312"); // notifies the browser of the code table to open PrintWriter out = response. getWriter (); out. write (username);} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet (request, response );}}

2. form submits data in the get mode at the front end:

The data submitted in get mode is still sent by the browser when any code table is opened. The difference is that when data is submitted in get mode, the request encoding is invalid. Even if the UTF-8 is set up, the ISO8859-1 will still be checked. Get (? ?), To solve this problem, you need to take (??) Reverse query ISO8859-1, get (98 99), and then check the correct code table.

1 package com. yyz. request; 2 3 import java. io. IOException; 4 import java. io. printWriter; 5 6 import javax. servlet. servletException; 7 import javax. servlet. http. httpServlet; 8 import javax. servlet. http. httpServletRequest; 9 import javax. servlet. http. httpServletResponse; 10 // submit Form 11 public class RequestDemo extends HttpServlet {12 13 public void doGet (HttpServletRequest request, HttpServletRespo Neuron response) 14 throws ServletException, IOException {15 // Chinese garbled error in request data 16 request. setCharacterEncoding ("UTF-8"); // when you submit data in get mode, the request setting encoding is invalid. Even if the UTF-8 is set, the ISO8859-117 String username = request is still queried. getParameter ("username"); 18 System. out. println (username); 19 byte source [] = username. getBytes ("iso8859-1"); 20 username = new String (source, "UTF-8"); 21 System. out. println (username); 22 23} 24 25 public void doPost (HttpServletRequest request, HttpServletResponse response) 26 throws ServletException, IOException {27 doGet (request, response); 28} 29 30}

Iii. Summary of Chinese garbled data submission:

1. If the submission method is post, you only need to set the encoding of the request object to avoid garbled characters.

Note: The request must be encoded based on the method in which the client data is submitted.

2. If the submission method is get, the encoding of the request object is invalid and can only be converted manually without garbled characters.

String data = "??????? "; // Garbled string
Byte source [] = data. getBytes ("iso8859-1"); // get the raw data submitted by the client
Data = new String (data. getBytes ("iso8859-1"), "UTF-8"); // fix garbled characters

// Equivalent

Data = new String (source, "UTF-8 ");

3. garbled get mode. You can also change the server configuration. Change the server. xml file in the conf directory of Tomact.

3.1 this method is not recommended because it changes the server and is not flexible.

3.2 after this setting, the request setCharacterEncoding sets the encoding that the connector uses. Although it is more flexible than the previous change, it will still cause our applications to be firmly dependent on the server, and is not recommended.

4. At the end of the article, let us provide a small detail: If the URL address is followed by Chinese data, it must be URL encoded. If the parameters submitted in the form contain Chinese data, the browser will automatically help us encode them. However, if the parameters are directly encoded through links, the browser will not help us encode them, in this case, if you want to solve the Chinese Garbled text problem through the second method above, it will not work. You should use URLEncoding. encode (, "UTF-8") is encoded first.

 

 

 


Chinese garbled characters when retrieving data submitted by forms in JSP

Written in jsp file <% @ page contentType = "text/html; charset = GBK" %>
Only valid for post. request. setCharacterEncoding ("gbk"); this code is generally placed at the beginning. The above line of code can only be set before getParameter (), otherwise this code is invalid.


How to avoid the garbled characters of Chinese data submitted by forms

Generally, garbled characters are solved by writing a Filter. The Filter content is as follows:
---------------------------------------------------------------
Package com. filter;

Import java. io. IOException;

Import javax. servlet. Filter;
Import javax. servlet. FilterChain;
Import javax. servlet. FilterConfig;
Import javax. servlet. ServletException;
Import javax. servlet. ServletRequest;
Import javax. servlet. ServletResponse;
Import javax. servlet. http. HttpServlet;

Public class PageEncodingFilter extends HttpServlet implements Filter {

Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
Request. setCharacterEncoding ("UTF-8 ");
Chain. doFilter (request, response );
}

Public void init (FilterConfig arg0) throws ServletException {

}

Public PageEncodingFilter (){
Super ();
}

Public void destroy (){
Super. destroy ();
}

Public void init () throws ServletException {
}
}
--------------------------------------------------------------------
Add the following configuration in web. xml:
<Filter>
<Filter-name> PageEncodingFilter </filter-name>
<Filter-class> com. filter. PageEncodingFilter </filter-class>
</Filter>

<Filter-mapping>
<Filter-name> PageEncodingFilter </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>... the remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.