Java Request详解
request对象和respose对象的原理
request对象
request对象和response对象都是由服务器创建的,我们是来使用他们
request对象是用来获取请求消息的,response对象是用来设置响应消息的
本篇文章我们先来讲解request
request的继承体系
request的功能
1. 获取请求消息数据
1. 获取请求行数据
-
GET /day14/demo1?name=zhangsan HTTP/1.1
-
方法:
1. 获取请求方式 :GET
* String getMethod()
2. (*)获取虚拟目录:/day14
* String getContextPath()
3. 获取Servlet路径: /demo1
* String getServletPath()
4. 获取get方式请求参数:name=zhangsan
* String getQueryString()
5. (*)获取请求URI:/day14/demo1
* String getRequestURI(): /day14/demo1
* 6.获取url
* StringBuffer getRequestURL() :http://localhost/day14/demo1* URL:统一资源定位符 : http://localhost/day14/demo1 中华人民共和国 * URI:统一资源标识符 : /day14/demo1 共和国 **6. 获取协议及版本**:HTTP/1.1 * String getProtocol() **7. 获取客户机的IP地址**: * String getRemoteAddr() **2. 获取请求头数据** * **方法**: * (*)String getHeader(String name):通过请求头的名称获取请求头的值 * Enumeration<String> getHeaderNames():获取所有的请求头名称 **3. 获取请求体数据**: * 请求体:只有POST请求方式,才有请求体,在请求体中封装了POST请求的请求参数 * 步骤: **1. 获取流对象** * BufferedReader getReader():获取字符输入流,只能操作字符数据 * ServletInputStream getInputStream():获取字节输入流,可以操作所有类型数据 * 在文件上传知识点后讲解 **2. 再从流对象中拿数据** **2. 其他功能**: **1. 获取请求参数通用方式**:不论get还是post请求方式都可以使用下列方法来获取请求参数 1. String getParameter(String name):根据参数名称获取参数值 username=zs&password=123 2. String[] getParameterValues(String name):根据参数名称获取参数值的数组 hobby=xx&hobby=game 3. Enumeration<String> getParameterNames():获取所有请求的参数名称 4. Map<String,String[]> getParameterMap():获取所有参数的map集合 * **中文乱码问题**: * **get方式**:tomcat 8 已经将get方式乱码问题解决了 * **post方式**:会乱码 * **解决**:在获取参数前,设置request的编码request.setCharacterEncoding("utf-8");
请求转发
什么是请求转发?
请求转发是服务器内部资源跳转的一种方式
步骤
- 通过request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)
- 使用RequestDispatcher对象来进行转发:forward(ServletRequest request, ServletResponse response)
2. 特点:
- 浏览器地址栏路径不发生变化
- 只能转发到当前服务器内部资源中。
- 转发是一次请求
请求转发图解:
代码
这个案例是以index.jsp页面向Servletimp请求,然后Servletimp跳转到Bservlet
Servletimp
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;
@WebServlet("/Servletimp")
public class Servletimp extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/Bservlet");
requestDispatcher.forward(req,resp);
System.out.println("Servletimp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req,resp);
}
}
Bservlet
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("/Bservlet")
public class Bservlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("Bservlet");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
<%--
Created by IntelliJ IDEA.
User: 86151
Date: 2020/7/29
Time: 11:00
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<FORM action="/Servletimp" method="post">
<input name="username">
<input type="submit" value="提交">
</FORM>
</body>
</html>
共享数据
我们先来了解什么是域对象?
域对象:一个有作用范围的对象,可以在范围内共享数据
request:代表一次请求的范围,一般用于请求转发中多个资源的共享数据
方法
- void setAttribute(String name,Object obj):存储数据
- Object getAttitude(String name):通过键获取值
- void removeAttribute(String name):通过键移除键值对
获取ServletContext
ServletContext getServletContext
简单的代码实现
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;
@WebServlet("/Servletimp")
public class Servletimp extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/Bservlet");
req.setAttribute("id",1);//存入键名为id值为1的数据
requestDispatcher.forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
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("/Bservlet")
public class Bservlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
Object id = req.getAttribute("id");//获取键名为id的键值
System.out.println(id);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}