Home > Java > javaTutorial > How to implement java filter

How to implement java filter

(*-*)浩
Release: 2020-09-17 18:27:51
Original
7226 people have browsed it

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初始化----");
    }
}
Copy after login

Configure the filter in web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!--配置过滤器-->
  <filter>
      <filter-name>FilterTest</filter-name>
      <filter-class>com.yangcq.filter.FilterTest</filter-class>
  </filter>
  <!--映射过滤器-->
  <filter-mapping>
      <filter-name>FilterTest</filter-name>
      <!--“/*”表示拦截所有的请求 -->
      <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template