Use of filters

Keywords: JSP Programming Java Web Server

1. What is a filter?

  In the process of client to server, when sending a request, if there is non-conforming information, it will be intercepted by the filter, if it is conforming, it will be released, and when the server responds to the client, it will also be judged that if there is non-conforming information, it will be intercepted by the filter, if it is conforming, it will be released.

 OOP: Java object-oriented programming, abstraction, encapsulation, inheritance, polymorphism.

 AOP: aspect oriented programming, filter is a aspect oriented programming idea.

AOP is a new function introduced after srvlet 2.3 of sun company. It is not available in the version before 2.3. To define a filter, you need to implement the filter interface. Here, you need to implement javax.servlet.Filter .

 A filter is a program that runs on the server before the servlet or JSP page associated with it. Filters can be attached to one or more servlets or JSP pages, and can check for request information to enter these resources. After that, the filter can be selected as follows:

① Call resources in the normal way (that is, call servlet s or JSP pages).
② Call the resource with the modified request information.
③ Call the resource, but modify the response before sending it to the client.
④ Block the resource call, instead go to another resource, return a specific status code or generate replacement output.
2. Filter life cycle:
When the project starts, the filter starts to initialize. When a request comes, the doFilter method is automatically executed. As the project is closed, the filter stops.

3. What's the use of filters?

The main function of the Filter is to Filter requests. Through the Filter technology, all web resources managed by the web server, such as JSP, Servlet, static picture file, or static HTML file, can be intercepted, so as to realize some special functions, such as URL level permission control, sensitive vocabulary filtering, response information compression and other advanced functions.

4. How to use it?

Multiple can be written, accessed from users -- "first filter --" second filter -- "Servlet"

Filter life cycle: created as the project starts. When the address is accessed, the doFilter method is called. This will be executed many times and destroyed as the project closes.

There are three parameters when calling the doFilter method:

    reqest(ServletRequest) - (HttpServletRequest)

    response(ServletResponse) - > HttpServletResponse (pay attention to forced conversion: when using the unique method of HttpServletResponse, forced conversion is required)

     The filterchain chain can point to the next address (if there is a filter, it points to the next filter, not to the resource Servlet) to filter, intercept and release

     chain.doFilter(req,resp); / / release

ChainFilter chain:
It can point to the next resource, if there is still a filter, continue with the next filter, if there is no filter, point to the resource (servlet).


filter application scenario:

(1) . set encoding for all servlets

(2) . set up cross domain issues

(3) . solve all anti-theft chain problems

Files to be configured when using filters:

(1) . fixed address

(2) , wildcard

(3) , specify directory

package servlet;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * urlPattern={"/*"}Get the mapping path of all servlets and realize the interaction between different servlets
 */
@WebServlet(name = "DemoFilter",urlPatterns = {"/*"})
public class DemoFilter implements Filter{
    /**
     * Initialization method of filter
     * @param filterConfig Can get deployment descriptor file( web.xml )Filter initialization parameters assigned in.
     * @throws ServletException
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("Filter created");
    }

    /**
     *
     * @param servletRequest request
     * @param servletResponse response
     * @param filterChain  Chain, stop and release filter
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("Filter executed");
        servletResponse.setContentType("text/html,charset=utf-8");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.addHeader("Access-Control-Allow-Origin","*");
        String referer = request.getHeader("referer");
        System.out.println(referer);
        filterChain.doFilter(servletRequest,servletResponse);//Release
    }

    @Override
    public void destroy() {
        System.out.println("The filter was destroyed");
    }
}

Posted by chadrt on Sun, 14 Jun 2020 21:23:07 -0700