Filter
Chapter 1 basic use of filter
1. Initial filter
- Filter - filter:
- Filter is a component of J2EE Servlet module
- The function of Filter is to block URL s in a unified way
- Filter is usually used for global processing at the application level
-
Three elements of filter development:
*Any filter must implement the javax. Servlet. Fiter interface
*Write the function code of the Filter in the doFilter() method of the Filter interface
*Configure the filter in web.xml to indicate the scope of intercepting URL
2. Develop the first filter
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>first-filter</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- filter Tags are used to indicate which class is a filter and are loaded automatically when the application starts --> <filter> <filter-name>MyFirstFilter</filter-name> <filter-class>com.imooc.filter.MyFirstFilter</filter-class> </filter> <!-- <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>com.imooc.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> <init-param> <param-name>p1</param-name> <param-value>v1</param-value> </init-param> <init-param> <param-name>p2</param-name> <param-value>v2</param-value> </init-param> </filter> --> <!-- filter-mapping Labels are used to describe filter pairsURLThere are two main points of application: 1. filter-name Filter name and filter.filter-name Bring into correspondence with 2. url-pattern Describe the range of action of the filter,/* For all URL Filtering --> <filter-mapping> <filter-name>MyFirstFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> --> </web-app> MyFirstFilter.java ```javascript 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; //To be a filter, any java class must implement the javax. Servlet. Fiter interface public class MyFirstFilter implements Filter { @Override public void destroy() { // TODO Auto-generated method stub System.out.println("Filter destroyed");; } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub System.out.println("Filter in effect"); chain.doFilter(request, response); } @Override public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub System.out.println("Filter initialized successfully"); } }