Request&Response
Request(请求)
Request继承体系
注:
1.Tomcat需要解析请求数据,封装为request对象,并且创建request对象传递到service方法中。
2.使用request对象,查阅javaEE API文档的 HttpServletRequest接口
Request获取请求数据(核心功能)
获取请求数据
1.请求行:
GET /request-demo/req1?username=zhangsan HTTP/2
- String getMethod():获取请求方式:GET
- Strintg getContextPath():获取虚拟目录(项目访问路径):/request-demo
- StringBuffer get RequestURL():获取URL(统一资源定位符):http://local:8080/request-demo/req1
- String get RequestURI():获取URL(统一资源标识符):/request-demo/req1
- String getQueryString():获取请求参数(GET方式):username=zhangsan&password=123
2.请求头
User-Agent:Mozilla/5.0 Chrome/91.0.4472.106
- String getHeader(String name):根据请求头名称获取值
3.请求体
POST请求才有请求体
- ServletinputStream getinputStream();获取字节输入流
- BufferedReader getReader():获取字符输入流
通用方式获取请求参数
思考:
get请求方式与post请求方式区别在于获取请求参数的方式不一样,是否可以提供一种统一获取请求参数的方式,从而统一doGet与doPost方法内的代码?
Request通用方式获取请求参数
- Map<String,String[]> getParameterMap():获取所有参数map集合
- String[] getParameterValues(String name):根据名称获取参数值(数组)
- String getParameter(String name):根据名称获取参数值(单个)
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.Map;
@WebServlet("/req2")
public class RequestDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("get.....");
Map<String, String[]> map = req.getParameterMap();
for (String key : map.keySet()) {
System.out.print(key+":");
String[] values = map.get(key);
for (String value : values) {
System.out.print(value+" ");
}
System.out.println();
}
String[] hobbies = req.getParameterValues("hobby");
for (String hobby : hobbies) {
System.out.println(hobby);
}
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println(username );
System.out.println(password );
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
Request请求转发
- forward:请求转发,一种在服务器内部的资源跳转方式
-
实现方式: req.getRequestDispatcher("资源B路径").forward(req,resp);
-
请求转发资源间共享数据:使用Request对象 void setAttribute(String name,Object o):存储数据到request域中
Object getAttribute(String name): 根据key,获取值
void removeAttribute(String name): 根据key删除键值对
-
例:
@WebServlet("/req5")
public class requestDemo05 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo5...");
request.setAttribute("ityc","yc");
request.removeAttribute("ityc");
request.getRequestDispatcher("/req6").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
@WebServlet("/req6")
public class requestDemo06 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo6...");
Object ityc = request.getAttribute("ityc");
System.out.println(ityc);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
结果:
- 请求转发特点:
- 浏览器地址栏不发生改变
- 只能转发到当前服务器
- 一次请求可以在转发的资源间使用request共享数据
Response(响应)
一:Response设置响应数据功能介绍
-
响应数据分为3部分:
-
响应行: HTTP/2 200 OK
void setStatus(int sc):设置响应状态码
-
响应头 content-type: text/html
void SetHeader(String name,String value):设置响应头键值对
-
响应体 <html><head></head><body></body></html>
PrintWriter getWriter():获取字符输出流
ServletOutputStream getOutputStream():获取字节输出流
二:Response完成重定向
-
实现方式: resp.setStatus(302);
resp.setHeader("location","资源B的路径");
-
联系 @WebServlet("/resp1")
public class ResponseDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("resp1.....");
response.sendRedirect("/Request-Tomcat/resp2");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
@WebServlet("/resp2")
public class ResponseDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("sesp2....");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
-
重定向特点:
- 浏览器地址栏路径发生变化。
- 可以重定向到任意位置。服务器内部,外部均可。
- 两次请求,不能在多个资源使用request共享数据。
三:Response响应字符数据
四:Rsponse响应字节数据
-
使用: 1.通过Response对象获取字符输出流
ServletOutputStream outputStream = resp.getOutputStream();
2.写数据
outputStream.write(字节数据);
-
例:将一张图片通过流,响应至浏览器 -
@WebServlet("/resp4")
public class ResponseDemo4 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
FileInputStream fileInputStream = new FileInputStream("D://a.jpg");
ServletOutputStream outputStream = response.getOutputStream();
IOUtils.copy(fileInputStream,outputStream);
fileInputStream.close();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
|