Tomcat5.5 Chinese Perfect solution

Source: Internet
Author: User

When beginners learn web programming, most of them use Tomcat as a server, one of the most important problems is the Chinese garbled problem. The Java Camp tools are not as easy to use as the. NET camp, and we have to do some manual modifications to make the Web page support Chinese. Luckily, Tomcat has developed to today's version 5.5, the support for non-English has been quite good, you need to do very few simple changes can make the page support Chinese, let's look at how to make changes.

First, all pages should be designated as GBK encoding, that is, include the following instructions:

<%@ page contenttype= "TEXT/HTML;CHARSET=GBK" language= "java"%>

The second step is to modify the Web.xml file in the Tomcat directory, and the path on my computer is C:/Program files/apache Software foundation/tomcat 5.5/conf/server.xml. There is a connector node in this file, add a property uriencoding attribute to it, and assign a value to GBK for the property, the result of the modification is as follows:

< Connector port = "maxhttpheadersize" = "8192"

MaxThreads = "Minsparethreads" = "maxsparethreads" = "75"

Enablelookups = "false" Redirectport = "8443" acceptcount = "100"

ConnectionTimeout = "20000" disableuploadtimeout = "true"

uriencoding = "GBK"/>

Note that the last line, which is the distinctive green line, needs to be added manually.

In the third step, add a filter for transcoding.

As for how to write the filter, you don't have to worry about it, Tomcat comes with a very perfect example, you just need to put the example code copy to your own class is OK, simple ...

In my system, this example is located in C:/Program files/apache Software foundation/tomcat 5.5/webapps/servlets-examples/web-inf/classes/ Filters/setcharacterencodingfilter.java.

For convenience, I post the code of this class, and make its English annotation simple Chinese, you use the time copy my Code is also effective: import java.io.IOException;
Import Javax.servlet.Filter;
Import Javax.servlet.FilterChain;
Import Javax.servlet.FilterConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;
Import javax.servlet.UnavailableException;

public class Setcharacterencodingfilter implements Filter ... {

You need to convert the code, for us, of course, hope his value is "GBK"
But you don't have to worry about it, you'll set it up later.
protected String encoding = NULL;

configuration files, automatically initialized according to the configuration content in Web.xml, don't worry about it.
protected Filterconfig filterconfig = null;

Whether the encoding rules specified by the client are ignored. True indicates ignore.
Of course we have to ignore it because we have to specify coding rules ourselves. GBK, GBK, gbk~~~
Protected Boolean ignore = true;

When this filter is destroyed by the system to call, do not care about his
public void Destroy () ... {

this.encoding = null;
This.filterconfig = null;

}


The real transcoding operation is done by this function
The content is very simple, is to assign the value of encoding to request
As for encoding equals what. Determined by the following selectencoding function
public void Dofilter (ServletRequest request, servletresponse response,
Filterchain chain)
Throws IOException, servletexception ... {

Conditionally select and set the character encoding to be used
if (Ignore | | (request.getcharacterencoding () = null)) ... {
String encoding = selectencoding (request);
if (encoding!= null)
request.setcharacterencoding (encoding);
}
Chain.dofilter (request, response);
}

Initialize for Filterconfig
An important method that assigns a value to the Encoding property, which comes from the configuration file
In other words, if we're going to specify a coding rule, we don't have to specify in the program
Only need to specify in the configuration file is ok.
So, you can't see the word "GBK" in this code.
But we modify it slightly in the configuration file, and it is able to follow the GBK to make the transcoding. So flexible.
public void init (Filterconfig filterconfig) throws Servletexception ... {

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.