JavaWeb14.3【servlet&http&request:Request概述、Request的获取请求消息数据功能】

 

 

 

 1 package com.haifei.web.request;
 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  * Request对象获取请求行数据
12  */
13 //Tips:IDEA->file->new->servlet直接生成 继承自HttpServlet的Servlet代码,包括doGet、doPost和WebServlet注解等
14 //@WebServlet(name = "RequestDemo1") //默认是name属性,我们用value就行,而在仅有value一个属性时,"value="可以省略
15 @WebServlet("/RequestDemo1") // 添加/
16 public class RequestDemo1 extends HttpServlet {
17     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
18 
19     }
20 
21     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
22 //        1. 获取请求方式 :GET
23         String method = request.getMethod();
24         System.out.println(method);
25 
26 //        2*. 获取虚拟目录:/day14
27         String contextPath = request.getContextPath();
28         System.out.println(contextPath);
29 
30 //        3. 获取Servlet路径: /demo1
31         String servletPath = request.getServletPath();
32         System.out.println(servletPath);
33 
34 //        4. 获取get方式请求参数:name=zhangsan
35         String queryString = request.getQueryString();
36         System.out.println(queryString);
37 
38 //        5*. 获取请求URI:/day14/demo1
39         String requestURI = request.getRequestURI();
40         StringBuffer requestURL = request.getRequestURL();
41         System.out.println(requestURI);
42         System.out.println(requestURL);
43 
44 //        6. 获取协议及版本:HTTP/1.1
45         String protocol = request.getProtocol();
46         System.out.println(protocol);
47 
48 //        7. 获取客户机的IP地址
49         String remoteAddr = request.getRemoteAddr();
50         System.out.println(remoteAddr);
51 
52         /*
53         http://localhost/day14/RequestDemo1
54 
55         GET
56         /day14
57         /RequestDemo1
58         null
59         /day14/RequestDemo1
60         http://localhost/day14/RequestDemo1
61         HTTP/1.1
62         0:0:0:0:0:0:0:1
63          */
64 
65         /*
66         http://localhost/day14/RequestDemo1?name=zhangsan&age=22
67 
68         GET
69         /day14
70         /RequestDemo1
71         name=zhangsan&age=22
72         /day14/RequestDemo1                     URI
73         http://localhost/day14/RequestDemo1     URL     URL<URI
74         HTTP/1.1
75         0:0:0:0:0:0:0:1                         自己访问自己机子默认返回IPV6地址
76          */
77     }
78 }

 

 

  1 package com.haifei.web.request;
  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 import java.util.Enumeration;
 10 
 11 /**
 12  * Request对象获取请求头数据
 13  */
 14 //@WebServlet(name = "RequestDemo2")
 15 @WebServlet("/RequestDemo2")
 16 public class RequestDemo2 extends HttpServlet {
 17     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 18 
 19     }
 20 
 21     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 22         //1 获取所有请求头名称
 23         Enumeration<String> headerNames = request.getHeaderNames();
 24         //2 遍历
 25         /*while (headerNames.hasMoreElements()){
 26             String name = headerNames.nextElement();
 27             String value = request.getHeader(name);
 28             System.out.println(name + ":" + value);
 29         }*/
 30 
 31         /*
 32         http://localhost/day14/RequestDemo2
 33         */
 34 //        host:localhost
 35 //        connection:keep-alive
 36 //        sec-ch-ua:" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"
 37 //        sec-ch-ua-mobile:?0
 38 //        upgrade-insecure-requests:1
 39 //        user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36
 40 //        accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
 41 //        sec-fetch-site:none
 42 //        sec-fetch-mode:navigate
 43 //        sec-fetch-user:?1
 44 //        sec-fetch-dest:document
 45 //        accept-encoding:gzip, deflate, br
 46 //        accept-language:zh-CN,zh;q=0.9,en;q=0.8
 47 //        cookie:JSESSIONID=AFBF93586FBE914A0F1D03CE33F8B4C7; Pycharm-bef28cc3=6a7e18fa-9ae4-419f-9f06-22bb003caf2b; _ga=GA1.1.1972130967.1563781280; _pk_id.100001.1fff=dafa65dff09610aa.1563781279.2.1568286400.1563781584.; __utma=111872281.1972130967.1563781280.1563781280.1568286412.2; Idea-d3ad9769=02eef562-1b85-4098-a324-4ed6e79f0a1f; JSESSIONID=1EB4990AB813A822C03A61EDC19BF84C
 48 
 49 
 50 
 51 
 52         //1 指定获取请求头数据user-agent
 53         String agent = request.getHeader("user-agent");
 54         System.out.println(agent);
 55         //2 判断浏览器的版本-案例
 56         if(agent.contains("Chrome")){
 57             System.out.println("谷歌来了。。。");
 58         }else if (agent.contains("FireFox")){
 59             System.out.println("火狐来了。。。");
 60         }
 61         /*
 62         Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36
 63         谷歌来了。。。
 64          */
 65 
 66 
 67 
 68 
 69         //1 指定获取请求头数据referer
 70         String referer = request.getHeader("referer");
 71         System.out.println(referer);
 72         /*
 73         http://localhost/day14/RequestDemo2
 74         --> null
 75          */
 76 
 77         /*
 78         http://localhost/day14/login.html
 79         点击超链接
 80         --> http://localhost/day14/login.html
 81          */
 82         //2 防盗链-案例
 83         if (referer != null){
 84             if (referer.contains("/day14")){ //正常访问
 85 //                System.out.println("播放电影。。。。");
 86 
 87                 response.setContentType("text/html;charset=utf-8");
 88                 response.getWriter().write("播放电影。。。");
 89             }else { //盗链
 90 //                System.out.println("想看电影吗,来Bili吧");
 91 
 92                 response.setContentType("text/html;charset=utf-8");
 93                 response.getWriter().write("想看电影吗,来Bili");
 94             }
 95         }
 96         /*
 97         http://localhost/day14/login.html
 98         点击超链接
 99         --> 播放电影。。。。
100          */
101 
102         /*
103         新建day14_RequestDemo2_test模块
104         新建盗链页面index.html
105         tomcat服务器配置这两个模块共同启动
106 
107         访问http://localhost:8080/day14/login.html页面并点击超链接
108         页面跳转到http://localhost:8080/day14/RequestDemo2,控制台输出:播放电影。。。。
109 
110         访问http://localhost:8080/daolian_test/index.html页面并点击超链接
111         页面跳转到http://localhost:8080/day14/RequestDemo2,控制台输出:想看电影吗,来Bili吧
112          */
113     }
114 }
 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 
13     <a href="/day14/RequestDemo2">RequestDemo2-获取请求头数据referer</a>
14 
15 </body>
16 </html>

 

 

 

 

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6 </head>
 7 <body>
 8     <a href="http://localhost:8080/day14/RequestDemo2">高清电影</a>  <!--盗链页面-->
 9 </body>
10 </html>

 

 

 

 

 

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6 </head>
 7 <body>
 8 
 9     <form action="/day14/RequestDemo3" method="post">
10         <input type="text" placeholder="请输入用户名" name="username"><br>
11         <input type="text" placeholder="请输入密码" name="password"><br>
12         <input type="submit" value="注册">
13     </form>
14 
15 </body>
16 </html>
 1 package com.haifei.web.request;
 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.BufferedReader;
 9 import java.io.IOException;
10 
11 /**
12  * Request对象获取请求体数据
13  * 注册案例
14  */
15 @WebServlet("/RequestDemo3")
16 public class RequestDemo3 extends HttpServlet {
17     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
18         //只有POST请求方式,才有请求体,在请求体中封装了POST请求的请求参数
19 
20         //1 获取字符流
21         BufferedReader br = request.getReader();
22         //2 读取数据
23         String line = null;
24         while ((line=br.readLine()) != null){
25             System.out.println(line);
26         }
27 
28         /*
29         http://localhost:8080/day14/regist.html
30         输入zhangsan + 123 点击注册按钮提交表单
31 
32         跳转到http://localhost:8080/day14/RequestDemo3
33         控制台输出:username=zhangsan&password=123
34          */
35     }
36 
37     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
38 
39     }
40 }

 

 

 

posted @ 2021-06-29 21:31  yub4by  阅读(74)  评论(0编辑  收藏  举报