JQuery Ajax passed values to the servlet the solution of garbled problem _jquery

Source: Internet
Author: User
Tags flush

Recently learning the jquery UI, when doing a small function, you need to get the value of the foreground, through Ajax to the servlet, and then return the data results, but when the servlet accept the parameters, print through the background, the 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");
PrintWriter out = 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 Doget () method code, then for garbled, what kind of situation, I talk about my views, the specific stages of the method I will be in code and screenshot of the way to give:

1. Ensure that the JSP Web page is not garbled

First have to 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 added to the top of the page can be added to the page instruction two parameters of the specific meaning, 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 the character encoding of the JSP page to support Chinese encoding, you need to set the page directive's Pageencoding property to " GB2312 "," GBk "or" UTF-8 ".

B.contenttype parameters

The ContentType property is used to specify the type and character encoding of the JSP page output. The content type portion of the property value can be text/html (plain text HTML page), Text/plain (plain text file), and so on.

2. Make sure jquery Ajax is not garbled before delivery

The Ajax code posted below, the parameter I want to pass to the background is 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 passing);
$.ajax ({
type: ' Get ',
URL: '/sgame/servlet/indexsearchitems ',
Data:{criticalword:encodeuri ( Request.term)},
success:function (RESPONSE,STATUS,XHR) {
alert (response);}}
);
Delay:100
})

As I marked the Code red, you are before the transmission of alert () window, test to see if your JS file has garbled, if it is, the solution:

1. Is it not resolved to go back to the previous step?

2.js file Encoding problem: Open JS in the Resource Manager Notepad, then save As, select Utf-8 in the code

The following figure:

When your first window is not garbled, that means that there is no problem before passing to the servlet, that before entering the next step to do a preparatory work, as shown in the red code above, first give the parameter transcoding:

Criticalword:encodeuri (Request.term)

This transcoding is to prevent the occurrence of garbled functions in the servlet, in the form of: encodeURI (param)

3. Ensure that the servlet is accepting Ajax parameters (request) No garbled

To this point you are not far from success, only need to request and Resopnse set the encoding method, and then decode it, Doget code as follows:

public void doget (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
Response.setcontenttype ("Text/html;charset=utf-8");
Request.setcharacterencoding ("Utf-8");
PrintWriter out = 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 response back to JSP or Ajax character encoding.

B.request.setcharacterencoding (): is used to set the response encoding for the receive request.

C.urldecoder.decode (): This function needs to first import the java.net package to decode the AJAX encoding.

After completing the three steps above, you can like me, before returning to add a System.out.println (Yourparam) Try, if it is Chinese then there is no problem

4. Ensure that the servlet in response (response) to the JSP is not garbled

In fact, in the third part of the point B set well, actually return to the HTML or JSP should be the Chinese.

Note: If you are using the Post method, simply add Contenttype:application/x-www-form-urlencoded;charset=utf-8 to Ajax and do not need to do transcoding.

Related Article

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.