Request
获取请求参数通用方式
1
String getParameter(String name)
根据参数名称获取参数值
1
String[] getParameterValues(String name)
根据参数名称获取参数值的数组
hobby=xx&hobby=game,多用于复选框1
Enumeration<String> getParameterNames()
获取所有请求的参数名称
1
Map<String,String[]> getParameterMap()
获取所有请求的键值对集合
演示
getParameter
创建一个regist2.html1
2
3
4
5
6
7
8
9
10
11
12
13
14
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/hello/request6" method="post">
<input type="text" placeholder="请输入用户名" name="username"><br>
<input type="password" placeholder="请输入密码" name="password"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
再创建RequestDemo61
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29package Request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
"RequestDemo6",urlPatterns = "/request6") (name =
public class RequestDemo6 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//post获取请求参数
//根据参数名称获取参数值
String username = request.getParameter("username");
System.out.println("post");
System.out.println("username");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//get获取请求参数
String username = request.getParameter("username");
System.out.println("get");
System.out.println("username");
}
}
首先在html的method为POST请求时,测试一下,打开提交表单,得到反馈
运行结果1
2post
username
再把method改为GET请求
运行结果1
2get
username
这样就可以看到getParameter
是通用的操作
无论是get还是post都可以使用
这种写法其实比较麻烦,所以可以将RequestDemon6改为1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30package Request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
"RequestDemo6",urlPatterns = "/request6") (name =
public class RequestDemo6 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//post获取请求参数
//根据参数名称获取参数值
String username = request.getParameter("username");
System.out.println("post");
System.out.println("username");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//get获取请求参数
// String username = request.getParameter("username");
// System.out.println("get");
// System.out.println("username");
this.doPost(request,response);
}
}
getParameterValues
刚才提到这个适用于复选框
修改regist2.html1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/hello/request6" method="get">
<input type="text" placeholder="请输入用户名" name="username"><br>
<input type="password" placeholder="请输入密码" name="password"><br>
<input type="checkbox" name="hobby" value="game">游戏
<input type="checkbox" name="hobby" value="study">学习
<br>
<input type="submit" value="注册">
</form>
</body>
</html>
同时修改Servlet1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36package Request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
"RequestDemo6",urlPatterns = "/request6") (name =
public class RequestDemo6 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//post获取请求参数
//根据参数名称获取参数值
String username = request.getParameter("username");
// System.out.println("post");
// System.out.println("username");
//根据参数名称获取参数值的数组
String[] hobbies = request.getParameterValues("hobby");
for(String hobby:hobbies){
System.out.println(hobby);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//get获取请求参数
// String username = request.getParameter("username");
// System.out.println("get");
// System.out.println("username");
this.doPost(request,response);
}
}
再次访问regist2.html,勾选游戏和学习,点击注册
运行结果1
2game
study
getParameterNames
修改Servlet1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51package Request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
@WebServlet(name = "RequestDemo6",urlPatterns = "/request6")
public class RequestDemo6 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//post获取请求参数
//根据参数名称获取参数值
String username = request.getParameter("username");
// System.out.println("post");
// System.out.println("username");
//根据参数名称获取参数值的数组
String[] hobbies = request.getParameterValues("hobby");
// for(String hobby:hobbies){
// System.out.println(hobby);
// }
//获取所有请求的参数名称
Enumeration<String> parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()){
String name = parameterNames.nextElement();
System.out.println(name);
String value = request.getParameter(name);
System.out.println(value);
System.out.println("--------------------------------");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//get获取请求参数
// String username = request.getParameter("username");
// System.out.println("get");
// System.out.println("username");
this.doPost(request,response);
}
}
访问regist2.html
运行结果1
2
3
4
5
6
7
8
9username
51nd0re1
--------------------------------
password
123
--------------------------------
hobby
game
--------------------------------
会发现一个问题,hobby明明勾选了两个但是却只输出了一个,这是因为getParameter是String类型,而不是数组
getParameterMap
修改Servlet1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65package Request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
@WebServlet(name = "RequestDemo6",urlPatterns = "/request6")
public class RequestDemo6 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//post获取请求参数
//根据参数名称获取参数值
String username = request.getParameter("username");
// System.out.println("post");
// System.out.println("username");
//根据参数名称获取参数值的数组
String[] hobbies = request.getParameterValues("hobby");
// for(String hobby:hobbies){
// System.out.println(hobby);
// }
//获取所有请求的参数名称
Enumeration<String> parameterNames = request.getParameterNames();
// while (parameterNames.hasMoreElements()){
// String name = parameterNames.nextElement();
// System.out.println(name);
// String value = request.getParameter(name);
// System.out.println(value);
// System.out.println("--------------------------------");
// }
//获取所有参数的map集合
Map<String,String[]> parameterMap = request.getParameterMap();
//循环
Set<String> keySet = parameterMap.keySet();
for(String name : keySet){
//根据key获取值
String[] values = parameterMap.get(name);
System.out.println(name);
for(String value : values){
System.out.println(value);
}
System.out.println("--------------------");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//get获取请求参数
// String username = request.getParameter("username");
// System.out.println("get");
// System.out.println("username");
this.doPost(request,response);
}
}
运行结果1
2
3
4
5
6
7
8
9
10username
51nd0re1
--------------------
password
123
--------------------
hobby
game
study
--------------------
这里看到已经获取到了game和study,这样就解决了上一个获取不完整的问题
中文乱码问题
get方式 tomcat8已经将中文乱码问题解决了
post方式解决办法
1 | package Request; |
设置为utf-8即可
请求转发
一种在服务器内部的资源跳转方式
通过request对象获取请求转发器对象:
RequestDispatcher getRequestDispatcher(String path)
使用RequestDispatcher对象来进行转发
演示
RequestDemo81
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28package Request;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
"RequestDemo8",urlPatterns = "/request8") (name =
public class RequestDemo8 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo8被访问了----");
//转发到demo9资源
// RequestDispatcher requestDispatcher = request.getRequestDispatcher("/request9");
// requestDispatcher.forward(request, response);
request.getRequestDispatcher("/request9").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
RequestDemo91
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22package Request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
"RequestDemo9",urlPatterns = "/request9") (name =
public class RequestDemo9 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo9被访问了----");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
访问request8
运行结果1
2demo8被访问了----
demo9被访问了----
特点
- 浏览器地址栏路径不发生变化
- 只能转发到当前的服务器内部资源中
- 转发是一次请求
共享数据
域对象:一个有作用范围的对象,可以在范围内共享数据
request域:代表一次请求的范围,一般用于请求转发的多个资源中共享数据
方法:
setAttribute(String name,Object obj):存储数据
Object getAttitude(String name):通过键获取值
removeAttribute(String name):通过键移除键值对
演示
修改request81
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32package Request;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
"RequestDemo8",urlPatterns = "/request8") (name =
public class RequestDemo8 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo8被访问了----");
//转发到demo9资源
// RequestDispatcher requestDispatcher = request.getRequestDispatcher("/request9");
// requestDispatcher.forward(request, response);
//存储数据到request域中
request.setAttribute("msg","hello");
request.getRequestDispatcher("/request9").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
在转发之前将数据存储到request域中
修改request91
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28package Request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
"RequestDemo9",urlPatterns = "/request9") (name =
public class RequestDemo9 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取数据
Object msg = request.getAttribute("msg");
System.out.println(msg);
System.out.println("demo9被访问了----");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
访问request8
运行结果1
2
3demo8被访问了----
hello
demo9被访问了----
这里可以知道,只有在转发的情况下才可以进行数据共享
获取ServletContext
1 | ServletContext getServletContext() |
演示1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25package Request;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
"RequestDemo10",urlPatterns = "/request10") (name =
public class RequestDemo10 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = request.getServletContext();
System.out.println(servletContext);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
运行结果1
org.apache.catalina.core.ApplicationContextFacade@672c0e6b