About the garbled problem in JSP and servlet

Source: Internet
Author: User

In the JSP and servlet development, today I do a small example of the Chinese garbled problem, using a lot of methods, found that still can not solve. Finally, I used the filter to calculate the end.

This method is summarized as follows: Use the <%@ page pageencoding= "GB2312"%> in the JSP page, where pageencoding is GB2312, then we use the < in the filter web.xml Param-value>gb2312</param-value> also for GB2312. In a word: The above two can be consistent, such as UTF-8

I write the Java code for the filter and the configuration of the Web.xml

--------------Java code section: Charsetfilter. java

Package com;

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;


public class Charsetfilter implements Filter {
Private String charset = "GB2312";

public void Destroy () {

}

public void Dofilter (ServletRequest request, servletresponse response,
Filterchain arg2) throws IOException, Servletexception {
Request.setcharacterencoding (CharSet);
Arg2.dofilter (request, response);
}

public void init (Filterconfig arg0) throws Servletexception {
CharSet = Arg0.getinitparameter ("charset");
System.out.println ("character filter Open, current state:" +charset);
}
}


--------------Web.xml section (the following sections can be copied and pasted directly under Web.xml)

<filter>
<filter-name>CharsetFilter</filter-name>
<filter-class>com. Charsetfilter</filter-class>
<init-param>
<param-name>charset</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Source: Http://blog.csdn.net/lvlingwy
 
 

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.