关于HttpServletRequest的理解

201 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路

HttpServletRequest

HttpServletRequest代表的是客户机的请求,当客户机通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过HttpServletRequest提供的方法,就可以获得客户机所有请求信息。

  • 获取客户机提交的数据:HttPServletRequest
  • 向客户机发送数据:HttPServletReponse

image.png

获得用户(客户机)信息(列举部分)
getRequestURL()Reconstructs the URL the client used to make the request.
getRequestURI()Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.
getQueryString ()Returns the query string that is contained in the request URL after the path.
getRemoteUserReturns the login of the user making this request, if the user has been authenticated, or if the user has not been authenticated
getPathInfo()Returns any extra path information associated with the URL the client sent when it made this request.

通过HttpServletRequest中的方法,我们可以轻易客户机的信息,就拿获取客户机的URL举例:

package com.liu.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class HttpRequest extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        StringBuffer requestURL = req.getRequestURL();
        System.out.println(requestURL);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

注意:此处省略在web.xml中注册的步骤,自行注册

上述代码运行结果如下图:

image.png

获得客户机请求头
getHeaders(java.lang.String name)Returns all the values of the specified request header as an of objects.
getHeaderNames()Returns an enumeration of all the header names this request contains.
getHeader(java.lang.String name)Returns the value of the specified request header as a

HttpServletRequest一个重要应用就是请求转发

1.那么什么是请求转发呢?
请求准发是指一个web资源收到客户端请求后,通知服务器去调用另外一个web资源进行处理。

2.那么在什么情况下需要用到请求转发这项技术呢?
在实际的项目开发过程中,服务器对于客户端请求做出的响应可能会复杂,需要多个Servlet 来协作完成。由于Servlet 中的service() 方法由Servlet 容器调用,所以一个 Servlet 的对象是无法调用另一个 Servlet 的方法的。当一个响应需要多个Servlet完成时,这时候就需要请求转发技术了。下面是请求转发的代码演示:

public class LoginServlet extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
  		req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
     

		// 接收参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String[] hobbys = req.getParameterValues("hobbys");

        //后台接收中文乱码问题
        System.out.println("=============================");
        System.out.println(username);
        System.out.println(password);
        System.out.println(Arrays.toString(hobbys));
        System.out.println("=============================");

        //通过请求转发
        System.out.println(req.getContextPath());
        req.getRequestDispatcher("/success.jsp").forward(req,resp);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
}