Request

获取请求参数通用方式

  1. 1
    String getParameter(String name)

    根据参数名称获取参数值

  2. 1
    String[] getParameterValues(String name)

    根据参数名称获取参数值的数组
    hobby=xx&hobby=game,多用于复选框

  3. 1
    Enumeration<String> getParameterNames()

    获取所有请求的参数名称

  4. 1
    Map<String,String[]> getParameterMap()

    获取所有请求的键值对集合

演示

getParameter

创建一个regist2.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
<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>

再创建RequestDemo6
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
package 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;

@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");
}

@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");
}
}

首先在html的method为POST请求时,测试一下,打开提交表单,得到反馈
运行结果

1
2
post
username

再把method改为GET请求
运行结果
1
2
get
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
30
package 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;

@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");
}

@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);
}
}

getParameterValues

刚才提到这个适用于复选框

修改regist2.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html>
<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>

同时修改Servlet

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
30
31
32
33
34
35
36
package 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;

@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);
}
}

@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
game
study

getParameterNames

修改Servlet

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package 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
9
username
51nd0re1
--------------------------------
password
123
--------------------------------
hobby
game
--------------------------------

会发现一个问题,hobby明明勾选了两个但是却只输出了一个,这是因为getParameter是String类型,而不是数组

getParameterMap

修改Servlet

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
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
65
package 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
10
username
51nd0re1
--------------------
password
123
--------------------
hobby
game
study
--------------------

这里看到已经获取到了game和study,这样就解决了上一个获取不完整的问题

中文乱码问题

get方式 tomcat8已经将中文乱码问题解决了

post方式解决办法

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
package 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 = "RequestDemo7",urlPatterns = "/request7")
public class RequestDemo7 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.设置流的编码
request.setCharacterEncoding("utf-8");

//获取请求参数
String username = request.getParameter("username");

System.out.println(username);
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}

设置为utf-8即可

请求转发

一种在服务器内部的资源跳转方式

  1. 通过request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)

  2. 使用RequestDispatcher对象来进行转发

演示

RequestDemo8

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
package 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.*;

@WebServlet(name = "RequestDemo8",urlPatterns = "/request8")
public class RequestDemo8 extends HttpServlet {
@Override
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);
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}

RequestDemo9

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package 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 = "RequestDemo9",urlPatterns = "/request9")
public class RequestDemo9 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo9被访问了----");
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}

访问request8
运行结果

1
2
demo8被访问了----
demo9被访问了----

特点

  1. 浏览器地址栏路径不发生变化
  2. 只能转发到当前的服务器内部资源中
  3. 转发是一次请求

共享数据

  • 域对象:一个有作用范围的对象,可以在范围内共享数据

  • request域:代表一次请求的范围,一般用于请求转发的多个资源中共享数据

方法:

  1. setAttribute(String name,Object obj):存储数据

  2. Object getAttitude(String name):通过键获取值

  3. removeAttribute(String name):通过键移除键值对

演示

修改request8

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
30
31
32
package 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.*;

@WebServlet(name = "RequestDemo8",urlPatterns = "/request8")
public class RequestDemo8 extends HttpServlet {
@Override
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);
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}

在转发之前将数据存储到request域中

修改request9

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
package 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 = "RequestDemo9",urlPatterns = "/request9")
public class RequestDemo9 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//获取数据
Object msg = request.getAttribute("msg");
System.out.println(msg);


System.out.println("demo9被访问了----");
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}

访问request8
运行结果

1
2
3
demo8被访问了----
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
25
package 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.*;

@WebServlet(name = "RequestDemo10",urlPatterns = "/request10")
public class RequestDemo10 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = request.getServletContext();

System.out.println(servletContext);
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}

运行结果
1
org.apache.catalina.core.ApplicationContextFacade@672c0e6b