Use the filter and modifier design mode (static proxy) to solve the getParameter garbled problem, getparameter Chinese garbled

Source: Internet
Author: User

Use the filter and modifier design mode (static proxy) to solve the getParameter garbled problem, getparameter Chinese garbled

The post garbled problem is better solved. Here we mainly deal with the garbled get request.

 

Solution: Enhance the getParameter method of the request object so that getParameter can directly obtain the data after garbled characters.

 

There are four methods,

1. Inheritance: (next policy)
① Clearly know the full class name
You can print the request implementation class and know the full Class Name of the implementation class.

(2) The new parent class can be added to the subclass.
Are you sure the request implementation class is new?

Make sure that you write general methods. Tomcat, weblogic, jboss ......


Disadvantages:
① Difficult to implement, almost no scalability
② It will take up valuable extends bits

2. Decoration Design Mode [Static proxy]
Advantages:
① You do not need to know the implementation class. You can use interfaces. Highly scalable code
② Do not use valuable extends bits


Steps:
① The enhancement class must implement all interfaces implemented by the enhancement class
② The enhancement class must be able to obtain the referenced class

Disadvantages:
If there are too many irrelevant methods in the interface, a lot of implementation is wasted.

Solution:
1. Use Dynamic proxy
2. Find the packaging class. You only need to inherit the packaging class, sub-packaging/sub-enhancement class.
HttpServletRequestWrapper

3. Dynamic proxy (not to mention it for the moment)
4. Enhanced bytecode (not to mention it for the moment)

 

Code implementation:

  

Solution: Use the static proxy to enhance the getParameter function.

 

 

 

1. Your class inherits HttpServletRequestWrapper and overwrites the getParameter method. 1. The enhancement class you write.
 1 package com.itheim.utils; 2 import java.io.UnsupportedEncodingException; 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletRequestWrapper; 5 public class MyHttpRequest extends HttpServletRequestWrapper { 6     HttpServletRequest req; 7     public MyHttpRequest(HttpServletRequest old) { 8         super(old); 9         // TODO Auto-generated constructor stub10         this.req=old;11     }12     @Override13     public String getParameter(String name) {14         String method = req.getMethod();15         if("get".equalsIgnoreCase(method)){16             String par = req.getParameter(name);17             if(par!=null){18                 String result = null;19                 try {20                     result=new String(par.getBytes("iso8859-1"), "utf-8");21                 } catch (UnsupportedEncodingException e) {22                     // TODO Auto-generated catch block23                     throw new RuntimeException(e);24                 }25                 return result;26             }27             28         }29         30         return req.getParameter(name);31     }32 }

2. Code implementation in the filter

1 public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {2 // fix post garbled 3 request. setCharacterEncoding ("UTF-8"); 4 // address get garbled 5 HttpServletRequest httpServletRequest = (HttpServletRequest) request; 6 MyHttpRequest request2 = new MyHttpRequest (httpServletRequest); 7 chain. doFilter (request2, response); 8}

 

 

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.