Filter filter Note 1, Filter filter note

Source: Internet
Author: User

Filter filter Note 1, Filter filter note

Filter: Filter  

Filters are mainly used to pre-process user requests. They can also post-process HttpServletResponse, which is a typical processing chain. The complete process of using the Filter is: the Filter processes user requests, then sends the requests to the Servlet for processing and generates a response, and finally the Filter processes the server response.


To create a Filter:

1. Create a Filter processing class

2. Configure Filter in the web. xml file (or directly use annotations)

To create a Filter, you must implement the javax. servlet. Filter interface, which defines three methods.
Void init (FilterConfig config): used to initialize the Filter.
Void destroy (): Used to recycle certain resources before the Filter is destroyed.
Void doFilter (ServletRequest request, ServletResponse response, FilterChain chain): implements the filter function. This method is used to add additional processing for each request and response.

In the following example, a practical Filter is used to Filter user requests. The Filter uses the doFilter method to set the character set of request Encoding, so that each jsp and Servlet must be set, the system also checks whether the user is logged on. If the user is not logged on, the system directly jumps to the logon page.


Program example:

/*** Description: Set the response set for the request, and verify that the user is logged on to * Author: Eleven * Date: 2018/1/6 */@ WebFilter (filterName = "loginFilter ", urlPatterns = {"/*"}, // This Filter intercepts all user requests under this item. initParams = {// configure the Filter initialization parameter @ WebInitParam (name = "encoding ", value = "GBK"), @ WebInitParam (name = "loginPage", value = "/login. jsp ")}) public class LoginFilter implements Filter {// FilterConfig can be used to access the configuration information of the Filter private FilterConfig config; @ O Verride public void init (FilterConfig filterConfig) throws ServletException {this. config = filterConfig ;}@ Override public void doFilter (ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {// get the Filter configuration parameter String encoding = config. getInitParameter ("encoding"); String loginPage = config. getInitParameter ("loginPage"); // sets the request Encoding Character Set servletRequest. setCharacterEncoding (encoding); System. out. println ("encoding completed"); HttpServletRequest request = (HttpServletRequest) servletRequest; // obtain the client request page String requestPath = request. getServletPath (); System. out. println ("user request page:" + requestPath); // determines whether the user has logged on to HttpSession session = request. getSession (); // No Logon. if (session. getAttribute ("user") = null &&! RequestPath. endsWith (loginPage) {// forward the request to the logon page. getRequestDispatcher ("/jsp/login. jsp "). forward (servletRequest, servletResponse);} else {// release chain. doFilter (servletRequest, servletResponse) ;}@override public void destroy () {this. config = null ;}}

  

Filter Type:
1. User-authorized Filter: The Filter checks user requests and filters illegal user requests according to requests.

2. LOG Filter: records some special user requests in detail.

3. Filter responsible for decoding: including request Decoding for non-standard encoding

4. XSLT Filter that can change XML content

5. Filters can intercept multiple requests or responses. One request or response can also be intercepted by multiple filters.

 

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.