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:
- ﹤%@ page contentType="text/html;charset=gb2312" %﹥
Or:
- <%
- StringHi="Hello";
- Byte []
- Tmpbyte=Hi. GetBytes ("ISO8859_1 ");
- Hi=New
- String (tmpbyte );
- Out. print (Hi );
- %>
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:
- ﹤%= 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:
- String name1=new
- String(request.getParameter("user_id").getBytes("ISO8859_1"));
In addition, jdk1.3 does not need to be added.
- ﹤%@ 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:
- ﹤%@ 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:
- <%@ PageContentType="Text/html; charset = gb2312"%>
- <Html>
- <%
- String
- Hi="Hello";
- Byte []Tmpbyte=Hi. GetBytes ("ISO8859_1 ");
- Hi=New
- String (tmpbyte );
- Out. print (Hi );
- %>
- </Body>
-
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
- ﹤meta http-equiv="Content-Type" content="text/html;charset=gb2312"﹥
No
- ﹤%@ 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.
- Functions and principles of several encodings in JSP and Servlet
- Introduction to JSP: several solutions to avoid repeated submission of Form forms
- Five Aspects to get started with JSP
- JSP2.0 features of basic JSP tutorial knowledge
- JSP entry-level website environment setup steps