This article for everyone to share the JSP with a filter to resolve request Chinese garbled problem, the specific contents are as follows
(1) The data of the client is usually submitted to the server via HTTP get/post, and the server End is Request.getparameter ()
When reading the parameters, it is very easy to appear the Chinese garbled phenomenon.
(2) Using the filter to solve the problem of Chinese garbled request.
(3) The code is as follows:
Package my;
Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
public class Chinesefilter implements filter {//defines a filter to implement the filter interface
private filterconfig config = null;
public void init (Filterconfig config) throws servletexception {
this.config = config;
}
public void Destroy () {
config = null;
}
public void Dofilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, Servletexception
{
request.setcharacterencoding ("GB2312");
Chain.dofilter (request, response); Forwarding the filtered Request object to the next filter processing
}
(4) Deployment of filters. Edit the Web-inf\web.xml file and add the following:
<filter>
<filter-name>cf</filter-name>
<filter-class>my. chinesefilter</filter-class>
</filter>
<filter-mapping>
<FILTER-NAME>CF </filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>request</ dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>include</dispatcher >
</filter-mapping>
The <dispatcher></dispatcher> is mainly used in conjunction with RequestDispatcher.
- 1. When the value is requested when the request is directly from the client, the filter can be activated, if the request is from Requestdispatcher.forward not activated;
- 2. A value of forward indicates that the filter is activated if the request is from Requestdispatcher.forward;
- 3. The value of include indicates that the filter is activated if the request is from Requestdispatcher.include;
- 4. When the value is error indicates that the filter is activated if the request is from RequestDispatcher using the error message page;
- 5. The default is request.
(5) Create a JSP page test
<%@ page contenttype= "text/html; charset=gb2312 "language=" java "import=" java.sql.* "errorpage=" "%> <!
DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
The above is about JSP to solve the request of Chinese garbled method, I hope to help you learn.