
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
16 <%
17 System.out.println("index.jsp");
18 %>
19 </body>
20 </html>
1 <%--
2 Created by IntelliJ IDEA.
3 User: yubaby
4 Date: 2021/7/5
5 Time: 15:20
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 hello.jsp
15
16 <%
17 System.out.println("hello.jsp");
18 %>
19 </body>
20 </html>
1 package com.haifei.web.servlet;
2
3 import javax.servlet.ServletException;
4 import javax.servlet.annotation.WebServlet;
5 import javax.servlet.http.HttpServlet;
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8 import java.io.IOException;
9
10 @WebServlet("/user/FindAllServlet")
11 public class ServletDemo1 extends HttpServlet {
12 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
13 System.out.println("FindAllServlet......");
14 }
15
16 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
17 this.doPost(request, response);
18 }
19 }
1 package com.haifei.web.servlet;
2
3 import javax.servlet.ServletException;
4 import javax.servlet.annotation.WebServlet;
5 import javax.servlet.http.HttpServlet;
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8 import java.io.IOException;
9
10 @WebServlet("/user/UpdateServlet")
11 public class ServletDemo2 extends HttpServlet {
12 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
13 System.out.println("UpdateServlet......");
14 }
15
16 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
17 this.doPost(request, response);
18 }
19 }
1 package com.haifei.web.filter;
2
3 import javax.servlet.*;
4 import javax.servlet.annotation.WebFilter;
5 import java.io.IOException;
6
7 /**
8 * 过滤器细节 之 拦截路径配置
9 */
10 //@WebFilter("/index.jsp")
11 //@WebFilter("/user/*")
12 //@WebFilter("/user/FindAllServlet")
13 //@WebFilter("*.jsp")
14 //@WebFilter("/*")
15 public class FilterDemo4 implements Filter {
16 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
17 System.out.println("FilterDemo4");
18 chain.doFilter(req, resp);
19
20 /*
21 http://localhost:8080/day19/index.jsp
22 FilterDemo4
23 index.jsp
24
25 http://localhost:8080/day19/hello.jsp
26 hello.jsp
27 */
28
29 /*
30 http://localhost:8080/day19/user/FindAllServlet
31 FilterDemo4
32 FindAllServlet......
33
34 http://localhost:8080/day19/user/UpdateServlet
35 FilterDemo4
36 UpdateServlet......
37 */
38
39 /*
40 http://localhost:8080/day19/user/FindAllServlet
41 FilterDemo4
42 FindAllServlet......
43
44 http://localhost:8080/day19/user/UpdateServlet
45 UpdateServlet......
46 */
47
48 /*
49 http://localhost:8080/day19/hello.jsp
50 FilterDemo4
51 hello.jsp
52
53 http://localhost:8080/day19/user/FindAllServlet
54 FindAllServlet......
55 */
56 }
57
58 public void init(FilterConfig config) throws ServletException {
59
60 }
61
62 public void destroy() {
63
64 }
65
66 }
1 package com.haifei.web.filter;
2
3 import javax.servlet.*;
4 import javax.servlet.annotation.WebFilter;
5 import java.io.IOException;
6
7 /**
8 * 过滤器细节 之 拦截方式配置
9 */
10 //@WebFilter(value = "/index.jsp", dispatcherTypes = DispatcherType.REQUEST) //浏览器直接请求index.jsp资源时,该过滤器会被执行
11 //@WebFilter(value = "/index.jsp", dispatcherTypes = DispatcherType.FORWARD) //只有转发访问index.jsp时,该过滤器才会被执行
12 //@WebFilter(value = "/index.jsp", dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.FORWARD}) //浏览器直接请求index.jsp或者转发访问index.jsp,该过滤器都会被执行
13 //@WebFilter(value = "/*", dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.FORWARD}) //浏览器直接请求index.jsp或者转发访问index.jsp,该过滤器都会被执行
14 public class FilterDemo5 implements Filter {
15 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
16 System.out.println("FilterDemo5.....");
17 chain.doFilter(req, resp);
18
19 /*
20 http://localhost:8080/day19/index.jsp
21 FilterDemo5.....
22 index.jsp
23
24 http://localhost:8080/day19/user/UpdateServlet
25 UpdateServlet......
26 index.jsp
27 */
28
29 /*
30 http://localhost:8080/day19/index.jsp
31 index.jsp
32
33 http://localhost:8080/day19/user/UpdateServlet
34 UpdateServlet......
35 FilterDemo5.....
36 index.jsp
37 */
38
39 /*
40 http://localhost:8080/day19/index.jsp
41 FilterDemo5.....
42 index.jsp
43
44 http://localhost:8080/day19/user/UpdateServlet
45 UpdateServlet......
46 FilterDemo5.....
47 index.jsp
48 */
49
50 /*
51 http://localhost:8080/day19/index.jsp
52 FilterDemo5.....
53 index.jsp
54
55 http://localhost:8080/day19/user/UpdateServlet
56 FilterDemo5.....
57 UpdateServlet......
58 FilterDemo5.....
59 index.jsp
60 */
61 }
62
63 public void init(FilterConfig config) throws ServletException {
64
65 }
66
67 public void destroy() {
68
69 }
70
71 }
1 package com.haifei.web.servlet;
2
3 import javax.servlet.ServletException;
4 import javax.servlet.annotation.WebServlet;
5 import javax.servlet.http.HttpServlet;
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8 import java.io.IOException;
9
10 @WebServlet("/user/UpdateServlet")
11 public class ServletDemo2 extends HttpServlet {
12 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
13 System.out.println("UpdateServlet......");
14
15 //转发到index.jsp
16 request.getRequestDispatcher("/index.jsp").forward(request, response);
17 }
18
19 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
20 this.doPost(request, response);
21 }
22 }
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> <!–拦截路径–>
15 </filter-mapping>-->
16
17 <!--<filter>
18 <filter-name>demo5</filter-name>
19 <filter-class>com.haifei.web.filter.FilterDemo5</filter-class>
20 </filter>
21 <filter-mapping>
22 <filter-name>demo5</filter-name>
23 <url-pattern>/*</url-pattern>
24 <dispatcher>REQUEST</dispatcher> <!–拦截方式–>
25 </filter-mapping>-->
26
27 </web-app>