请求:获取资源。在BS架构中,就是客户端浏览器向服务器端发出询问
请求对象:就是在项目当中用于发送请求的对象
返回值 | 方法名 | 说明 |
---|---|---|
String | getContextPath() | 获取虚拟目录名称 |
String | getServletPath() | 获取Servlet映射路径 |
String | getRemoteAddr() | 获取访问者ip地址 |
String | getQueryString() | 获取请求的消息数据 |
String | getRequersURI() | 获取统一资源标识符 |
StringBuffer | getRequestURL() | 获取统一资源定位符 |
- package servlet;
-
- 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("/servletDemo01")
- public class ServletDemo01 extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //1.获取虚拟目录名称 getContextPath()
- String contextPath = req.getContextPath();
- System.out.println(contextPath);
- //2.获取Servlet映射路径 getServletPath()
- String servletPath = req.getServletPath();
- System.out.println(servletPath);
- //3.获取访问者ip getRemoteAddr()
- String ip = req.getRemoteAddr();
- System.out.println(ip);
- //4.获取请求消息的数据 getQueryString() url?后边的字符串(查询字符串)
- String queryString = req.getQueryString();
- System.out.println(queryString);
- //5.获取统一资源标识符 getRequestURI() /request/servletDemo01 共和国
- String requestURI = req.getRequestURI();
- System.out.println(requestURI);
- //6.获取统一资源定位符 getRequestURL() http://localhost:8080/request/servletDemo01 中华人民共和国
- StringBuffer requestURL = req.getRequestURL();
- System.out.println(requestURL);
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req,resp);
- }
-
- }
返回值 | 方法名 | 说明 |
---|---|---|
String | getHeader(String name) | 根据请求头名称获取一个值 |
Enumeration | getHeaders(String name) | 根据请求头名称获取多个值 |
Enumeration | getHeaderNames() | 获取所有请求头名称 |
- package servlet;
-
- 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("/servletDemo02")
- public class ServletDemo02 extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //1.根据请求头名称获取一个值
- String connection = req.getHeader("connection");
- System.out.println(connection);
- System.out.println("-------------------");
- //2.根据请求头名称获取多个值
- Enumeration
values = req.getHeaders("accept-encoding"); - while(values.hasMoreElements()) {
- String value = values.nextElement();
- System.out.println(value);
- }
- System.out.println("-------------------");
- //3.获取所有的请求头名称
- Enumeration
names = req.getHeaderNames(); - while(names.hasMoreElements()) {
- String name = names.nextElement();
- String value = req.getHeader(name);
- System.out.println(name + "," + value);
- }
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req,resp);
- }
- }
返回值 | 方法名 | 说明 |
---|---|---|
String | getParameter(String name) | 根据名称获取数据 |
String[] | getParameterValues(String name) | 根据名称获取所有数据 |
Enumeration | getParameterNames() | 获取所有名称 |
Map | getParameterMap() | 获取所有参数的键值对 |
在web目录下创建regist.html文件
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>注册页面title>
- head>
- <body>
- <form action="/request/servletDemo03" method="get" autocomplete="off">
- 姓名:<input type="text" name="username"> <br>
- 密码:<input type="password" name="password"> <br>
- 爱好:<input type="checkbox" name="hobby" value="study">学习
- <input type="checkbox" name="hobby" value="game">游戏 <br>
- <button type="submit">注册button>
- form>
- body>
- html>
- package servlet;
-
- 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;
- import java.util.Map;
-
- /*
- 获取请求参数信息的相关方法
- */
- @WebServlet("/servletDemo03")
- public class ServletDemo03 extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //1.根据名称获取数据 getParameter()
- String username = req.getParameter("username");
- System.out.println(username);
- String password = req.getParameter("password");
- System.out.println(password);
- System.out.println("--------------------");
- //2.根据名称获取所有数据 getParameterValues()
- String[] hobbies = req.getParameterValues("hobby");
- for(String hobby : hobbies) {
- System.out.println(hobby);
- }
- System.out.println("--------------------");
- //3.获取所有名称 getParameterNames()
- Enumeration
names = req.getParameterNames(); - while(names.hasMoreElements()) {
- String name = names.nextElement();
- System.out.println(name);
- }
- System.out.println("--------------------");
- //4.获取所有参数的键值对 getParameterMap()
- Map
map = req.getParameterMap(); - for(String key : map.keySet()) {
- String[] values = map.get(key);
- System.out.print(key + ":");
- for(String value : values) {
- System.out.print(value + " ");
- }
- System.out.println();
- }
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req,resp);
- }
- }
三种封装方式
1.手动封装反射
2.反射封装方式
3.工具类封装方式
新建bean.Student类
- package bean;
-
- import java.util.Arrays;
-
- public class Student {
- private String username;
- private String password;
- private String[] hobby;
-
- public Student() {
- }
-
- public Student(String username, String password, String[] hobby) {
- this.username = username;
- this.password = password;
- this.hobby = hobby;
- }
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- public String[] getHobby() {
- return hobby;
- }
-
- public void setHobby(String[] hobby) {
- this.hobby = hobby;
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "username='" + username + '\'' +
- ", password='" + password + '\'' +
- ", hobby=" + Arrays.toString(hobby) +
- '}';
- }
- }
- package servlet;
- import bean.Student;
-
- 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("/servletDemo04")
- public class ServletDemo04 extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //1.获取所有的数据
- String username = req.getParameter("username");
- String password = req.getParameter("password");
- String[] hobbies = req.getParameterValues("hobby");
- //2.封装学生对象
- Student stu = new Student(username,password,hobbies);
- //3.输出对象
- System.out.println(stu);
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req,resp);
- }
- }
-
修改regist.html
<form action="/request/servletDemo04" method="get" autocomplete="off">
- package servlet;
- import bean.Student;
-
- 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.beans.PropertyDescriptor;
- import java.io.IOException;
- import java.lang.reflect.Method;
- import java.util.Map;
- /*
- 封装对象--反射方式
- */
-
- @WebServlet("/servletDemo05")
- public class ServletDemo05 extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //1.获取所有的数据
- Map
map = req.getParameterMap(); - //2.封装学生对象
- Student stu = new Student();
- //2.1遍历集合
- for(String name : map.keySet()) {
- String[] value = map.get(name);
- try {
- //2.2获取Student对象的属性描述器
- //构造函数的第一个参数决定是哪个属性的描述器。第二个参数是当前属性所属对象的字节码
- PropertyDescriptor pd = new PropertyDescriptor(name,stu.getClass());
- //2.3获取对应的setXxx方法
- Method writeMethod = pd.getWriteMethod();
- //2.4执行方法
- if(value.length > 1) {
- writeMethod.invoke(stu,(Object)value);//如果是多个属性值(爱好),需要转换为object
- }else {
- writeMethod.invoke(stu,value);//通过当前属性描述器的写入方法,将value写入stu对象的这个属性身上
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- //3.输出对象
- System.out.println(stu);
-
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req,resp);
- }
- }
<form action="/request/servletDemo05" method="get" autocomplete="off">
导入jar包
- package servlet;
-
- import bean.Student;
- import org.apache.commons.beanutils.BeanUtils;
-
- 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("/servletDemo06")
- public class ServletDemo06 extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //1.获取所有的数据
- Map
map = req.getParameterMap(); - //2.封装学生对象
- Student stu = new Student();
- try {
- BeanUtils.populate(stu,map);//将map中的数据,封装到stu对象中(就这么一句话就搞定了)
- } catch (Exception e) {
- e.printStackTrace();
- }
- //3.输出对象
- System.out.println(stu);
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req,resp);
- }
- }
<form action="/request/servletDemo06" method="get" autocomplete="off">
返回值 | 方法名 | 说明 |
---|---|---|
BufferedReader | getReader() | 获取字符输入流 |
ServletInputStream | getInputStream() | 获取字节输入流 |
- package servlet;
-
- import javax.servlet.ServletException;
- import javax.servlet.ServletInputStream;
- 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("/servletDemo07")
- public class ServletDemo07 extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //字符流(必须是post方式)
- /*BufferedReader br = req.getReader();
- String line;
- while((line = br.readLine()) != null) {
- System.out.println(line);
- }*/
- //br.close();//这个流是从req对象中获取的,不是自己创建的,所以不需要关闭
-
- //字节流 (应用场景,请求参数中有二进制文件(图片)时)
- ServletInputStream is = req.getInputStream();
- byte[] arr = new byte[1024];
- int len;
- while((len = is.read(arr)) != -1) {
- System.out.println(new String(arr,0,len));
- }
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req,resp);
- }
- }
-
- <form action="/request/servletDemo06" method="post" autocomplete="off">
流方式获取到的中文,会进行编码,所以看到的姓名是编码后的
解决乱码问题
GET 方式
没有乱码问题。在 Tomcat 8版本后已经解决
POST 方式
有乱码问题。可以通过 setCharacterEncoding() 方法来解决
- //设置编码格式
- req.setCharacterEncoding("UTF-8");
请求域(request域):可以在一次请求范围内进行共享数据。
一般用于请求转发的多个资源中共享数据。
返回值 | 方法名 | 说明 |
---|---|---|
void | setAttribute(String name,Object value) | 向请求域对象中储存数据 |
Object | getAttribute(String name) | 通过名称获取请求域对象中的数据 |
void | removeAttribute(String name) | 通过名称移除请求域对象中的数据 |
请求到ServletA,实现的功能,需要用到ServletB,那这时候就可以在ServletA将请求转发到ServletB
那ServletB中可能会用到ServletA中的数据,那这时候就涉及到数据共享了
在这里如果使用应用域来共享数据,就有点浪费了,因为我们只是在这次请求中需要共享数据,不是整个应用
所以就用到了请求域
请求转发:客户端的一次请求到达后,发现需要借助其他Servle来实现功能
特点:
浏览器地址栏不变
域对象中的数据不丢失
负责转发的 Servlet 转发前后的响应正文会丢失 (ServletA的响应正文会丢失,由ServletB响应客户端)
由转发的目的地来响应客户端
返回值 | 方法名 | 说明 |
---|---|---|
RequestDispatcher | getRequestDispatcher(String name) | 获取请求调度对象 |
void | forward(ServletRequest req,ServletResponse resp) | 实现转发 |
- package servlet;
-
- 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("/servletDemo09")
- public class ServletDemo09 extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //设置共享数据
- req.setAttribute("encoding","gbk");
- //获取请求调度对象
- RequestDispatcher rd = req.getRequestDispatcher("/servletDemo10");
- //实现转发功能
- rd.forward(req,resp);
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req,resp);
- }
- }
-
- package servlet;
-
- 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("/servletDemo10")
- public class ServletDemo10 extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- //获取共享数据
- Object encoding = req.getAttribute("encoding");
- System.out.println(encoding);
- System.out.println("servletDemo10执行了...");
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req,resp);
- }
- }
在实际开发中,我们可能需要把两个Servlet的内容合并到一起来响应浏览器,而HTTP协议的特点是一请求,一响应的方式
所以绝对不可能出现有两个Servlet同时响应方式。那么我们就需要用到请求包含,把两个Servlet的响应内容合并输出
请求包含:可以合并其他 Servlet 中的功能一起响应给客户端
特点:
浏览器地址栏不变
域对象中的数据不丢失
被包含的 Servlet 响应头会丢失
返回值 | 方法名 | 说明 |
---|---|---|
RequestDispatcher | getRequestDispatcher(String name) | 获取请求调度对象 |
void | include(ServletRequest req,ServletResponse resp) | 实现包含 |
- package servlet;
-
- 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("/servletDemo11")
- public class ServletDemo11 extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- System.out.println("servletDemo11执行了...");
- //获取请求调度对象
- RequestDispatcher rd = req.getRequestDispatcher("/servletDemo12");
- //实现包含功能
- rd.include(req,resp);
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req,resp);
- }
- }
- package servlet;
-
- 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("/servletDemo12")
- public class ServletDemo12 extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- System.out.println("servletDemo12执行了...");
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req,resp);
- }
- }