Character Set problems in Tomcat 5.x

Source: Internet
Author: User

Character Set problems in Tomcat 5.x --

 

There are three areas to be set: 1. Client 2. Tomcat 3. Server

1. The encoding format must be set for the client request. Assume that the HTTP program is used to automatically request data. The sample code is as follows:

Encode the request data using urlencoder for UTF-8 and then write it to the requested data stream using WR

Private Static inputstream httprequest (string Surl, map ATTR) throws ioexception {

String data = "";
Inputstream is = NULL;
If (ATTR! = NULL ){
Iterator it = ATTR. keyset (). iterator ();
While (it. hasnext ()){
String key = (string) it. Next ();
Data + = "&" + urlencoder. encode (key, "UTF-8") + "=" + urlencoder. encode (string) ATTR. get (key), "UTF-8 ");

}
// Send data
URL url = new URL (Surl );
Urlconnection conn = URL. openconnection ();

Conn. setdooutput (true );
Outputstreamwriter wR = new outputstreamwriter (conn. getoutputstream ());

Wr. Write (data. substring (1 ));
Wr. Flush ();
Is = conn. getinputstream ();
} Else {
URL url = new URL (Surl );
Urlconnection conn = URL. openconnection ();
Conn. Connect ();
Is = conn. getinputstream ();
}

Return is;
}

JSP should have the following content:

<% @ Page contenttype = "text/html;Charset = UTF-8"%>

 

2. Set a certain ctor in Tomcat.

In server. XML, confirm that the encoding format is set.

<ConnectorUriencoding = "UTF-8"Acceptcount = "100" connectiontimeout = "20000" disableuploadtimeout = "true" Port = "80" redirectport = "8443">
</Connector>

3. You need to set the encoding format for the server segment program. For example, you can write

Public void Service (servletrequest request, servletresponse resp)
Throws servletexception, ioexception {


Request. setcharacterencoding ("UTF-8");

Of course, server segments generally use filters to do this, so that you do not need to set the encoding format in each servlet, and you can use parameters to determine what encoding format to use.

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.