JSP Chinese Filter

Source: Internet
Author: User

When Using JSP for web projects, we may encounter problems that cannot be displayed in Chinese.
In servlet, request. setcharacterencoding ("gb2312"); (the request is an object of httpservletrequest) is used for processing.
Every servlet is very troublesome.
The following describes how to use a filter to solve this problem, which is simple and convenient and does not require repeated writes.

1. First write an encodingfilter. Java

Package AA;
Import java. Io .*;
Import javax. servlet .*;

Public class encodingfilter implements Filter
{
Protected string encoding = NULL;
Protected filterconfig config;

Public void Init (filterconfig) throws servletexception
{
This. Config = filterconfig;
// Obtain the encoding configuration from the Web. xml configuration file
This. Encoding = filterconfig. getinitparameter ("encoding ");
}
Public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception
{
If (request. getcharacterencoding () = NULL)
{
String encode = getencoding ();
If (encode! = NULL)
{
// Set the Request Encoding Method
Request. setcharacterencoding (encode );
}
}
Chain. dofilter (request, response );
}
Public String getencoding ()
{
Return encoding;
}
Public void destroy ()
{

}

2. Configure the following content in the web. xml file:

<Filter>

<Filter-Name> encodingfilter </filter-Name>

<Filter-class> AA. encodingfilter </filter-Name>

<Init-param>

<Param-Name> encoding </param-Name>

<Param-value> gb2312 </param-value>

</Init-param>

</Filter>

<Filter-mapping>

<Filter-Name> encodingfilter </filter-Name>

<URL-pattern>/* </url-pattern>

</Filter-mapping>

3. Restart the service and solve the problem.

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.