JSP garbled Problem and Solution

Source: Internet
Author: User

Thoughts on JSP garbled code and Solutions

I. possible causes of garbled characters include:

1. garbled characters in the response: displays garbled characters on the page. For example, when index. jsp is displayed, "User Name" is garbled.

2. garbled characters when the form is submitted: a garbled code occurs when the form is entered as Chinese in the form text box.

Ii. Solutions for various causes:

1. JSP garbled characters in the response:

Note:

1). Common encoding methods:

ISO-8859-1, GB18030, GB2312, UTF-8;

2) Meaning of pageEncoding:

PageEncoding indicates the encoding of the JSP page, that is, you want to save a JSP page. The system will use this encoding to save it. When we know that the text file is saved as a file, a prompt window will pop up, after careful consideration, you will find that you can choose different "encoding" to save ),
Similarly, when the JSP file is saved by default, the JSP page will be saved with the pageEncoding attribute value you set.
In Eclipse, if you set Encoding on Window \ preferences \ jspfiles to a value such as GB2312, the system will automatically add pageEncoding = "GB2312" when creating a JSP file.

Note: pageEncoding = "ISO-8859-1" is not to save Chinese, that is, if you set to ISO-8859-1, but want to write a Chinese paragraph in the JSP file, when saving the file, you will be prompted that the file cannot be saved.

3) JSP implementation process:

The customer submits a URL request such as www.WebTest.com/index.jsp.

The server receives the request

The server looks for the local file index. jsp

The server translates index. jsp into index. java and then compiles it into the index_jsp.class file, and sends it as a response to the client.

The client browser interprets and executes the HTML code and displays it to the customer.

As can be seen from the above, three points must be taken into account to solve the JSP garbled problem in the response:

I. Check the pageEncoding attribute of the local file index. jsp, because the server first looks for this file after receiving the request. PageEncoding can be set to GB18030, GB2312, UTF-8

II. JSP uses <% @ pagecontentType = "text/html; charset = GB2312" %> to add encoding information for the response, if charset = "ISO-8859-1 ", the displayed Chinese characters must be garbled characters.

III. Remember to add <metahttp-equiv = "Content-Type" content = "text/html" to

After the preceding three steps, the response Garbled text shows Garbled text on the page.

2. Form submission garbled characters:

Note:

1) the main reason for the garbled characters in the form text box when the input is Chinese is submitted to the service is: the form is submitted, that is, the encoding used in the request sending process is ISO-8859-1.

The form submission methods include POST and GET. Therefore, the form submission garbled characters include POST and GET garbled characters.

I. POST garbled characters

The solution is to add the Statement on the JSP page: <% request. setCharacterEncoding ("UTF-8"); %>

II. GET garbled characters

The solution in POST mode is simple, because the data submitted in POST mode is sent in binary mode and appended to the body part of the http request, you only need to specify the encoding format in the background.

In GET mode, parameters are directly appended to the url. The request. setCharacterEncoding () cannot be used for processing these parameters. The result is that all the Chinese files in get form are garbled.

Here, only the submitted content can be converted into bytes and then converted into another encoded character:

 
 
  1. <%  
  2. Stringusername=request.getParameter("username");  
  3. byte[]bytes=username.getBytes("iso-8859-1");  
  4. Stringresult=newString(bytes,"gb2312");  
  5. out.print(result);  
  6. %> 

It is easy to understand: First, I want to obtain a String that is different from the existing encoding. The constructor in the String class: newString (byte [], charsetch );
Now I need a byte array, that is, to convert the original String, from String to byte array, the getBytes (charsetch) function in the String class

So the combination is: newString (username. getBytes ("iso-8859-1"), "gb2312 ");

  1. Introduction to JSP page Jump and request forwarding
  2. Analysis of garbled characters in JSP Web development in Tomcat
  3. How to accelerate JSP access
  4. Which of ASP. NET, JSP, and PHP is better?
  5. JSP-related software

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.