HTTP Hypertext Transfer Protocol and Principle of Request--1

HTTP Hypertext Transfer Protocol and Request Principle

1. HTTP
Hyper Text Transfer Protocol (HTTP) Hyper Text Transfer Protocol (HTTP) The
client sends a request to the server, and the server responds to the client after receiving the request information. The
request has a request format, and the response has a response format.
Transmission protocol: Defines the format of the data sent when the server and the client communicate
Insert picture description here
(1) Features:
1. Advanced protocol based on TCP/IP
2. Default port number: 80
That is, if the port number is the default port number, you can omit 80, such as tomcat The port number is set to 80
3. A request based on the request/response model
corresponds to only one response
4. Stateless:
each request is independent of each other and cannot exchange data
(2) Request data format: request line, request header, Request blank line, request body
1, request line
Composition: request method/request url request protocol/version For
example: GET/http://localhost:8080/hello.html HTTP/1.1
2, request header
format: request header name: value
There are multiple values ​​that can be separated by a comma.
3, request a blank line
to split the request header and request body (equivalent to a line of space)
4.
Place the requested parameters in the request body , such as the name entered in the name box, post request There is a request body.
For example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/test01" method="get">
    <input type="text" placeholder="请输入用户名" name="username">
    <input type="submit" value="提交">
</form>
</body>
</html>

operation result:
Insert picture description here

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 {
    
    
        String username = request.getParameter("username");//获取username的值:zhangsan
        System.out.println(username); //打印结果
    }

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

Operation results:
Insert picture description here
(3) Request methods
There are seven request methods in the HTTP protocol, two commonly used get and post
1. Get:
①. The request parameters are in the request line, which is behind the url.
② The length of the requested url is limited. The
URL length limit of firefox (Firefox) is 65536 characters, and the URL length limit of
chrome (Google) exceeds 8182 characters. A 414 error is returned.
③. The request is relatively insecure, and it is easy to expose data.
2. post
①. The request parameters are in the request body
. ②. The length of the requested url is not limited
. ③. Request security
(4) The Referer
client tells the server where the current request comes from.
Functions:
1. Anti-theft link (place stolen hyperlinks)
2. Statistics Work
two, Request request principle
(1), Request and Response principle
1, Requeset and Response objects are created by the tomcat server. The
request object is used to obtain request data. The
response object is to set response data.
Insert picture description here
Note: ServletRequest is the parent interface of HttpServeltRequest
2. The request object inherits the architecture:
servletRequest-interface
inherits
HttpservletRequest-interface
l Realize the
org.apache.catalina.connector.RequestFacade class (tomcat)
by printing the request object, view the tomcat source code
(2), Request function
1, get the request message
data①, get the request line data, you can check the ServletRequest document
Insert picture description here
URL: 统—Resources Positioning symbol
URI: system-resource identifier
Code display:

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 {
    
    
        //获取请求行信息
        //1,获取请求方法    request.getMethod
        String method = request.getMethod();
        System.out.println("请求方法是:"+method);

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

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

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

        //5,获取uri
        String requestURI = request.getRequestURI();
        System.out.println("获取的uri是:"+requestURI);

        //6,获取url
        StringBuffer requestURL = request.getRequestURL();
        System.out.println("获得url是:"+requestURL);

        //7,获取协议和版本号
        String protocol = request.getProtocol();
        System.out.println("获取到的版本号是:"+protocol);

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

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

operation result:
Insert picture description here

②, get request header data:
Insert picture description here
code display:

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 {
    
    
        //获取所有请求头名字
        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);
    }
}

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/tan1024/article/details/111436271