Solve Chinese garbled characters in jsp URL parameters

Source: Internet
Author: User
Tags tomcat server

Garbled characters occur when the jsp tutorial is used to pass Chinese parameters to the servlet at the backend. After reading the code on the Internet, there are many codec methods available and there are many other methods. Finally, this method is the simplest.

Open tomcat server. xml

Add the blue part!

<Connection port = "8080"
Maxthreads = "150" minsparethreads = "25" maxsparethreads = "75"
Enablelookups tutorial = "false" redirectport = "8443" acceptcount = "100"
Debug = "0" connectiontimeout = "20000"
Disableuploadtimeout = "true" uriencoding = "UTF-8 or gbk"/>


Test instance code

Java

Public class setcharacterencodingfilter implements filter
{
Protected string encoding = null;
Protected filterconfig = null;
Protected boolean ignore = true;
   
Public void destroy ()
    {
       
This. encoding = null;
This. filterconfig = null;
       
    }
Public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception
    {
       
If (ignore | (request. getcharacterencoding () = null ))
        {
String encoding = selectencoding (request );
If (encoding! = Null)
Request. setcharacterencoding (encoding );
        }
   
Chain. dofilter (request, response );
       
    }
   
Public void init (filterconfig) throws servletexception
    {
       
This. filterconfig = filterconfig;
This. encoding = filterconfig. getinitparameter ("encoding ");
String value = filterconfig. getinitparameter ("ignore ");
If (value = null)
This. ignore = true;
Else if (value. Inclusignorecase ("true "))
This. ignore = true;
Else if (value. Inclusignorecase ("yes "))
This. ignore = true;
Else
This. ignore = false;
       
    }
   
Protected string selectencoding (servletrequest request)
    {
       
Return (this. encoding );
       
    }
}

Web. xml configuration

<

Filter>
<Filter-name> encodingfilter </filter-name>
<Filter-class> com. dfgg. sms. web. setcharacterencodingfilter </filter-class>
<Init-param>
<Param-name> encoding </param-name>
<Param-value> UTF-8 </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-name> encodingfilter </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>
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.