JavaWeb14.4【servlet&http&request:Request的其他功能】

 

 

 

 

 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/RequestDemo4" method="post">
10         <input type="text" placeholder="请输入用户名" name="username"><br>
11         <input type="text" placeholder="请输入密码" name="password"><br>
12         <input type="checkbox" name="hobby" value="game">游戏
13         <input type="checkbox" name="hobby" value="study">学习
14         <br>
15         <input type="submit" value="注册">
16     </form>
17 
18 </body>
19 </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.IOException;
 9 import java.util.Enumeration;
10 import java.util.Map;
11 import java.util.Set;
12 
13 /**
14  * request的其他功能 之 获取请求参数通用方式
15  */
16 @WebServlet("/RequestDemo4")
17 public class RequestDemo4 extends HttpServlet {
18     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
19         //post 获取请求参数
20 
21         //1 根据参数名称获取参数值
22         String username = request.getParameter("username");
23         /*System.out.println("post");
24         System.out.println(username);*/
25         /*
26         <form action="/day14/RequestDemo4" method="post">
27         http://localhost:8080/day14/regist2.html
28 
29         http://localhost:8080/day14/RequestDemo4
30 
31         post
32         yubaby
33          */
34 
35         //2 根据参数名称获取参数值的数组
36         String[] hobbies = request.getParameterValues("hobby"); //获取勾选的复选框的值
37         /*for (String hobby: hobbies){
38             System.out.println(hobby);
39         }*/
40 
41         //3 获取所有请求的参数名称
42         Enumeration<String> parameterNames = request.getParameterNames();
43         /*while (parameterNames.hasMoreElements()){
44             String name = parameterNames.nextElement();
45             System.out.println(name);
46             String value = request.getParameter(name);
47             System.out.println(value);
48             System.out.println("-------------------");
49         }*/
50 
51         //4 获取所有参数的map集合
52         Map<String, String[]> parameterMap = request.getParameterMap();
53         Set<String> keySet = parameterMap.keySet();
54         for (String name : keySet) { //iter+回车  快速生成foreach
55             System.out.println(name);
56             String[] values = parameterMap.get(name);
57             for (String value : values) {
58                 System.out.println(value);
59             }
60             System.out.println("======================");
61         }
62     }
63 
64     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
65 
66         /*//get 获取请求参数
67 
68         //1 根据参数名称获取参数值
69         String username = request.getParameter("username");
70         System.out.println("get");
71         System.out.println(username);*/
72 
73         /*
74         <form action="/day14/RequestDemo4" method="get">
75         http://localhost:8080/day14/regist2.html
76 
77         http://localhost:8080/day14/RequestDemo4?username=haifei&password=222
78 
79         get
80         haifei
81          */
82 
83         //PS 既然在获取请求参数这方面,doGet和doPost中的代码通用,那么:
84         this.doPost(request, response);
85     }
86 }

 

 

 

 

 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/RequestDemo5" method="post">
10         <input type="text" placeholder="请输入用户名" name="username"><br>
11         <input type="text" placeholder="请输入密码" name="password"><br>
12         <input type="checkbox" name="hobby" value="game">游戏
13         <input type="checkbox" name="hobby" value="study">学习
14         <br>
15         <input type="submit" value="注册">
16     </form>
17 
18 </body>
19 </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.IOException;
 9 
10 /**
11  * request的其他功能 之 获取请求参数通用方式
12  * 中文乱码问题
13  */
14 @WebServlet("/RequestDemo5")
15 public class RequestDemo5 extends HttpServlet {
16     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
17         //设置流的编码 解决post中文乱码问题
18         request.setCharacterEncoding("utf-8");
19         String username = request.getParameter("username");
20         System.out.println(username);
21     }
22 
23     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
24         //tomcat8及后已经将get方式乱码问题解决了
25         this.doPost(request, response);
26     }
27 }
 1 package com.haifei.web.request;
 2 
 3 import javax.servlet.RequestDispatcher;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.annotation.WebServlet;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 import java.io.IOException;
10 
11 /**
12  * request的其他功能 之 请求转发 和 数据共享
13  */
14 @WebServlet("/RequestDemo6")
15 public class RequestDemo6 extends HttpServlet {
16     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
17         System.out.println("RequestDemo6被访问了。。。");
18 
19         //转发之前存储数据到request域中
20         request.setAttribute("msg", "hello");  //键值对
21 
22         // 转发资源 到RequestDemo7
23         /*RequestDispatcher requestDispatcher = request.getRequestDispatcher("/RequestDemo7");
24         requestDispatcher.forward(request, response);*/
25         request.getRequestDispatcher("/RequestDemo7").forward(request, response);
26         //request.getRequestDispatcher("http://www.itcast.cn").forward(request,response); //转发失败404 只能转发到当前服务器内部资源中
27 
28         /*
29         http://localhost:8080/day14/RequestDemo6
30 
31         RequestDemo6被访问了。。。
32         RequestDemo7被访问了。。。
33          */
34     }
35 
36     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
37         this.doPost(request, response);
38     }
39 }
 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 @WebServlet("/RequestDemo7")
14 public class RequestDemo7 extends HttpServlet {
15     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
16         System.out.println("RequestDemo7被访问了。。。");
17 
18         //获取来自RequestDemo6的数据
19         Object msg = request.getAttribute("msg");
20         System.out.println(msg); //hello
21     }
22 
23     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
24         this.doPost(request, response);
25     }
26 }
 1 package com.haifei.web.request;
 2 
 3 import javax.servlet.ServletContext;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.annotation.WebServlet;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 import java.io.IOException;
10 
11 /**
12  * request的其他功能 之 获取ServletContext
13  */
14 @WebServlet("/RequestDemo8")
15 public class RequestDemo8 extends HttpServlet {
16     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
17         ServletContext servletContext = request.getServletContext();
18         System.out.println(servletContext); //org.apache.catalina.core.ApplicationContextFacade@666293eb
19     }
20 
21     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
22         this.doPost(request, response);
23     }
24 }

 

posted @ 2021-06-30 13:29  yub4by  阅读(37)  评论(0编辑  收藏  举报