JSP with filter to solve request GetParameter Chinese garbled problem _jsp programming

Source: Internet
Author: User

(1) The data of the client is usually submitted to the server through the HTTP Get/post way, when the server end uses Request.getparameter () to read the parameter, it is 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 "> 
 
 

(6) ok! At this end hope you succeed!

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.