Solutions to JSP Chinese problems

Time:2022-5-5

In JSP, Chinese garbled code often makes people feel confused.
For the common Countermeasures of Chinese processing, the following two kinds are often seen on the Internet:

<%@ page
contentType=”text/html;charset=gb2312″ %>  

Or:

<%
String hi = “hello”;
byte[]
tmpbyte=Hi.getBytes(“ISO8859_1”);
Hi=new
String(tmpbyte);
out.print(Hi);
%>  

The virtual Dragon Pavilion has also been modified by the above two methods, which has become version 1.1.

Through a brief summary, the Chinese processing in the example occurs in the following places:

1. Chinese parameters are attached to the URL, which can be read directly.

For example:

<%= request.getParameter(“showword”)%>  

2 various SQL operations related to the database

There is no problem with the access used here.

3 read HTML

Chinese value submitted in form

It needs to be encoded in jswdk. The more concise writing method is as follows:

String name1=new
String(request.getParameter(“user_id”).getBytes(“ISO8859_1”));  

In addition, in jdk1 With the support of 3, you don’t need to join

<%@
page contentType=”text/html;charset=gb2312″
%>  

And in jdk1 2.2 below, even if the two methods are used at the same time, it is very unstable.

On the resin platform, the situation is better. Just add in the first line of the page:

<%@
page contentType=”text/html;charset=gb2312″
%>

You can handle Chinese correctly.

If you add coding, it will be wrong.

5 Chinese contained in session

In jswdk, the strange thing is that if the value read out from the form is encoded, it can be displayed correctly;
However, it is not possible to give Chinese values directly.
The resin platform is very good, as above.

6. After assigning Chinese value to the variable, Chinese will be displayed correctly.

For example, the following procedure:

<%@ page contentType=”text/html;charset=gb2312″
%>
<html><head></head><body>
<%
String
Hi = “hello”;
byte[] tmpbyte=Hi.getBytes(“ISO8859_1”);
Hi=new
String(tmpbyte);
out.print(Hi);
%>
</body></html>  

The same is jswdk1 0.1, the above expression method is in jdk1 There is no problem in 2.2, but in jdk1 3 times, but it can’t be displayed.

The resin platform is very good. The same as above. After testing, it only needs to be added in < head >

<meta
http-equiv=”Content-Type” content=”text/html;
charset=gb2312″>
Instead of <% @ page
Contenttype = “text / HTML; charset = GB2312″% > can also display Chinese correctly.

Experience:

Jswdk may only be used for general development, and its stability and other problems may not be as good as commercial software.
Because jdk1 The performance of version 3 is better than that of 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, automatic compilation, can point out error lines, can support the use of JavaScript on the server, but also, at least I think it has good support for Chinese.

Of course, if we can display Chinese correctly without any processing, it will be what we expect.

In addition, I believe there should be no such problem under UNIX / Linux / Solaris.

Recommended Today

Modify user information changeinfo

When judging the persistence layer: Problem: there is such a problem when modifying user information. For example: the user’s email is not required. It was not empty originally. At this time, the user deletes the mailbox information and submits it. At this time, if it is not empty to judge whether it needs to be […]