当前位置:网站首页>Request and response description
Request and response description
2022-07-22 10:35:00 【Hua Weiyun】
Request and Response explain
Simple function description
From the literal meaning of the word , We understand that a person is asking , One is to respond .
We write Servlet When coding, we often see these two parameters .
We need to know what role these two parameterizers play .
Quote the description of dark horse
request: Get request data
The browser will send HTTP Request to the background server [Tomcat]
HTTP Your request will contain a lot of request data [ Request line + Request header + Request body ]
Background server [Tomcat] Would be right HTTP The data in the request is parsed and the parsing result is stored in an object. The stored object is request object , So we can request Object to get the relevant parameters of the request
After obtaining the data, you can continue the subsequent business , For example, obtaining the user name and password can realize the related business of login operation
response: Set response data
After business processing , The background needs to return the result of business processing to the front end, that is, response data, and encapsulate the response data into response In the object
Background server [Tomcat] Can parse response object , according to [ Response line + Response head + Response body ] Format splicing result browser final parsing result , Display the content in the browser for users to browse
Code test cases
package jgd;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("/dod/*")public class ServletDemo8 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// super.doGet(req, resp);// Get request method String method = req.getMethod(); System.out.println(" Request mode :"+method);// Get virtual access directory ( Project access path ) String contextPath = req.getContextPath(); System.out.println(" Virtual directory :"+contextPath);// obtain url StringBuffer requestURL = req.getRequestURL(); System.out.println("url"+requestURL.toString());// Get resource identifier String requestURI = req.getRequestURI(); System.out.println(" Resource identifier "+requestURI);// Get request parameters String queryString = req.getQueryString(); System.out.println(" Request parameters "+queryString);// Get a request header of the browser version String agent = req.getHeader("user-agent");// obtain System.out.println(" browser "+agent); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// super.doPost(req, resp);// obtain post Of 2 Request body // 1: Get character input stream BufferedReader br = req.getReader();// Reading data String line = br.readLine(); System.out.println(line);// After one request ,request After the object is destroyed , The flow will automatically close }}
package jgd;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("/demo9")public class ServletDemo9 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String name = req.getParameter("name"); resp.setHeader("content-type", "text/html;character=utf-8"); resp.setContentType("text/html;charset=utf-8");//html// resp.setContentType("text/plain; charset=utf-8"); Text // charset=UTF-8. If the method is getWriter() Call before method is called , Then the character encoding of the response will only start from the given content type Set in . If the method is in the getWriter() The method is called or is called after it is submitted. ,// The character encoding of the response will not be set , In the use of http In the case of an agreement , The method is designed as follows Set up Content-type Physical header . resp.getWriter().write("<h1>" + name + ", Welcome </h1>"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// super.doPost(req, resp); System.out.println("POST..."); }}
== Please see the code comments for the points needing attention ==
==request Object is the object used to encapsulate the requested data
response Object is an object used to encapsulate response data ==
Of course, this is just our little understanding
Request Inheritance system
Then we will find that in different Servlet The writing of these two parameters in is also different .
package jgd;import javax.servlet.*;import javax.servlet.annotation.WebServlet;import java.io.IOException;@WebServlet("/demo1")public class ServletDemo01 implements Servlet { public void init(ServletConfig servletConfig) throws ServletException { } public ServletConfig getServletConfig() { return null; } public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { //Servlet When interviewed service Automatically System.out.println("servlet hello world"); } public String getServletInfo() { return null; } public void destroy() { }}
package jgd;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("/jgdabc/666")// Multiple access paths can be configured , This is an exact match //@WebServlet(urlPatterns = {"/demo7","/demo07"})//@WebServlet(urlPatterns = "/user/*") Directory matching // When both exact matching and directory matching are satisfied , Precise matching has higher priority //@WebServlet("*.do") Extension matching // Any match // /* Has a higher priority than ///@WebServlet("/")//@WebServlet("/*")public class ServletDemo7 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("demo7get..."); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// super.doPost(req, resp); }}
One is existing in inheritance Servlet Method parameter body , One is HttpServlet Of doGet() and doPost() In the parameter body of the two methods . We can obviously find that the writing of these two parameters is different . But it is still an implementation request and an implementation response .
Now let's look at Request, Presumably, it also has its own inheritance system .
ServletRequest and HttpServletRequest All are Java Provided . Then it is implemented by Tomcat To complete . The implementation class is RequestFacade.
== This class implements HttpServletRequest Interface , It also indirectly realizes ServletRequest Interface .
Servlet Class service Method 、doGet Method or doPost The method is ultimately determined by Web The server [Tomcat]
To invoke the , therefore Tomcat The specific implementation class of method parameter interface is provided , And completed the creation of the object ==
The verification method is that you put request Print out , You can understand .
边栏推荐
- Energy principle and variational method note 10: virtual displacement principle
- 史上最全的mysql数据类型汇总-(上)
- 能量原理与变分法笔记02:变分问题 变分和微分运算能交换次序 欧拉方程
- Laundry applet source code - add docking with third-party errands
- 【Cicadaplayer】vs2019(V142) X64 FFMEPG : error C2169: “lrintf”: 内部函数,不能定义
- 如何使用文档工具进行 API 管理?
- 走出心理舒适区的七个最佳方法
- Eolink ——通过文档驱动,快速开发接口
- 2022 latest Hubei construction eight members (Mechanics) simulated examination question bank and answers
- mysql中的字段如何选择合适的数据类型呢?
猜你喜欢
智能手机天线调谐
1307_嵌入式设计中的晶振测试小结
Use cpolar to build a business website (3)
输入密码才可以浏览网页的PHP源码
Analyze a ros2 cmakelists in detail Txt file
壁纸背景墙/头像/动态壁纸小程序源码-支持用户投稿-带部分采集功能+搭建教程
什么是FastAPI异步框架?(全面了解)
2022 latest Hubei construction eight members (Mechanics) simulated examination question bank and answers
【Cicadaplayer】vs2019(V142) X64 FFMEPG : error C2169: “lrintf”: 内部函数,不能定义
day02-2
随机推荐
微信小程序开发学习6(基础加强之使用npm包和全局数据共享及分包【Tab底栏案例改进】)
Leetcode skimming: dynamic planning 02 (climbing stairs)
Lihuadaiyu music player 3.91 source code is open source (add background music to the website)
嵌入式总线与通信
Smart phone antenna tuning
2022.7.21 特殊矩阵压缩
How to add an operator in ONEFLOW
Some personal understanding
AtCoder Beginner Contest 260 E - At Least One
公司新来一个同事,把 Typora 玩得炉火纯青!太强悍了
Baidu PaddlePaddle easydl x wesken: see how to install the "eye of AI" in bearing quality inspection
详细分析一个ROS2 CMakeLists.txt文件
Start, close and view MySQL service (Linux)
Construction of Chenzhou stem cell laboratory: analysis of strong current system
Energy principle and variational method note 05: examples of natural boundary conditions
Execution failed for task ‘:app:kaptDevDebugKotlin‘.
QUuid
能量原理与变分法笔记08: 虚功原理的推导
反弹shell通过普通用户进行SUID提权
Std:: thread combined with class object