Recently I encountered a problem, that is, the normal Java site under Windows deployment to Linux run after the Chinese request parameter parsing garbled. The method is that I do a double encodeuricomponent () encoding of the Chinese parameters to be requested on the client, and the Request.getparameter () method is invoked directly in the background to get the parameters, which are handled in the same way for getting and post. Today, I accidentally tried the following method to find that this problem will not occur. The method is as follows:
First, to understand that the mechanism of a GET request and a POST request is different, the coding mechanism is not the same. The parameters submitted by get method must be encoded by the code function explicitly in the client, while the whole coding mode can be set when the parameters are submitted by post, and the parameters need not be coded separately. The code for the client JSP page is as follows:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > Second, the server cannot generalize to get requests and post requests. When the client is set to UTF-8, the service-side post method does not need to be decoded. The code for the service-side servlet is as follows:
Package com.smapp.dataexchange.service;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.annotation.WebServlet;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse; /** * Servlet Implementation class Test */@WebServlet ("/test") public class Test extends HttpServlet {private static
Final long serialversionuid = 1L;
/** * @see httpservlet#httpservlet () * */Public Test () {super (); TODO auto-generated Constructor stub}/** * @see httpservlet#doget (httpservletrequest request, HTTPSERVLETRESP Onse response) */protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexceptio
N, ioexception {String strcontent = request.getparameter ("content"); Strcontent = new String (strcontent.getbytes ("iso-8859-1"), "UTF-8"); <span style= "White-space:pre" > </span >//can see that only get requests are requiredCarry out transcoding response.setcontenttype ("Text/text");
Response.setcharacterencoding ("UTF-8");
Response.getwriter (). write (strcontent); /** * @see Httpservlet#dopost (httpservletrequest request, httpservletresponse response) * * protected void DoPost ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {String strcontent = req
Uest.getparameter ("content");
Response.setcontenttype ("Text/text");
Response.setcharacterencoding ("UTF-8");
Response.getwriter (). write (strcontent);
}
}
OK, when you run the JSP page, the test submits the Chinese parameter, whether through get or Post method can parse correctly.