|
7 | 7 | ### 一、webapp下页面测试
|
8 | 8 |
|
9 | 9 | * 1、导入jar包
|
10 |
| -```xml |
11 |
| -<dependency> |
12 |
| - <groupId>junit</groupId> |
13 |
| - <artifactId>junit</artifactId> |
14 |
| - <version>4.12</version> |
15 |
| - <scope>test</scope> |
16 |
| -</dependency> |
17 |
| -<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> |
18 |
| -<dependency> |
19 |
| - <groupId>javax.servlet</groupId> |
20 |
| - <artifactId>javax.servlet-api</artifactId> |
21 |
| - <version>3.1.0</version> |
22 |
| - <scope>provided</scope> |
23 |
| -</dependency> |
24 |
| -``` |
| 10 | + ```xml |
| 11 | + <dependency> |
| 12 | + <groupId>junit</groupId> |
| 13 | + <artifactId>junit</artifactId> |
| 14 | + <version>4.12</version> |
| 15 | + <scope>test</scope> |
| 16 | + </dependency> |
| 17 | + <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> |
| 18 | + <dependency> |
| 19 | + <groupId>javax.servlet</groupId> |
| 20 | + <artifactId>javax.servlet-api</artifactId> |
| 21 | + <version>3.1.0</version> |
| 22 | + <scope>provided</scope> |
| 23 | + </dependency> |
| 24 | + ``` |
25 | 25 | * 2、webapp/index.jsp
|
26 |
| -```jsp |
27 |
| -<%-- |
28 |
| - Created by IntelliJ IDEA. |
29 |
| - User: sunny |
30 |
| - Date: 2018/8/12 |
31 |
| - Time: 13:49 |
32 |
| - To change this template use File | Settings | File Templates. |
33 |
| ---%> |
34 |
| -<%@ page contentType="text/html;charset=UTF-8" language="java" %> |
35 |
| -<html> |
36 |
| -<head> |
37 |
| - <title>Title</title> |
38 |
| -</head> |
39 |
| -<body> |
40 |
| -<h3>hello world!</h3> |
41 |
| -</body> |
42 |
| -</html> |
43 |
| -``` |
| 26 | + ```jsp |
| 27 | + <%-- |
| 28 | + Created by IntelliJ IDEA. |
| 29 | + User: sunny |
| 30 | + Date: 2018/8/12 |
| 31 | + Time: 13:49 |
| 32 | + To change this template use File | Settings | File Templates. |
| 33 | + --%> |
| 34 | + <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
| 35 | + <html> |
| 36 | + <head> |
| 37 | + <title>Title</title> |
| 38 | + </head> |
| 39 | + <body> |
| 40 | + <h3>hello world!</h3> |
| 41 | + </body> |
| 42 | + </html> |
| 43 | + ``` |
44 | 44 | * 3、HelloServlet.java (`/`除了`.jsp`全过滤;`/*`全过滤)
|
45 |
| -```java |
46 |
| -package com.edu.servlet; |
47 |
| - |
48 |
| -import javax.servlet.ServletException; |
49 |
| -import javax.servlet.annotation.WebServlet; |
50 |
| -import javax.servlet.http.HttpServlet; |
51 |
| -import javax.servlet.http.HttpServletRequest; |
52 |
| -import javax.servlet.http.HttpServletResponse; |
53 |
| -import java.io.IOException; |
54 |
| - |
55 |
| -@WebServlet("/") //.jsp可以直接访问 |
56 |
| -public class HelloServlet extends HttpServlet { |
57 |
| - protected void doPost(HttpServletRequest request, |
58 |
| - HttpServletResponse response) |
59 |
| - throws ServletException, IOException { |
60 |
| - doGet(request,response); |
61 |
| - } |
62 |
| - |
63 |
| - protected void doGet(HttpServletRequest request, |
64 |
| - HttpServletResponse response) |
65 |
| - throws ServletException, IOException { |
66 |
| - System.out.println("=============="); |
67 |
| - } |
68 |
| -} |
69 |
| -``` |
| 45 | + ```java |
| 46 | + package com.edu.servlet; |
| 47 | + |
| 48 | + import javax.servlet.ServletException; |
| 49 | + import javax.servlet.annotation.WebServlet; |
| 50 | + import javax.servlet.http.HttpServlet; |
| 51 | + import javax.servlet.http.HttpServletRequest; |
| 52 | + import javax.servlet.http.HttpServletResponse; |
| 53 | + import java.io.IOException; |
| 54 | + |
| 55 | + @WebServlet("/") //.jsp可以直接访问 |
| 56 | + public class HelloServlet extends HttpServlet { |
| 57 | + protected void doPost(HttpServletRequest request, |
| 58 | + HttpServletResponse response) |
| 59 | + throws ServletException, IOException { |
| 60 | + doGet(request,response); |
| 61 | + } |
| 62 | + |
| 63 | + protected void doGet(HttpServletRequest request, |
| 64 | + HttpServletResponse response) |
| 65 | + throws ServletException, IOException { |
| 66 | + System.out.println("=============="); |
| 67 | + } |
| 68 | + } |
| 69 | + ``` |
70 | 70 | ### 二、WEB-INF下页面测试
|
71 | 71 |
|
72 | 72 | * 1、导入jar包(同上)
|
73 | 73 |
|
74 | 74 | * 2、webapp/WEB-INF/hello.jsp
|
75 |
| -``` |
76 |
| -<%-- |
77 |
| - Created by IntelliJ IDEA. |
78 |
| - User: sunny |
79 |
| - Date: 2018/8/12 |
80 |
| - Time: 14:36 |
81 |
| - To change this template use File | Settings | File Templates. |
82 |
| ---%> |
83 |
| -<%@ page contentType="text/html;charset=UTF-8" language="java" %> |
84 |
| -<html> |
85 |
| -<head> |
86 |
| - <title>Title</title> |
87 |
| -</head> |
88 |
| -<body> |
89 |
| -<h3>Hello world WEB-INF!</h3> |
90 |
| -</body> |
91 |
| -</html> |
92 |
| -```jsp |
| 75 | + ```jsp |
| 76 | + <%-- |
| 77 | + Created by IntelliJ IDEA. |
| 78 | + User: sunny |
| 79 | + Date: 2018/8/12 |
| 80 | + Time: 14:36 |
| 81 | + To change this template use File | Settings | File Templates. |
| 82 | + --%> |
| 83 | + <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
| 84 | + <html> |
| 85 | + <head> |
| 86 | + <title>Title</title> |
| 87 | + </head> |
| 88 | + <body> |
| 89 | + <h3>Hello world WEB-INF!</h3> |
| 90 | + </body> |
| 91 | + </html> |
| 92 | + ``` |
93 | 93 | * 3、HelloServlet.java
|
94 |
| -```java |
95 |
| -package com.edu.servlet; |
96 |
| -
|
97 |
| -import javax.servlet.ServletException; |
98 |
| -import javax.servlet.annotation.WebServlet; |
99 |
| -import javax.servlet.http.HttpServlet; |
100 |
| -import javax.servlet.http.HttpServletRequest; |
101 |
| -import javax.servlet.http.HttpServletResponse; |
102 |
| -import java.io.IOException; |
103 |
| -
|
104 |
| -@WebServlet("/*") //所有请求(无论在根目录后加任何请求)全跳转到webapp/WEB-INF/hello.jsp; |
105 |
| -public class HelloServlet extends HttpServlet { |
106 |
| - protected void doPost(HttpServletRequest request, |
107 |
| - HttpServletResponse response) |
108 |
| - throws ServletException, IOException { |
109 |
| - doGet(request,response); |
110 |
| - } |
111 |
| -
|
112 |
| - protected void doGet(HttpServletRequest request, |
113 |
| - HttpServletResponse response) |
114 |
| - throws ServletException, IOException { |
115 |
| - System.out.println("=============="); |
116 |
| - request.getRequestDispatcher("/WEB-INF/hello.jsp").forward(request,response); |
117 |
| - } |
118 |
| -} |
119 |
| -``` |
| 94 | + ```java |
| 95 | + package com.edu.servlet; |
| 96 | + |
| 97 | + import javax.servlet.ServletException; |
| 98 | + import javax.servlet.annotation.WebServlet; |
| 99 | + import javax.servlet.http.HttpServlet; |
| 100 | + import javax.servlet.http.HttpServletRequest; |
| 101 | + import javax.servlet.http.HttpServletResponse; |
| 102 | + import java.io.IOException; |
| 103 | + |
| 104 | + @WebServlet("/*") //所有请求(无论在根目录后加任何请求)全跳转到webapp/WEB-INF/hello.jsp; |
| 105 | + public class HelloServlet extends HttpServlet { |
| 106 | + protected void doPost(HttpServletRequest request, |
| 107 | + HttpServletResponse response) |
| 108 | + throws ServletException, IOException { |
| 109 | + doGet(request,response); |
| 110 | + } |
| 111 | + |
| 112 | + protected void doGet(HttpServletRequest request, |
| 113 | + HttpServletResponse response) |
| 114 | + throws ServletException, IOException { |
| 115 | + System.out.println("=============="); |
| 116 | + request.getRequestDispatcher("/WEB-INF/hello.jsp").forward(request,response); |
| 117 | + } |
| 118 | + } |
| 119 | + ``` |
120 | 120 |
|
121 | 121 |
|
122 | 122 |
|
|
0 commit comments