Solves the problem of POST and GET Chinese encoding and full-Site Chinese encoding.

Source: Internet
Author: User

Solves the problem of POST and GET Chinese encoding and full-Site Chinese encoding.

In fact, the difficulty of the whole site garbled problem is the problem of processing GET request parameters.

If we only handle the POST Request Encoding and response encoding, this will solve the response well. setContentType ("text/html; charset = UTF-8"); and request. setCharacterEncoding ("UTF-8 ");

 

Okay, Here we mainly solve the GET request parameter problem. I am using a Filter (interceptor) Filter to solve it,

My thinking is:

First, define a filter for allDecodingFilter. This filter filters all files in the entire site (<url-pattern>/* </url-pattern> ),

1. to solve the problem of GET request parameters, we first define a decoration class (this involves the modifier mode). The common saying of the decoration class is to rebuild the methods or attributes of the original class. I will define a decoration class named DecodingRequest, which must inherit from the HttpServletRequestWrapper class, because this class inherits the HttpServletRequest interface and implements all methods of the HttpServletRequest interface, so long as I inherit the HttpServletRequestWrapper class, I can rewrite the methods in the class (if you are interested, you can look at the j2ee source code ). Here we will mainly rewrite the getParameter (String name) method.

2. back in the filter, I can pass the DecodingRequest class defined by myself to the doget () and dopost () Methods of Servlet. When the getParameter (String name) parameter needs to be obtained, actually, the getParameter (String name) method defined by myself will not be garbled. It may not be good to dictate. Let me talk about it through code.

 

Filter allDecodingFilter:

Public class allDecodingFilter implements Filter {public void destroy () {}/ **** @ see Filter # doFilter (ServletRequest, ServletResponse, FilterChain) */public void doFilter (ServletRequest request, servletResponse response, FilterChain chain) throws IOException, ServletException {HttpServletRequest req = (HttpServletRequest) request; // process response code HttpServletResponse reps = (HttpServletResponse) response; response. setContentType ("text/html; charset = UTF-8"); // process the garbled request of post request parameters. setCharacterEncoding ("UTF-8"); // if it is a Get request, use the method of parameters obtained by the decoration class if (req. getMethod (). equals ("GET") {HttpServletRequest newReq = new DecodingRequest (req); // this class will be stated next/** request 1. custom request decoration Class 2. when the interception passes, use our own request 3. * In Servlet *, only you call the request. the essence of the getParameter (name) method is to execute a custom request. getParameter * (name) Method */chain. doFilter (newReq, response);} else {// If post is used, no decorative chain is used. doFilter (request, response) ;}} public void init (FilterConfig fConfig) throws ServletException {}}

DecodingRequest

/*** Decoration class (this is the decorator mode) * HttpServletRequestWrapper: This class helps us implement all the methods of the HttpServletRequest interface, * Then we can implement the methods in the method as needed */public class DecodingRequest extends HttpServletRequestWrapper {private HttpServletRequest req; public DecodingRequest (HttpServletRequest request) {super (request); this. req = request;}/*** get the parameter and modify the encoding method. When you get the parameter, this method is executed */@ Override public String getParameter (String name) {try {// modify the character encoding method name = new String (req. getParameter (name ). getBytes ("iso-8859-1"), "UTF-8");} catch (UnsupportedEncodingException e) {e. printStackTrace () ;}return name ;}}

 

Servlet Testing

Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// the request in the DecodingRequest class is actually called here. getParameter ("uname"); Method String name = request. getParameter ("uname"); System. out. println (name); response. getWriter (). print (name);} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String name = request. getParameter ("uname"); System. out. println (name); response. getWriter (). print (name );}

 

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.