Request继承体系及获取请求数据

简介: Request继承体系及获取请求数据

一、Request继承体系

8b0271f0bffd48e09420d0f442af016d.png

1、Tomcat需要解析请求数据,封装为request对象,并且创建request对象传递到service方法中


2、使用request对象,查阅JavaEE API文档的HttServletRequest接口


代码示例查看request全类名:

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/demo2")
public class ServletDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(request);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

访问demo2路径:

580447ec816943eab4422c11677712cd.png

控制台输出全类名:

7a71abebd2804a52ab74c8234909cffb.png

二、Request获取请求数据

请求数据分为3部分:


1、请求行:

GET/request-demo/req1?username=zhangsan HTTP/1.1


String getMethod():获取请求方式:GET

String getContextPath():获取虚拟目录(项目访问路径):/request-demo

StringBuffer getRequestURL:获取URL(统一资源定位符):http://localhost:8080/request-demo/req1

String getRequestURL():获取URL(统一资源标识符):/request-demo/req1

String getQueryString():获取请求参数(GET方式):username=zhangsan&password=123


以上方法代码示例:

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("/req1")
public class RequestDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //String getMethod() :获取请求方式:GET
        String method = req.getMethod();
        System.out.println(method);//GET
        //String getContextPath():获取虚拟目录(项目访问路径):/request-demo
        String contextPath = req.getContextPath();
        System.out.println(contextPath);
        //StringBuffer getRequestURL:获取URL(统一资源定位符):http://localhost:8080/request-demo
        StringBuffer url = req.getRequestURL();
        System.out.println(url.toString());
        //String getRequestURI():获取URL(统一资源标识符):/request-demo/req1
        String uri = req.getRequestURI();
        System.out.println(uri);
        //String getQueryString():获取请求参数(GEI方式):username=zhangsan
        String queryString = req.getQueryString();
        System.out.println(queryString);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }
}

访问req1路径:

434e8e21179b455a8e4dcc0fff4d8604.png

控制台输出:

32262422fadf478e9bc9355fda11c643.png

2、请求头:

User-Agent:Mozilla/5.0 Chrome/91.0.4472.106

String getHeader(String name):根据请求头名称,获取值


以上方法代码示例:

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("/req1")
public class RequestDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求头:user-agent:浏览器版本信息
        String agent = req.getHeader("user-agent");
        System.out.println(agent);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }
}

访问req1路径:

434e8e21179b455a8e4dcc0fff4d8604.png

控制台输出:

4f82402394c348d69929b2cc87564158.png

3、请求体:

username=superbaby&password=123


ServletInputStream getInputStream():获取字节输入流

BufferedReader getReader():获取字符输入流


post请求需要表单,req.html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/Tomcate-demo2/req4" method="get">
    <input type="text" name="username"><br>
    <input type="password" name="password"><br>
    <input type="submit">
</form>
</body>
</html>

获取post请求参数代码:

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("/req1")
public class RequestDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取post请求体,请求参数
        //1、获取字符输入流
        BufferedReader br = req.getReader();
        //2、读取数量
        String line = br.readLine();
        System.out.println(line);
    }
}

访问req.html,并输出参数提交:

3e2a1af0e09241cdb17007db8e822649.png

控制台输出:

15d9439f3b6643b293b7ec9c0090f185.png

目录
相关文章
uniapp 如何封装uni.request请求(登录接口、业务接口)
uniapp 如何封装uni.request请求(登录接口、业务接口)
uniapp 如何封装uni.request请求(登录接口、业务接口)
|
5月前
|
前端开发 开发工具 数据安全/隐私保护
大事件项目13----axios请求拦截器,统一携带token
大事件项目13----axios请求拦截器,统一携带token
|
5月前
|
Java 应用服务中间件
Request继承体系,获取请求数据,Request通用方式获取请求参数
Request继承体系,获取请求数据,Request通用方式获取请求参数
|
6月前
|
存储 Java 应用服务中间件
Servlet执行流程&生命周期&方法介绍&体系结构、Request和Response的功能详解(2)
Servlet执行流程&生命周期&方法介绍&体系结构、Request和Response的功能详解
67 2
|
6月前
|
Web App开发 XML Java
Servlet执行流程&生命周期&方法介绍&体系结构、Request和Response的功能详解(1)
Servlet执行流程&生命周期&方法介绍&体系结构、Request和Response的功能详解
111 2
|
JSON 前端开发 API
新建一个uniapp请求,并且封装request
新建一个uniapp请求,并且封装request
40 1
|
前端开发 API
uniapp封装request请求
uniapp封装request请求
77 0
|
存储 API
10JavaWeb基础 - Request类
10JavaWeb基础 - Request类
57 0
|
XML JSON Java
10将业务接口的返回值写入到Response
MediaType、MimeType、Content-Type三者的关系 spring-mvc中的MediaType类 在spring-mvc中通过内容协商策略决定MediaType HttpMessageConverter体系介绍 MappingJackson2HttpMessageConverter是怎么完成写入操作的
334 0
|
Java Spring
SpringMVC源码分析:一个request请求的完整流程和各组件介绍
SpringMVC源码分析:一个request请求的完整流程和各组件介绍
48 0