Character encoding of JSP and Servlet in Tomcat

Source: Internet
Author: User

Character encoding of JSP and Servlet in Tomcat
 
Use filter to change the Request Encoding

In the previous article, we discussed the issue of JSP and Servlet character encoding under tomcat!

Knowing that when no request encoding is specified, the data obtained from the client is iso-8859-1 encoded (request. getparameter () to get the passed parameter value );

But how can we change the Request Encoding?

There are many methods!

For example, before getrequestdispatcher ("/JSP/jsptoserv/Hello. jsp"). Forward (request, response );

Request Encoding. The parameter value obtained in JSP/jsptoserv/Hello. jsp is the encoded character.

In this article, we use filter to modify the request encoding!

 

1) first write the filter class:

Package myfilter;

 

Import java. Io. ioexception;
Import javax. servlet .*;

 

Public class changecharsetfilter implements filter {

 

Protected string encoding = NULL; // The encoding to be formulated, which is configured in Web. xml.

Protected filterconfig = NULL;

Public void destroy (){

This. Encoding = NULL;
This. filterconfig = NULL;

}

Public void dofilter (servletrequest request, servletresponse response, filterchain chain)
Throws ioexception, servletexception {

If (request. getcharacterencoding () = NULL ){
String encoding = getencoding (); // get the specified encoding name.
If (encoding! = NULL)
Request. setcharacterencoding (encoding); // you can specify the request encoding.
}

Chain. dofilter (request, response); // you have the opportunity to execute the next filter.

}

Public void Init (filterconfig) throws servletexception {

This. filterconfig = filterconfig;
This. Encoding = filterconfig. getinitparameter ("encoding"); // get the encoding configured in Web. xml
}

 

Protected string getencoding (){

Return (this. Encoding); // get the specified Encoding

}

 

}

 

2. Edit the Web. xml file

<? XML version = "1.0" encoding = "ISO-8859-1"?>

<! Doctype web-app
Public "-// Sun Microsystems, Inc. // DTD web application 2.3 // en"
Http://java.sun.com/dtd/web-app_2_3.dtd>

<Web-app>

<Filter>
<Filter-Name> setcharacterencoding </filter-Name>
<Filter-class> myfilter. changecharsetfilter </filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> gb2312 </param-value> //// specify the Encoding As gb2312.
</Init-param>
</Filter>
<Filter-mapping>
<Filter-Name> setcharacterencoding </filter-Name>
<URL-pattern>/* </url-pattern> // change the encoding of all requests.
</Filter-mapping>

</Web-app>
///

3.

Write a. jsp

<% @ Page contenttype = "text/html; charset = gb2312" %>
<HTML>
<Head> <Body>
<%
String name = request. getparameter ("name"); // originally, the character is iso-8859-1 encoded and cannot be directly

Output in the console, but now the request encoding method is changed. The name encoding is gb2312, so it can be correctly displayed in the console.

System. Out. println (name );

%>
<Form action = "A. jsp" method = "Post">
<Input type = "text" name = "name">
<Input type = "Submit">
</Form>
<% = Name %>
</Body>
</Html>

Complete!

This is all about Chinese processing!

Related Article

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.