##  Request

  1、request对象和response对象的原理

    1、request和response对象是由服务器创建的。我们来使用他们。

    2、request对象是来获取请求消息,response对象是来设置响应消息

 

   2、request对象集成体系结构:

    ServletRequest:----接口

        |  继承

    HttpServletRequest: ----接口

        |   实现

    org.apache.catalina.connector.RequestFacade  类(tomcat)

   3、request功能

    1、获取请求消息

      1、获取请求行数据

      *  GET /day14/demo1?name=zhangsan  HTTP/1.1

      *  方法:

        1、获取请求方式:GET

        *  String  getMethod()

        2、获取虚拟目录:/day14

        *  String  getContextPath();

        3、获取Servlet路径:/demo01

        *  String getServletPath();

        4、获取GET方式请求参数:name=zhangsan

        *  String  getQueryString();

        5、获取请求的URI:/day14/demo1

        *  String  getRequestURI();      /day14/demo1

        *  StringBuffer  getRequestURL();      http://localhost/day14/demo1

        URL:统一资源定位符    (中华人民共和国)

        URI:统一资源标识符  (共和国)

        6、获取协议及版本:GTTP/1.1

        *  String  getProtocol();

        7、获取客户机的IP地址:

        *  String  getRemoteAddr();

      2、获取请求头数据

        *  方法:

          *  String  getHeader(String name):通过请求头的名称来获取请求头的值

          *  getHeaderNames():获取所有请求头名称

      3、获取请求体数据

        *  请求体:只有POST请求方式,才有请求体,在请求体中封装了POST请求的的请求参数

        *  步骤:

          1、获取流对象

          *  BufferedReader  getReader()   获取字符输入流,只能操作字符数据

package com.Request.demo03;

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.BufferedReader;
import java.io.IOException;

@WebServlet("/requestDemo04")
public class RequestDemo01 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader br = request.getReader();
        String line = null;
        while ((line = br.readLine()) != null){
            System.out.println(line); //username=ftj&password=123
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

          *  ServletInputStream  getInputStream()   获取字节输入流,可以操作所有类型的数据

          2、再从流对象中拿数据

    2、其他功能

      1、获取请求参数通用方法

      *  String getParameter(String name):根据参数名称获取参数值  username=zhangsan&password=123

      *  String[]  getParameterValues(String name):根据参数名称获取参数值的数组  hobbies=XX&hobbies=YY

      *  Enumeration<String>  getParameterNames():获取所有请求参数名称

      *  Map<String,String[]>  getParameterMap():所有参数的键值对集合

package com.Request.demo03;

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("/requestDemo05")
public class RequestDemo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*Enumeration<String> parameterNames = req.getParameterNames();
        while (parameterNames.hasMoreElements()){
            String s = parameterNames.nextElement();
            System.out.println(s);
            String[] para = req.getParameterValues(s);
            //System.out.println(para);
            for (String s1 : para) {
                System.out.println(s1);
            }
        }*/
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        System.out.println(username);
    }

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

      2、请求转发:一种在服务器内部资源跳转的方式。

      *  步骤:

        1、通过request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)

        2、使用这个对象进行转发:forward(ServletRequest  request,  ServletResponse  response)

      *  特点:

        1、地址栏路径没有发生变化

        2、转发只能转发到当前服务器内部资源中

        3、转发是一次请求

      3、共享数据

      *  域对象:一个有作用范围的对象,可以再范围内共享数据。

      *  request域:代表一次请求的范围,一般用于请求转发的多个资源中去共享数据

      *  方法: 

        1、setAttribute(String name, Object obj):存储数据

        2、getAttribute(String name):通过键来获取值

        3、removeAttribute(String name):通过键来移除键值对

package com.Request.demo04;

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;

/**
 * 共享数据:request域
 */
@WebServlet("/RequestDemo01")
public class RequestDemo03 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("msg","hello");
        request.getRequestDispatcher("/RequestDemo02").forward(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
package com.Request.demo04;

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("/RequestDemo02")
public class RequestDemo04 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Object msg = request.getAttribute("msg");
        System.out.println(msg);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

      4、获取ServletContext对象

      *  ServletContext  getServletContext();

Posted on 2020-03-10 23:02  夏洛蒂的手稿  阅读(149)  评论(0编辑  收藏  举报