JavaWeb14.1【servlet&http&request:Servlet的体系结构、相关配置】

 

 

 

 

 

 

 

 

 1 package com.haifei.web.servlet;
 2 
 3 import javax.servlet.*;
 4 import javax.servlet.annotation.WebServlet;
 5 import java.io.IOException;
 6 
 7 
 8 //@WebServlet("./demo1") //项目启动是会报错
 9 @WebServlet("/demo1") //千万不要写成"./demo1"
10 public class ServletDemo1 implements Servlet {
11 
12 
13     @Override
14     public void init(ServletConfig servletConfig) throws ServletException {
15 
16     }
17 
18     @Override
19     public ServletConfig getServletConfig() {
20         return null;
21     }
22 
23     @Override
24     public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
25         System.out.println("demo1....");
26         //http://localhost/demo1
27     }
28 
29     @Override
30     public String getServletInfo() {
31         return null;
32     }
33 
34     @Override
35     public void destroy() {
36 
37     }
38 }
 1 package com.haifei.web.servlet;
 2 
 3 import javax.servlet.GenericServlet;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.ServletRequest;
 6 import javax.servlet.ServletResponse;
 7 import javax.servlet.annotation.WebServlet;
 8 import java.io.IOException;
 9 
10 @WebServlet("/demo2")
11 public class ServletDemo2 extends GenericServlet {
12     @Override
13     public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
14         System.out.println("demo2....");
15         //http://localhost/demo2
16     }
17 }
 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("/demo3")
11 public class ServletDemo3 extends HttpServlet {
12     @Override
13     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
14 //        super.doGet(req, resp);
15         System.out.println("doGet.....");
16         /*
17         浏览器直接请求是doGet方式
18 
19         浏览器地址栏http://localhost/demo3
20         控制台输出doGet.....
21          */
22     }
23 
24     @Override
25     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
26 //        super.doPost(req, resp);
27         System.out.println("doPost.....");
28         /*
29         截止目前学到的知识,只能通过表单实现doPost方式
30 
31         http://localhost/login.html
32         随便输入,点击提交,自动跳转到http://localhost/demo3
33         控制台输出doPost.....
34          */
35     }
36 }
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6 </head>
 7 <body>
 8     <form action="/demo3" method="post">
 9         <input name="username">
10         <input type="submit" value="提交">
11     </form>
12 </body>
13 </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 /**
11  * Servlet路径配置方式
12  */
13 //@WebServlet({"/d4","/dd4","/ddd4"})
14 //@WebServlet("/user/demo4")
15 //@WebServlet("/user/*") //*是通配符,写啥都行
16 //@WebServlet("/*")
17 @WebServlet("*.do") //注意这种方式前面不要加/,并且do写其他词也行,随便,比如.action等
18 public class ServletDemo4 extends HttpServlet {
19     @Override
20     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
21         System.out.println("demo4 .... doGet");
22         /*
23         浏览器地址栏访问
24         http://localhost/d4
25 26         http://localhost/dd4
27 28         http://localhost/ddd4
29         均可令控制台输出demo4 .... doGet
30          */
31 
32         //http://localhost/user/demo4
33 
34         /*
35         http://localhost/user/
36         http://localhost/user/hehe
37         http://localhost/user/heihei
38         http://localhost/user/haha
39         ...
40          */
41 
42         /*
43         http://localhost/
44         http://localhost/qdu
45         http://localhost/fuck
46         ...
47          */
48         /*
49         疑问:例如ServletDemo3类中设置了@WebServlet("/demo3")
50               此时ServletDemo4类中设置了@WebServlet("/*")
51               当浏览器访问http://localhost/demo3时会怎样
52        答案:doGet.....
53        解析:通配符*的优先级最低,即其他方式都访问不到时才会利用*访问
54          */
55 
56         /*
57         http://localhost/.do
58         http://localhost/demo4.do
59         http://localhost/xxx.do
60         ....
61          */
62     }
63 }

 

 

 

 

 

 

 

tomcat8对应最大版本jdk1.8
tomcat7对应最大版本jdk1.7

posted @ 2021-06-29 20:08  yub4by  阅读(45)  评论(0编辑  收藏  举报