Solutions to JSP Chinese garbled characters in programming

Source: Internet
Author: User

The issue of JSP Chinese garbled characters is often confusing, especially for beginners who are familiar with JSP programming.

Common countermeasures for JSP Chinese processing are as follows:

 
 
  1. ﹤%@ page contentType="text/html;charset=gb2312" %﹥  

Or:

 
 
  1. <%
  2. StringHi="Hello";
  3. Byte []
  4. Tmpbyte=Hi. GetBytes ("ISO8859_1 ");
  5. Hi=New 
  6. String (tmpbyte );
  7. Out. print (Hi );
  8. %>

The virtual Dragon pavilion also uses the above two methods for modification, thus becoming version 1.1.

Through a simple summary, the Chinese garbled characters in JSP in the example occur in the following places:

◆ Chinese parameters attached to the URL can be directly read.

For example:

 
 
  1. ﹤%= request.getParameter("showword")%﹥   

◆ Database-related SQL operations

There is no problem with Access.


◆ Read HTML

Chinese value submitted in form

In jswdk, the code is required. The concise method is as follows:

 
 
  1. String name1=new 
  2. String(request.getParameter("user_id").getBytes("ISO8859_1"));   

In addition, jdk1.3 does not need to be added.

 
 
  1. ﹤%@ page contentType="text/html;charset=gb2312"%﹥    

In jdk1.2.2, even the two methods are not stable at the same time.

On the resin platform, the situation is good. Add the following content to the first line of the page:

 
 
  1. ﹤%@ page contentType="text/html;charset=gb2312"%﹥ 

You can correctly handle JSP Chinese garbled characters.

If the encoding is added, it is not correct.

◆ Chinese characters contained in the session

In jswdk, it is strange that if the value read from form is encoded, it can be correctly displayed;

However, it is not possible to assign a Chinese value directly.

The resin platform is good, just like above.

◆ Correct display of Chinese characters After assigning a Chinese value to the variable.

For example, the following program:

 
 
  1. <%@ PageContentType="Text/html; charset = gb2312"%>
  2. <Html>
  3. <%
  4. String
  5. Hi="Hello";
  6. Byte []Tmpbyte=Hi. GetBytes ("ISO8859_1 ");
  7. Hi=New 
  8. String (tmpbyte );
  9. Out. print (Hi );
  10. %>
  11. </Body>
  12.  

Jswdk1.0.1 is the same. The above representation is correct in jdk1.2.2, but cannot be displayed in jdk1.3.

The resin platform is good. Same as above. After testing, you only need to add it in

 
 
  1. ﹤meta http-equiv="Content-Type" content="text/html;charset=gb2312"﹥ 

No

 
 
  1. ﹤%@ pagecontentType="text/html;charset=gb2312" %﹥ 

Chinese characters can also be correctly displayed.

Experience:

Jswdk may only be used for common development, and stability and other problems may be inferior to commercial software.

Jdk1.3 has better performance than jdk1.2.2 many times and better support for Chinese, so it should be adopted.

As a free commercial software, resin is not only fast, stable, and automatically compiled, but can indicate error lines and support javascript on the server side. Moreover, at least I think the support for Chinese is good.

Of course, it is expected that JSP Chinese garbled characters can be avoided without any processing.

In addition, we believe there should be no such problem in unix/linux/solaris.

  1. Functions and principles of several encodings in JSP and Servlet
  2. Introduction to JSP: several solutions to avoid repeated submission of Form forms
  3. Five Aspects to get started with JSP
  4. JSP2.0 features of basic JSP tutorial knowledge
  5. JSP entry-level website environment setup steps

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.