一、request
由于request我们在前面已经介绍过这边就不再继续介绍了,并且request和response也比较相像所以下面就直接贴代码了。
1.获取请求行
/*******************获取请求行*******************/
//1.获取请求方式 get post
String method = request.getMethod();
System.out.println(method);
//2.获取请求资源 URL URI
String requestURI = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
System.out.println(requestURL);
System.out.println(requestURI);
//3.获取get请求参数
String queryString = request.getQueryString();
System.out.println(queryString);
//4.获取web应用名称
String contextPath = request.getContextPath();
System.out.println(contextPath);
2.获取请求头
/*****************获取请求头**********************/
/*
* //1.获取所有请求头名称
* Enumeration<String> headerNames = request.getHeaderNames();
* while(headerNames.hasMoreElements()) {
* System.out.println(headerNames.nextElement()); }
*
* //2.根据名称获取指定名称头信息
* String host = request.getHeader("host");
* System.out.println(host);
*/
//3.打印所有的请求头和请求头内容
Enumeration<String> headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
String header = request.getHeader(headerName);
System.out.println(headerName+": "+header);
}
3.获取请求体
/*****************获取请求体**********************/
/*
* //1.获取一个值 String gender = request.getParameter("gender");
* System.out.println(gender); //2.获取多个值 String[] values =
* request.getParameterValues("hobby"); System.out.println(values.toString());
* //3.获取所有请求参数名称 Enumeration<String> names = request.getParameterNames();
* while(names.hasMoreElements()) { String name = names.nextElement();
* System.out.println(name); }
*/
//4.获取所有的请求参数
Map<String, String[]> parameterMap = request.getParameterMap();
for (Map.Entry<String, String[]> entry: parameterMap.entrySet()) {
System.out.println(entry.getKey());
System.out.println(Arrays.toString(entry.getValue()));
}
User u = new User();
try {
BeanUtils.populate(u, parameterMap);
System.out.println(u);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
获取请求体我们用到了一个BeanUtil小工具,需要额外配置jar包,BeanUtil可以很方便的把从request中接收到的所有参数封装进我们实现写好的javabean中。如下面一个例子:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/Request/RequestServlet">
<input type="text" placeholder="请输入.." name="userName"><br />
<input type="text" placeholder="请输入.." name="pwd"><br />
性别:
<input type="radio" name="gender" value="man">男
<input type="radio" name="gender" value="female">女<br/>
爱好:
<input type="checkbox" value="cy" name="hobby">抽烟
<input type="checkbox" value="hj" name="hobby">喝酒
<input type="checkbox" value="tt" name="hobby">抽烟
<input type="submit" value="提交">
</form>
</body>
</html>
相应的javabean为
public class User {
String gender;
String userName;
String pwd;
String[] hobby;
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "User [gender=" + gender + ", userName=" + userName + ", pwd=" + pwd + ", hobby="
+ Arrays.toString(hobby) + "]";
}
}
二、请求转发
我们之前提到过重定向问题,重定向是指浏览器发来的请求先找到servlet1,在servlet1中通过设置响应告诉浏览器, 再让浏览器发送请求到servlet2。总共发送了两次请求并且地址栏会发生变化。而请求转发不一样,请求转发只需要发送一次请求找servlet1,然后在servlet1当中直接转发给servlet2,不要再告诉浏览器。只发送一次请求浏览器地址栏当中还是servlet1,不会发生变化。
//获取转发器
RequestDispatcher dispatcher = request.getRequestDispatcher("/Servlet2");
//转发
dispatcher.forward(request, response);
request域对象
再一次请求过程当中request是可以共享数据的。什么是一次请求过程?我们上文提到请求转发中浏览器请求到servlet1而servlet1在转发给servlet2这个过程就叫一次转发过程。其中servlet1是可以通过request向servlet2传递数据的。但是仅仅是将servlet1中数据复制一份给servlet2而已,两个servlet中并不是同一个request对象。
servlet1中的service:
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("lk", "12345");
System.out.println(request);
RequestDispatcher dispatcher = request.getRequestDispatcher("/Servlet2");
dispatcher.forward(request, response);
}
servlet2中的service:
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("servlet2");
System.out.println(request.getAttribute("lk"));
System.out.println(request);
}
从结果我们可以看到两者并不是同一个对象,他们做的只是数据的传递。