Servlet—HTTP—Request

    技术2024-11-08  11

    request对象和response对象的原理

    request和response对象是由服务器创建的,用来给我们使用。request对象是来获取请求消息,response对象是来设置响应消息。 执行原理 Tomcat服务器会根据请求url中的资源路径,创建对应的ServletDemo1的对象。Tomcat服务器会创建request和response对象,request对象中封装请求消息的数据。Tomcat服务器将request和response两个对象传递给service方法,并且调用service方法。程序员可以通过request对象来获取消息数据,通过response对象来设置响应消息数据。Tomcat服务器在给浏览器做出响应之前,会从response对象取出程序员设置的响应消息数据。

    request类的继承体系结构

    ServletRequest – 接口 | 继承 HttpServletRequest – 接口 | 继承 RequestFacade – 类(Tomcat)

    request主要功能

    获取请求消息数据 获取请求行数据 GET /study/demo1?name=zhangsan HTTP/1.1方法: 获取请求方式:GET String getMethod() 获取虚拟目录:/sdudy String getContextPath() 获取Servlet路径:/demo1 String getServletPath() 获取请求的URI:/study/demo1 String getRequestURI() 获取请求的URL:http://localhost/study/demo1 StringBuffer getRequestURL() 获取GET方式的请参数:name=zhangsan String getQueryString() 获取协议及版本:HTTP/1.1 String getProtocol() 获取客户机IP地址: String getRemoteAddr() 代码: @WebServlet("/requestDemo1") public class RequestDemo1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1. 获取请求方式:GET String method = request.getMethod(); System.out.println(method); // 2. 获取虚拟目录:/sdudy String contextPath = request.getContextPath(); System.out.println(contextPath); // 3. 获取Servlet路径:/demo1 String servletPath = request.getServletPath(); System.out.println(servletPath); // 4. 获取请求的URI:/study/demo1 String requestURI = request.getRequestURI(); System.out.println(requestURI); // 5. 获取请求的URL:http://localhost/study/demo1 StringBuffer requestURL = request.getRequestURL(); System.out.println(requestURL); // 6. 获取GET方式的请参数:name=zhangsan String queryString = request.getQueryString(); } } 浏览器访问:http://localhost/study/requestDemo1?name=zhangsan&passowrd=123运行: 获取请求头数据 方法: String getHeader(String name):通过请求头的名称来获取请求头的值Enumeration<String> getHeaderNames:获取所有请求头名称 Servlet: @WebServlet("/requestDemo3") public class RequestDemo3 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //演示获取请求头数据: //1.获取所有的请求头名称 Enumeration<String> headerNames = request.getHeaderNames(); //2.遍历 while(headerNames.hasMoreElements()){ String name = headerNames.nextElement(); //根据名称来获取请求头的值 String value = request.getHeader(name); System.out.println(name+"-->"+value); } } } 访问后输出: 获取请求体数据 请求体:只有POST请求方式,才有请求体,在请求体中封装了POST请求的请求参数步骤: 获取流对象 BufferReader getReader():获取字符输入流,只能操作字符数据ServletInputStream getInputStream():获取字节输入流,可以操作所有类型数据 从流对象中拿到数据 表单页: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>注册页面</title> </head> <body> <form action="/duing1/requestDemo5" method="post"> <input type="text" placeholder="请输入用户名" name="username"> <br> <input type="text" placeholder="请输入密码" name="password"> <br> <input type="submit" value="注册"> </form> </body> </html>

    Servlet: @WebServlet("/requestDemo5") public class RequestDemo5 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取请求消息体--请求参数 //1.获取字符流 BufferedReader br = request.getReader(); //2.读取数据 String line = null; while((line = br.readLine()) != null){ System.out.println(line); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } } 表单页点击提交后获取的数据:

    request其他的功能

    获取请求参数通用方式:不论get还是post请求方式个都可以使用下列方法来获取请求参数 String getParameter():根据参数名称获取参数值 username=zhangsan&password=123 Servlet: @WebServlet(name = "RequestDemo6") public class RequestDemo6 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //post 获取请求参数 //根据参数名称获取参数值 String username = request.getParameter("username"); System.out.println("post"); System.out.println(username); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //get 获取请求参数 //根据参数名称获取参数值 String username = request.getParameter("username"); System.out.println("get"); System.out.println(username); } }

    提交表单:

    提交后获取到参数值:

    String[] getParameterValues():根据参数名称获取参数值的数组 hobby=game&hobby=sport *Servlet: @WebServlet("/requestDemo7") public class RequestDemo7 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //根据参数名称获取参数值得数组 String[] hobbies = request.getParameterValues("hobby"); for(String hobby:hobbies){ System.out.println(hobby); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } } 多选框: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>注册页面</title> </head> <body> <form action="/study/requestDemo7" method="post"> <input type="text" placeholder="请输入用户名" name="username"> <br> <input type="text" placeholder="请输入密码" name="password"> <br> <input type="checkbox" name="hobby" value="game">游戏 <input type="checkbox" name="hobby" value="study">学习 <br> <input type="submit" value="注册"> </form> </body> </html>

    提交后获取到参数值:

    Enumeration<String> getParamaterNames():获取所有请求的参数名称(与上面类似)Map<String,String[]> getParamaterMap():获取所有参数的Map集合(与上面类似)中文乱码问题: get方式:Tomcat 8 已经将get方式了乱码问题解决了post方式:会乱码 解决:在获取参数时,设置request的编码:request.setCharacterEncoding(“utf-8”); 请求转发:一种在服务器内部的资源跳转方式 步骤: 通过request对象来获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)使用RequestDispatcher对象来进行转发:forward(ServletRequest request, ServletResponse response) Servlet: @WebServlet("/requestDemo8") public class RequestDemo8 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("demo8被访问了"); //转发到demo9资源 RequestDispatcher requestDispatcher = request.getRequestDispatcher("/requestDemo9"); requestDispatcher.forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } } @WebServlet("/requestDemo9") public class RequestDemo9 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("demo9被访问了"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } } 访问RequestDemo8的结果: 特点: 浏览器地址栏路径不发生变化只能转发到访问当前服务器内部资源中转发是一次请求 共享数据 域对象:一个 有作用范围的对象,可以在范围内共享数据request域:代表一次请求的范围,一般用于请求转发的多个资源中共享数据方法: void setAttribute(String name,Object obj):存储数据Object getAttribute(String name):通过键获取值void removeAttribute(String name):通过键移除键值对 Servlet: @WebServlet("/requestDemo8") public class RequestDemo8 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("demo8被访问了"); //存储数据到request中 request.setAttribute("msg","hello"); //转发到demo9资源 RequestDispatcher requestDispatcher = request.getRequestDispatcher("/requestDemo9"); requestDispatcher.forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } } @WebServlet("/requestDemo9") public class RequestDemo9 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取数据 Object msg = request.getAttribute("msg"); System.out.println(msg); System.out.println("demo9被访问了"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } } 访问RequestDemo8的结果: 获取ServletContext ServletContext getServletContext()
    Processed: 0.012, SQL: 9