Request请求原理以及获取请求行,请求头,请求体

一、request和response的原理

request和response对象是由Tomcat服务器创建的

  • request对象是来获取请求数据的
  • response对象是设置响应数据的

request执行原理
在这里插入图片描述

二、request获取请求行

1、获取请求消息数据
获取请求行数据,可查看ServletRequest文档
代码如下:

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("/test01")
public class test01_Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("hahahahhh");

        String method = request.getMethod();//掌握
        System.out.println("获取到的请求方法:"+method);

        String contextPath = request.getContextPath();//掌握
        System.out.println("获取到的虚拟目录:"+contextPath);

        String servletPath = request.getServletPath();//掌握
        System.out.println("获取到的servlet路径:"+servletPath);

        String queryString = request.getQueryString();//知道
        System.out.println("获取到的请求参数是:"+queryString);

        String requestURI = request.getRequestURI();
        System.out.println("获取到的URI是:"+requestURI);

        StringBuffer requestURL = request.getRequestURL();
        System.out.println("获取到的URL是:"+requestURL);
		//URL:统—资源定位符号   URI:统—资源标识符,范围更广
        String protocol = request.getProtocol();
        System.out.println("获取到的版本信息是:"+protocol);

        String remoteAddr = request.getRemoteAddr();
        System.out.println("获取到的请求地址是:"+remoteAddr);

    }

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

运行结果如下:
在这里插入图片描述

三、request获取请求头

代码如下:

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("/test02")
public class test02_Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("hhhhhhh");

        //引号内为请求头的名字,获取请求头的值
        String referer = request.getHeader("referer");
        System.out.println("我从哪里来:"+referer);

        //获取所有的请求头的名字
        Enumeration<String> names = request.getHeaderNames();
        while (names.hasMoreElements()){
            String name = names.nextElement();
            System.out.println("请求头名字是:"+name);

            String value = request.getHeader(name);
            System.out.println(name+":"+value);//键和值依次输出
        }
    }

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

运行结果如下:
在这里插入图片描述

四、request获取请求体

  • 只用post提交方式有请求体
  • 请求体把请求内容以流的方式封装起来

代码如下:
HTML代码块:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试http请求数据格式</title>
</head>
<body>
<form action="/test03" method="post">
    <input type="text" placeholder="请输入用户名" name="user">
    <input type="submit" value="提交">
</form>
</body>
</html>

Java代码块:

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;

//获取post请求的请求体

@WebServlet("/test03")
public class test03_Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取post请求的请求体
        BufferedReader reader = request.getReader();
        String s=null;
        while ((s=reader.readLine())!=null){
            System.out.println(s);
        }
    }

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

运行结果如下:
在这里插入图片描述

在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值