1: The form submits data in the foreground in post mode:
When the browser sends data (assumed to be "China") to the server, it must look up the code table when turning the data into binary data of 0101 (assumed to be 98 99). Which code table the browser uses to open the web page, the browser will Which code table to submit data with. After the data reaches the server, the data (98 99) must be encapsulated into the request. Calling the getParameter method of the Request in the servlet returns a string ("China"). After getting the number inside the method, it must be converted into characters. Be sure to check the code. Table, since the designer of the request is a foreigner, the default query is ISO8859-1, which is commonly used by them. This is the source of garbled request data.
Copy the code code as follows:
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 in post mode
public class RequestDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//Chinese garbled problem of request data
request.setCharacterEncoding("UTF-8");//We control the client web page to be UTF-8
String username = request.getParameter("username");
//Getting data is normal, you can check different code tables when outputting data
response.setCharacterEncoding("gb2312");//Notify the server of the code table to consult when sending data
response.setContentType("text/html;charset=gb2312");//Notify the browser with which 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: The form submits data in the foreground in get mode:
The data submitted in the get method is still sent using the code table used by the browser to open it. The difference is that when submitting data in get mode, the request setting encoding is invalid. Even if UTF-8 is set, ISO8859-1 will still be checked. Get (??). To solve this problem, you need to use (??) to reversely check ISO8859-1. After getting (98 99), check the correct code table.
Copy the code code as follows:
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 via get method
public class RequestDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//Chinese garbled problem of request data
request.setCharacterEncoding("UTF-8");//When submitting data in get mode, the request setting encoding is invalid. Even if UTF-8 is set, ISO8859-1 will still be checked.
String username = request.getParameter("username");
System.out.println(username);
byte source [] = username.getBytes("iso8859-1");
username = new String (source,"UTF-8");
System.out.println(username);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
Three: Summary of the problem of Chinese garbled characters in submitted data:
1. If the submission method is post, you only need to set the encoding of the request object if you want to avoid garbled characters.
Note: Which way the client data is submitted depends on what encoding the request should be set to.
2. If the submission method is get, setting the encoding of the request object is invalid. If you want to avoid garbled characters, you can only convert it manually.
String data = "???????";//garbled string
byte source [] = data.getBytes("iso8859-1");//Get the original data submitted by the client
data = new String (data.getBytes("iso8859-1"),"UTF-8");//solve garbled characters
//Equal to
data = new String (source,"UTF-8");
3. The garbled code in the get method can also be achieved by changing the server configuration. Change the server.xml file in Tomact's conf directory.
3.1
This approach is not recommended as it changes the server and is not flexible.
3.2
After this setting, whatever encoding is set by request's setCharacterEncoding will be used by the connector. Although it is more flexible than the previous change, it will still cause our application to be firmly dependent on the server, and is not recommended.
Four: Finally, let me mention a small detail: if the URL address is followed by Chinese data, it must be URL encoded. The parameters submitted in the form include Chinese data, and the browser will automatically help us encode it. However, if the Chinese parameters are directly brought through the link, the browser will not help us encode it. In this case, we want to solve the problem of Chinese garbled characters through the second method above. It sometimes doesn't work. You should encode it first through URLEncoding.encode(,"UTF-8").