HttpServletRequest

HttpServletRequest

service方法中的request的类型是ServletRequest,而doGet/doPost方法的request的类型是HttpServletRequest,HttpServletRequest是ServletRequest的子接口,功能和方法更加强大

request的运行流程

HttpServletRequest

 

 

 

通过抓包工具抓取Http请求

HttpServletRequest

 

 

 

因为request代表请求,所以我们可以通过该对象分别获得Http请求的请求行,请      求头和请求体

通过request获得请求行

获得客户端的请求方式:String getMethod()

获得请求的资源:

String getRequestURI()

StringBuffer getRequestURL()

String getContextPath() ---web应用的名称

String getQueryString() ---- get提交url地址后的参数字符串

username=zhangsan&password=123

 

注意:request获得客户机(客户端)的一些信息

request.getRemoteAddr() --- 获得访问的客户端IP地址

 

HttpServletRequest
public class LineServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getMethod();
        System.out.println("请求方式:"+method);
        //获取请求地址
        String URI=request.getRequestURI();
        System.out.println("URI:"+URI);//相对路径
        StringBuffer URL=request.getRequestURL();
        System.out.println("URL:"+URL);
        //获取WEB应用名称
        String name=request.getContextPath();
        System.out.println("WEB应用名称"+name);
        //获取get请求的url地址兰后参数字符
        String query=request.getQueryString();
        System.out.println("get请求参数:"+query);
        //获取客户端ip地址
        String ip=request.getRemoteAddr();
        System.out.println("ip地址:"+ip);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
HttpServletRequest

HttpServletRequest

 

 

通过request获得请求头

String getHeader(String name)

referer头的作用:获取该访问的的来源

做防盗链

HttpServletRequest
public class HeadServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取tou
        String tou=request.getHeader("Referer");
        //解决乱码
        response.setContentType("text/html;charset=UTF-8");
        if(tou.startsWith("http://localhost")){
            response.getWriter().write("特朗普");
        }else{
            response.getWriter().write("小偷");
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
HttpServletRequest

点击链接跳转HeadServlet判定是否是本地地址

<body>
   <a href="/WEB04/HeadServlet">特朗普强行收购TikTok</a>
</body>

 

通过request获得请求体

请求体中的内容是通过post提交的请求参数,格式是:

username=zhangsan&password=123&hobby=football&hobby=basketball

key ---------------------- value

username                    [zhangsan]

password                    [123]

hobby                         [football,basketball]

                       

 

以上面参数为例,通过一下方法获得请求参数:

String getParameter(String name)

String[] getParameterValues(String name)

Enumeration getParameterNames()

Map<String,String[]> getParameterMap()

 

      注意:get请求方式的请求参数 上述的方法一样可以获得

      解决post提交方式的乱码:request.setCharacterEncoding("UTF-8");

      解决get提交的方式的乱码:

            parameter = new String(parameter.getbytes("iso8859-1"),"utf-8");

 

HttpServletRequest
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取用户名
        String username=request.getParameter("username");
        System.out.println("用户名"+username);
        System.out.println("========================");
        //获取爱好
        String[] h=request.getParameterValues("hobby");
        for(String hobb:h){
            System.out.println(hobb);
        }
        System.out.println("========================");
        //获取所有参数所在的Map集合
        Map<String, String[]> map=request.getParameterMap();
        //获取所有Key
        Set<String> keys= map.keySet();
        for(String key:keys){
            String[] values=map.get(key);
            for(String value:values){
                System.out.println(key+"..."+value);
            }
        }
 

 

 

HttpServletRequest
<body>
    <form action="/WEB04/BodyServlet" method="get">
       用户名:<input type="text" name="username"><br>
       密码:<input type="password" name="pwd"><br>
       性别:男<input type="radio" name="sex" value="nan">
            女<input type="radio" name="sex" value="nv"><br>
       爱好:  足球<input type="checkbox" name="hobby" value="zq">
                     兰球<input type="checkbox" name="hobby" value="lq">
                     拍球<input type="checkbox" name="hobby" value="pq"><br>
         <input type="submit" value="注册">
    </form>
</body>
HttpServletRequest

 

request的其他功能

(1)request是一个域对象

request对象也是一个存储数据的区域对象,所以也具有如下方法:

setAttribute(String name, Object o)

getAttribute(String name)

removeAttribute(String name)

 

注意:request域的作用范围:一次请求中

(2)request完成请求转发

获得请求转发器----path是转发的地址

RequestDispatcher getRequestDispatcher(String path)

通过转发器对象转发

requestDispathcer.forward(ServletRequest request, ServletResponse response)

 

              

      注意:ServletContext域与Request域的生命周期比较?

         ServletContext

            创建:服务器启动

            销毁:服务器关闭

            域的作用范围:整个web应用

         request

            创建:访问时创建request

            销毁:响应结束request销毁

            域的作用范围:一次请求中

 

      注意:转发与重定向的区别?

         1)重定向两次请求,转发一次请求

         2)重定向地址栏的地址变化,转发地址不变

         3)重新定向可以访问外部网站 转发只能访问内部资源

         4)转发的性能要优于重定向

        

     

      注意:客户端地址与服务器端地址的写法?

         客户端地址:

            是客户端去访问服务器的地址,服务器外部的地址,特点:写上web应用名        

           

            直接输入地址:

            重定向

           

 

 

         服务器端地址:

            服务器内部资源的跳转的地址,特点:不需要写web应用的名称

 

            转发

 

总结:

request获得行的内容

      request.getMethod()

      request.getRequestURI()

      request.getRequestURL()

      request.getContextPath()

      request.getRemoteAddr()

request获得头的内容

      request.getHeader(name)

request获得体(请求参数)

      String request.getParameter(name)

      Map<String,String[]> request.getParameterMap();

      String[] request.getParameterValues(name);

      注意:客户端发送的参数 到服务器端都是字符串

 

      获得中文乱码的解决:

         post:request.setCharacterEncoding(“UTF-8”);

         get:

parameter = new String(parameter.getBytes(“iso8859-1”),”UTF-8”);

 

request转发和域

      request.getRequestDispatcher(转发的地址).forward(req,resp);

      request.setAttribute(name,value)

      request.getAttribute(name)

 

HttpServletRequest
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getMethod();
        System.out.println("请求方式:"+method);
        //获取请求地址
        String URI=request.getRequestURI();
        System.out.println("URI:"+URI);//相对路径
        StringBuffer URL=request.getRequestURL();
        System.out.println("URL:"+URL);
        //获取WEB应用名称
        String name=request.getContextPath();
        System.out.println("WEB应用名称"+name);
        //获取get请求的url地址兰后参数字符
        String query=request.getQueryString();
        System.out.println("get请求参数:"+query);
        //获取客户端ip地址
        String ip=request.getRemoteAddr();
        System.out.println("ip地址:"+ip);
    }
HttpServletRequest

HttpServletRequest

 

 请求转发

HttpServletRequest
public class Servlet01 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //存值
        request.setAttribute("name", "小薯条");
        //请求转发,request跳转,跳转,地址不变
        request.getRequestDispatcher("/Servlet02").forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
HttpServletRequest

 

HttpServletRequest
public class Servlet02 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //取值
        String name=(String)request.getAttribute("name");
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("hello dandan..."+name);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
HttpServletRequest

HttpServletRequest

上一篇:Webpack打包


下一篇:ASPMVC课程设计