JSP uses filters to solve Chinese request garbled characters

Source: Internet
Author: User

JSP uses filters to solve Chinese request garbled characters

(1) client data is generally submitted to the server through http get/POST, and request. getParameter ()

When reading parameters, Chinese characters may be garbled.

(2) Use a filter to solve the 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 implementation Filter interface private FilterConfig = 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); // forward the filtered request object to the next filter }}


(4) Deploy the filter. Edit the WEB-INF \ web. xml file and add the following:

     
  
   cf
      
  
   my.ChineseFilter
  
 
     
  
   cf
      
  
   /*
      
  
   REQUEST
      
  
   FORWARD
      
  
   INCLUDE
  
 

Here It is mainly used with RequestDispatcher.


1. When the value is REQUEST, the filter can be activated only when a REQUEST comes directly from the client. If the REQUEST comes from RequestDispatcher. forward, It is not activated;


2. If the value is FORWARD, the filter is activated if the request is from RequestDispatcher. forward;


3. When the value is INCLUDE, this filter is activated if the request is from RequestDispatcher. include;


4. When the value is "ERROR", it indicates that this filter is activated only when the request is from RequestDispatcher using the "ERROR information page;


5. The default value is REQUEST.


(5) create a jsp page for verification

<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. SQL. *" errorPage = "" %>
 Untitled document<% String s = request. getParameter ("data"); out. print (s); %>


(6) OK! We hope you have succeeded!

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.