Recently learning jquery UI, when doing a small function need to get the value of the foreground, through Ajax to the servlet, and then return the data results, but when the servlet accepts parameters, through the background printing, found to accept garbled, code examples are as follows:
Public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, IOException { response.setcontenttype ("Text/html;charset=utf-8"); Request.setcharacterencoding ("Utf-8"); = Response.getwriter (); String Testword=request.getparameter ("Criticalword") System.out.println (Testword); Out.println (Testword); Out.flush (); Out.close (); }
I only use the Ajax get way to pass, so the above also only intercepts the code of the Doget () method, then for garbled, what kind of situation, I talk about my opinion, the specific method of each phase I will be in the code and the way to give:
1. Ensure JSP Web page is not garbled
First of all make sure that your JSP page is not garbled, the specific code is as follows:
<% @page pageencoding= "Utf-8" contenttype= "Text/html;charset=utf-8"%>
This sentence is added at the top of the page to be able to add the page instruction of the specific meaning of the two parameters, many people may use, but I think it is necessary to understand the following:
A.pageencoding parameters
The Pageencoding property is used to specify the character encoding of the JSP page, which defaults to iso-8859-1, because the method does not support Chinese, so if you want to specify how the JSP page's character encoding supports Chinese encoding, you need to set the Pageencoding property of the page directive to " GB2312 "," GBk "or" UTF-8 ".
B.contenttype parameters
The ContentType property is used to specify the type and character encoding of the content of the JSP page output. The content type part of the property value can be text/html (plain text HTML page), Text/plain (plain text file), and so on.
2. Ensure that jquery Ajax is not garbled before delivery
Below the AJAX code, I want to pass to the background parameter is a get type, the parameter is called Criticalword, the code is as follows:
$ (' #search '). AutoComplete ({ Source:function(request,response) {
alert (' See if there is no garbled ' +request.term before delivery '); $.ajax ({ type:' get ', URL:'/sgame/servlet/indexsearchitems ', Data:{criticalword: encodeURI (request.term)}, success:function(response,status,xhr { alert (response); }} ); }, delay:
As my Code Red mark, you before the transmission of the alert () Pop-up window, measured to see if your JS file is garbled, if so, the solution:
1. Check back the previous step is not resolved
2.js file encoding problem: In the resource manager with Notepad open JS, and then save As, select Utf-8 in the code
Such as:
When your first pop-up window is not garbled, it means that there is no problem before passing it to the servlet, which is to do a preparatory work before going to the next step, as shown in the red code above, to transcode the parameters first:
Criticalword:encodeuri (Request.term)
This transcoding is to prevent the occurrence of a garbled function in the servlet in the form of: encodeURI (param)
3. Ensure that the servlet is not garbled when accepting Ajax parameters (request)
This step means that you are not far from success, just need to first request and Resopnse set the encoding, and then decode it, the Doget code is as follows:
void doget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException { Response.setcontenttype ( "Text/html;charset=utf-8"); Request.setcharacterencoding ("Utf-8"); = Response.getwriter (); String Testword=Urldecoder.decode (request.getparameter ("Criticalword"), "Utf-8") ; System.out.println (Testword); Out.println (Testword); Out.flush (); Out.close (); }
There are three main points:
A.response.setcontenttype (): Used to set the character encoding of the response back to the JSP or Ajax.
B.request.setcharacterencoding (): Used to set the response encoding for receiving requests.
C.urldecoder.decode (): This function needs to import the java.net package to decode the encoding of Ajax.
After completing the three steps above, you can try to add a System.out.println (Yourparam) before returning, as I do, and if it is Chinese then it will be fine.
4. Ensure that the servlet is not garbled in response (response) to JSP
actually in the third part of the point B is set, actually return to the HTML or JSP should be the Chinese.
Bloggers before the online to see a lot about the Ajax code garbled problem, but can solve really not much, so I hope to write an introduction to a comprehensive blog, hoping to help you solve the problem at the same time, the way to understand the whole coding process, in addition, about this also have any questions, welcome to send me an e-mail: [ Email protected].
JQuery Ajax value to the servlet, in the servlet to accept the argument garbled solution