Filter 4: Case-Character Set Filter; (Set the encoding of the request and response, and solve the Chinese garbled code by means of a filter)

Character set filter: Solve the problem of Chinese garbled characters in web applications.

(1) This is a complete case of filters; (2) Show the use of filters to solve the problem of Chinese garbled characters; (3) It can be found that the role of filters is not only limited to "intercept, screening", filters are also Many other types of functions can be completed, that is, the filter can "intercept" a request (or response), and then perform other processing on the request (or response) before releasing it .

table of Contents

One: Problem statement

Two: start development

1. Write the CharacterEncodingFilter class, implement the Filter interface, and write the doFilter method as a character set filter:

2. Configure the filter (same as before)

3. Completion of writing, start testing:


One: Problem statement

For the Chinese garbled problem: The Servlet request and response Chinese garbled problem is described in detail in this blog.

In the above solutions, you need to configure or write code according to certain rules; and if the code is wrong, there will be Chinese garbled problems. Therefore, in order to deal with this problem that may go wrong, the character set filter is implemented. Raw.

The role of the character set filter is to pre-process all requests and set the character set in the request and response in a unified way. In this way, when using Servlet development, there is no need to write two and three in the figure above. In other words, this can greatly reduce the risk of programmer errors.


Two: start development

1. Write the CharacterEncodingFilter class, implement the Filter interface, and write the doFilter method as a character set filter:

CharacterEncodingFilter

          Note:

              (1) Because this filter is based on HTTP requests, when writing the doFilter() method body, you need to force the top-level interface of ServletRequest to HttpServletRequest;

              (2) The Chinese garbled code of the get request has been solved by Tomcat 8.x, so there is no processing in the doFilter() method; the doFilter() method only deals with the encoding of the post request and response;

              (3) This example also confirms the filter introduction in this blog, the "filter execution process" section said: the request will go through the filter, and the response will go through the filter !

package com.imooc.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CharacterEncodingFilter implements Filter{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	/*
	 * doFilter方法的参数是ServletRequest类型,而不是HttpServletRequest类型,需要强转一下;
	 * so,为什么doFilter方法的参数为什么不设置成HttpServletRequest类型呐?
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// 因为这个过滤器是基于HTTP请求来进行的,所以需要将ServletRequest转成HttpServletRequest
		HttpServletRequest req = (HttpServletRequest)request;
		req.setCharacterEncoding("UTF-8");
		HttpServletResponse res = (HttpServletResponse)response;
		res.setContentType("text/html;charset=UTF-8");
		
		chain.doFilter(request, response);
		
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}

So why, the parameters in the doFilter method are not directly set to HttpServletRequest?

When each request data sent from the client to the server is parsed by the RequestFacade class; therefore, as long as you are using Tomcat and see the HttpServletRequest interface, you need to understand that the bottom layer is processed by the RequestFacade class; if You are not using Tomcat, and naturally other classes implement the HttpServletRequest interface at the bottom;

The ServletResponse interface is the same as the HttpServletResponse interface.

……………………………………………………

2. Configure the filter (same as before)

3. The character set filter is written and the test is started:

After launching the application, visiting this Servlet, the browser displays the result: It is found that the written character set filter has solved the Chinese garbled problem.

Guess you like

Origin blog.csdn.net/csucsgoat/article/details/114267499