Home  >  Article  >  Java  >  How to implement java filter

How to implement java filter

(*-*)浩
(*-*)浩Original
2019-05-22 16:51:537067browse

Java filter implementation steps: 1. Write a filter class to implement the Filter interface; 2. Implement the methods that have not yet been implemented in the interface (focus on implementing the doFilter method); 3. Configure in web.xml (The main thing is to configure which resources should be filtered).

How to implement java filter

Filter is also called a filter. It is one of the most exciting technologies in Servlet technology. WEB developers use Filter technology to manage web servers. All web resources: such as Jsp, Servlet, static image files or static html files, etc. are intercepted to achieve some special functions. For example, some advanced functions such as URL-level permission access control, sensitive vocabulary filtering, and response information compression can be implemented.

So how is it achieved?

Write a filter class to implement the Filter interface

Implement the methods that have not yet been implemented in the interface (focus on implementing the doFilter method)

Configure in web.xml (Mainly configuring which resources to filter)

How does it work?

There is a doFilter method in the Filter interface. When we write the Filter and configure which web resource to intercept, the WEB server will call it every time before calling the service method of the web resource. filter's doFilter method, therefore,

Writing code in this method can achieve the following purposes:
Let a piece of code execute before calling the target resource.
Whether to call the target resource (that is, whether to allow users to access web resources).
After calling the target resource, let a piece of code execute.
When the web server calls the doFilter method, it will pass in a filterChain object. The filterChain object is the most important object in the filter interface. It also provides a
doFilter method. Developers can decide whether to call this based on their needs. Method, when this method is called, the web server will call the service method of the web resource, that is, the web resource will be accessed, otherwise the web resource will not be accessed.

Filter example:

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;
/**
* @author yangcq
* @description 过滤器Filter的工作原理
*/
public class FilterTest implements Filter{
    public void destroy() {
        System.out.println("----Filter销毁----");
    }
public void doFilter(ServletRequest request, ServletResponse response,FilterChain filterChain) throws IOException, ServletException {
    // 对request、response进行一些预处理
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=UTF-8");
    System.out.println("----调用service之前执行一段代码----");
    filterChain.doFilter(request, response); // 执行目标资源,放行
    System.out.println("----调用service之后执行一段代码----");
}
    public void init(FilterConfig arg0) throws ServletException {
        System.out.println("----Filter初始化----");
    }
}

Configure the filter in web.xml:



      
  
    index.jsp
  
  
  
      FilterTest
      com.yangcq.filter.FilterTest
  
  
  
      FilterTest
      
      /*
  

The above is the detailed content of How to implement java filter. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn