1.简介
request对象和response对象的原理:都是由服务器创建的。
2.Request
1.request对象的继承体系结构
ServletRequest ------- 接口
|继承
HttpServletRequest-------接口
|实现
org.apache.catalina.connector.RequestFacade(Tomcat)
2.1获取请求行数据
@WebServlet("/demo1")
public class ServletDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取请求消息数据
//获取请求方式
String method = req.getMethod();
System.out.println(method);
//获取虚拟目录
String contextPath = req.getContextPath();
System.out.println(contextPath);
//获取Servlet路径
String servletPath = req.getServletPath();
System.out.println(servletPath);
//获取get方式的请求参数
String queryString = req.getQueryString();
System.out.println(queryString);
//获取请求URI
String requestURI = req.getRequestURI();
System.out.println(requestURI);
//获取请求URL
StringBuffer requestURL = req.getRequestURL();
System.out.println(requestURL);
//获取客户端的Ip地址
String remoteAddr = req.getRemoteAddr();
System.out.println(remoteAddr);
}
GET
/demo1
null
/demo1
http://localhost:8080/demo1
0:0:0:0:0:0:0:1
2.2获取请求头数据
String host = req.getHea![![2.png](https://upload-images.jianshu.io/upload_images/14115249-cfca1090bbac0695.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
](https://upload-images.jianshu.io/upload_images/14115249-2ea6b9b283536266.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
er("host");
System.out.println(host);
//获取所有的headers
Enumeration headerNames = req.getHeaderNames();
while(headerNames.hasMoreElements()){
String s = headerNames.nextElement();
String val = req.getHeader(s);
System.out.println(val);
}
2.3获取请求体数据
只有使用post请求方式时,才有请求体。
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
BufferedReader reader = req.getReader();
String str;
while((str = reader.readLine()) != null){
System.out.println(str);
}
//ServletInputStream inputStream = req.getInputStream();
}
测试
{
"name":"张三",
"age":11
}
2.4其他功能:
- 获取请求参数的通用方式:
1.String getParameter(String name)
2.String[] getParameterValues(String name)
3.EnumerationgetParameterNames()
4.MapgetParameterMap() - 请求转发:(服务器内部的资源跳转方式)
- 共享数据
- 获取ServletContext
2.4.1获取请求参数的通用方式
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("name");
System.out.println(name);
String[] names = req.getParameterValues("name");
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}
Enumeration parameterNames = req.getParameterNames();
while(parameterNames.hasMoreElements()){
System.out.println(parameterNames.nextElement());
}
Map parameterMap = req.getParameterMap();
System.out.println(parameterMap);
}
2.4.2请求转发
//通过request对象获取RequestDispatcher对象
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/demo0");
requestDispatcher.forward(req,resp);
2.4.3共享数据
- 域对象:一个有作用范围的对象,可以在范围内共享数据
- request域:代表依次请求的范围,一般用于请求转发的多个资源中共享数据
- 方法: req.setAttribute(key,value);req.getAttribute(key);removeAttribute(key)
2.4.4获取ServletContext对象:
- 概念:代表整个web应用,可以和程序的容器(服务器)来通信
- 功能:
1.获取MIME类型(在互联网通信过程中定义的一种文件数据类型)
格式:大类型/小类型 text/html;image/jpeg
2.域对象:共享数据(由于是所有用户共享,不安全,一般不用此对象存储数据)
3.获取文件的真实(服务器)路径
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = req.getServletContext();
String realPath = servletContext.getRealPath("index.jsp");//获取web目录下
System.out.println(realPath);
//获取WEB-INF目录下
String realPath1 = servletContext.getRealPath("/WEB-INF/web.xml");
System.out.println(realPath1);
//获取src目录下
String realPath2 = servletContext.getRealPath("/WEB-INF/classes/test.txt");
System.out.println(realPath2);
}
3.Response
1.设置响应行(setStatus)
2.设置响应头(setHeader)
3.设置响应体(getWriter(),getOutputStream()
编码问题
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("utf-8");
//告诉浏览器,服务器发送的消息的编码,建议浏览器使用该编码解码。否则浏览器会使用默认的解码。
//resp.setHeader("content-type","text/html;charset=utf-8");
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();
writer.write("hello world 你好 世界");
}
重定向
@WebServlet("/demo1")
public class ServletDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//resp.setStatus(302);
//resp.setHeader("location","/hello.html");
resp.sendRedirect("/hello.html");
}
重定向和转发的区别
重定向:
1.地址栏发生变化
2.可以访问其他服务器的资源
3.两次请求,不能使用request对象来共享数据
转发:
1.地址栏路径不变
2.只能访问当前服务器下的资源
3.一次请求,可以使用request对象来共享资源