web 编写一个过滤器

1、在com.test.filter中编写一个过滤类  如:TestFilter.java

2、定义一个servlet,如processUser.servlet  

我想过滤processUser.servlet  

3、配置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>
             
  <filter>
    <filter-name>UsersFilter</filter-name>
    <filter-class>com.test.filter.UserFilter</filter-class>
             
  </filter>
             
  <filter-mapping>
    <filter-name>UsersFilter</filter-name>
    <url-pattern>/ProcessUserServlet</url-pattern>
             
  </filter-mapping>
             
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
               
    <servlet-name>ProcessUserServlet</servlet-name>
    <servlet-class>com.test.servlet.ProcessUserServlet</servlet-class>
               
  </servlet>
  <servlet-mapping>
    <servlet-name>ProcessUserServlet</servlet-name>
    <url-pattern>/ProcessUserServlet</url-pattern>
  </servlet-mapping> 
             
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

配置完成可以使用了

你可能感兴趣的:(web编写过滤器)