This article mainly introduces the Ajax submitted to the Servelt get parameters have garbled solution, the need for friends can refer to the following
Here, suppose the reader understands basic ajax technology or more. I only explain one phenomenon, one solution; reason, principle, same unclear ...... We start with a simple, servlet to JSP page JS Script, use the script to display related values on the page. Solve this direction (servlet--javascript) of Chinese garbled problem is very simple. Only in servlet, add a encoding setting: code as follows: Response.setcontenttype ("text/html;charset= UTF-8 "); and JSP page encoding can be set at will, GBK, GB2312, UTF-8, of course iso-8859-1 is not good, at least you want to display Chinese on the page. from the JS script to the servlet, the problem due to the XMLHTTP Object Open () method, the value of the way (get, POST) different, and divided into two situations: before introducing this issue, First, the two ways to show the difference between the value of the: hypothesis transfer value "software Engineering", variable name "Selcol". 1, get method: code as follows: Function Selectcol () { createxmlhttprequest (); var Selcol = " Software engineering code as follows:"; var url = "/emp/findspecial?selcol=" + selcol; Xmlhttp.onreadystatechange = handlestatechange; Xmlhttp.open ("get", url,true); XmlHttp.send (null); } 2, POSThe T method: code is as follows: function Selectcol () { createxmlhttprequest (); var selcol = "selcol= Software engineering"; nbsp var url = "/emp/findspecial"; Xmlhttp.onreadystatechange = handlestatechange; Xmlhttp.open ("POST", Url,true); Xmlhttpxmlhttp.setrequestheader ("Content-type", "application/ X-www-form-urlencoded "); Xmlhttp.send (selcol); } If you use a Get method to pass, So when we get a value in the servlet, we encode the: code as follows: String selcol = new String (Request.getparameter ("Selcol"). GetBytes (" Iso-8859-1 ")," GBK "); turn GBK, GB2312 are all right, turn UTF-8 No! This time you may be surprised to find that both statements appear at the same time: code as follows: Response.setcontenttype ("Text/html;charset=utf-8"); String selcol = new String (Request.getparameter ("Selcol"). GetBytes ("Iso-8859-1"), "GBK"); This is understood by the individual: the first sentence guarantees that the data encoded by the servlet is utf-8; and the latter is converting the data passed in to GBK encoded data. Convenience and identification and processing. If you use post to pass, then when the servlet takes the value, ditto, just convert the code to utf-8, and thisA time, GBK and GB2312 not! Code is as follows: string selcol = new String (Request.getparameter ("Selcol"). GetBytes ("Iso-8859-1"), "UTF-8"); & nbsp