JavaWeb19.1【Filter&Listener:过滤器Filter的概念和快速入门】

 

 

 

 

 1 package com.haifei.web.filter;
 2 
 3 import javax.servlet.*;  //注意Filter来自这个包下
 4 import javax.servlet.annotation.WebFilter;
 5 import java.io.IOException;
 6 
 7 /**
 8  *  过滤器快速入门
 9  *
10  *      通过注解配置过滤器拦截路径
11  *          或
12  *      通过web.xml配置过滤器拦截路径
13  *
14  */
15 //@WebFilter("/*") //配置拦截路径;/*表示访问本项目的所有资源之前都会执行该过滤器
16 public class FilterDemo1 implements Filter {
17     @Override
18     public void init(FilterConfig filterConfig) throws ServletException {
19 
20     }
21 
22     @Override
23     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
24         System.out.println("filterdemo1....");
25 
26         //放行
27         filterChain.doFilter(servletRequest, servletResponse);
28 
29         /*
30         未配置放行之前:
31         http://localhost:8080/day19/index.jsp
32         控制台输出filterdemo1....    页面无内容
33          */
34 
35         /*
36         未配置放行之后:
37         http://localhost:8080/day19/index.jsp
38         控制台输出filterdemo1....    页面显示index.jsp
39          */
40     }
41 
42     @Override
43     public void destroy() {
44 
45     }
46 }
47 
48 
49 /*  filter注解模式代码快速创建默认模板
50 #if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
51 #parse("File Header.java")
52 @javax.servlet.annotation.WebFilter(filterName = "${Entity_Name}")
53 public class ${Class_Name} implements javax.servlet.Filter {
54     public void destroy() {
55     }
56 
57     public void doFilter(javax.servlet.ServletRequest req, javax.servlet.ServletResponse resp, javax.servlet.FilterChain chain) throws javax.servlet.ServletException, java.io.IOException {
58         chain.doFilter(req, resp);
59     }
60 
61     public void init(javax.servlet.FilterConfig config) throws javax.servlet.ServletException {
62 
63     }
64 
65 }
66  */
67 
68 /* 自定义修改模板
69 #if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
70 #parse("File Header.java")
71 @javax.servlet.annotation.WebFilter("/*")
72 public class ${Class_Name} implements javax.servlet.Filter {
73 
74     public void doFilter(javax.servlet.ServletRequest req, javax.servlet.ServletResponse resp, javax.servlet.FilterChain chain) throws javax.servlet.ServletException, java.io.IOException {
75         chain.doFilter(req, resp);
76     }
77 
78     public void init(javax.servlet.FilterConfig config) throws javax.servlet.ServletException {
79 
80     }
81 
82     public void destroy() {
83 
84     }
85 
86 }
87  */

 

 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: yubaby
 4   Date: 2021/7/4
 5   Time: 21:35
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <html>
10   <head>
11     <title>$Title$</title>
12   </head>
13   <body>
14   index.jsp
15   </body>
16 </html>
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 5          version="3.1">
 6 
 7 
 8     <!--<filter>
 9         <filter-name>demo1</filter-name>
10         <filter-class>com.haifei.web.filter.FilterDemo1</filter-class>
11     </filter>
12     <filter-mapping>
13         <filter-name>demo1</filter-name>
14         <url-pattern>/*</url-pattern>  &lt;!&ndash;拦截路径&ndash;&gt;
15     </filter-mapping>-->
16 
17 
18 </web-app>

 

 

 

 

posted @ 2021-07-05 14:48  yub4by  阅读(49)  评论(0编辑  收藏  举报