【Servlet】HTTP protocol

content

 What is an agreement?

Requested HTTP protocol format

GET request

Icon:

POST request

 Common request header description

What are GET and POST requests

GET request:

POST request:

HTTP protocol format of the response

Common Responses Explained

MIME Type Description

 HttpServletRequest class

Common methods of the HttpServletRequest class

Method demonstration:

After running:

Servlet gets parameters in web form

get request:

post request:


 What is an agreement?

Agreement refers to the rules that two parties, or multiple parties, have agreed upon with each other, and everyone needs to abide by them. It is called an agreement.

The HTTP protocol means that the data sent between the client and the server needs to comply with the rules, which is called the HTTP protocol.

The data in HTTP is also called a message.

Requested HTTP protocol format

The data sent by the client to the server is called a request.

The server sends data back to the client, called a response.

There are two types of requests: GET and POST requests

GET request

1. Request line

(1) The method of request is GET

(2) The requested resource path [+?+ request parameter]

(3) The version number of the requested protocol HTTP/1.1

2. Request header

key:value is composed of different key-value pairs, representing different meanings.

Start the server:

Icon:

POST request

1. Request line

 (1) The method of request is POST

   (2) Requested resource path [+?+Request parameter]

   (3) Requested protocol number HTTP/1.1

2. Request header

     1) key:value Different request headers have different meanings

    blank line

3. The request body ===>> is the data sent to the server

Main content of the form: 

<from action="http://localhost:8080/06_servlet/hello3" method="post">
    <input type="hidden" name="action" value="login"/>
    <input type="hidden" name="username" value="root"/>
    <input type="submit">
</from>

Icon:

 Common request header description

        Accept: Indicates the data type that the client can accept

        Accpet-languege: Indicates the type of language that the client can accept

        User-Agent: Indicates information about the client browser

        Host: indicates the server ip and port number when requesting

What are GET and POST requests

GET request:

         1. form tag method=get

        2. a label

        3. The link tag introduces css

        4. Script tags import js files

        5. The img tag introduces pictures

        6. Ifram introduces html page

        7. Enter the address in the browser address bar and press Enter

POST request:

        1. from tag method=post

HTTP protocol format of the response

1. Response line

        (1) The protocol and version number of the response

        (2) Response status code 1

        (3) Response status descriptor 

2. Response header

        (1) key:value Different response headers have different meanings

 blank line

3. The response body - >>> is the data returned to the client

Common Responses Explained

200 means the request was successful

302 means request redirection

404 means that the request server has received it, but the data you want does not exist (address error)

500 means the server has received the request, but the server has an internal error (bad code)

MIME Type Description

MiME is a data type in the HTTP protocol.

The full English name of MIME is "Multipurpose Internet Mail Extensions", and the format of MIME type is "large type/small type", which corresponds to the extension of a certain file.

Such as: gif type; large type is picture, small type is gif, expressed as image/gif

Common MIME types:

 HttpServletRequest class

effect:

Every time a request enters the Tomcat server, the Tomcat server will parse and encapsulate the requested HTTP protocol information into the Request object, and then pass it to the service method (doGet and doPost for us to use, we can get it through the HttpServletRequest object) all requested information)

Common methods of the HttpServletRequest class

getRequestURI() gets the requested resource path

getRequestURL() Get the requested Uniform Resource Locator (absolute path)

getRemoteHost gets the client's ip address

getHeader() Get the request header

getParameter Get request parameters

getParameterValues() Get the parameters of the request pair (used at the same time when there are multiple values)

getMethod() Get the request method GET or POST

setAttribute(key,value) set field data

getAttribute(key) Get domain data

getRequestDispatcher Get the request forwarding object

Method demonstration:

Create a web project 03_servlet and configure it in web.xml

    <servlet>
        <servlet-name>RequestAPIServlet</servlet-name>
        <servlet-class>com.servlet.RequestAPIServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>RequestAPIServlet</servlet-name>
        <url-pattern>/requestAPIServlet</url-pattern>
    </servlet-mapping>

Create a class RequestAPIServlet under src to inherit HttpServlet


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RequestAPIServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        getRequestURI() 获取请求的资源路径
        System.out.println("URI->"+req.getRequestURI());
        //        getRequestURL()  获取请求的统一资源定位符(绝对路径)
        System.out.println("URL->"+req.getRequestURL());
        //        getRemoteHost 获取客户端的ip地址
        System.out.println("客户端ip地址:"+req.getRemoteHost());
        //        getHeader()   获取请求头
        System.out.println("请求头User-Agent->"+req.getHeader("User-Agent"));
        //        getMethod()获取请求的方式GET或POST
        System.out.println("请求的方式:"+req.getMethod());
    }
}

Modify the information in the edit configuration to something like 03_servlet.

After running:

 URI->/03_servlet/requestAPIServlet URL-
>http://localhost:8080/03_servlet/requestAPIServlet
client ip address: 0:0:0:0:0:0:0:1
Request header User-Agent->Mozilla/ 5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36
Request method: GET

We can also find that the URI is just the resource path to the project name, while the URL is the full path and can be accessed directly.

Servlet gets parameters in web form

get request:

Create a parameter class ParameterServlet under src to inherit the HttpServlet class

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
public class ParameterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
     String username=req.getParameter("username");
     String password=req.getParameter("password");
     String[] hobby=req.getParameterValues("hobby");
        System.out.println("账号:"+username);
        System.out.println("密码:"+password);
        //Arrays.asList()返回的是一个数组
        System.out.println("爱好:"+ Arrays.asList(hobby));
    }

}

Configure parameters in web.xml

 <servlet>
        <servlet-name>ParameterServlet</servlet-name>
        <servlet-class>com.servlet.ParameterServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ParameterServlet</servlet-name>
        <url-pattern>/parameterServlet</url-pattern>
    </servlet-mapping>

Write the html file in the web directory

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="http://localhost:8080/03_servlet/parameterServlet" method="get">
 账号:<input type="text" name="username"><br>
 密码 :<input type="password" name="password"><br>
    <input type="checkbox" name="hobby" value="HTML">HTML
    <input type="checkbox" name="hobby" value="Java">Java
    <input type="checkbox" name="hobby" value="JavaScript">JavaScript
    <input type="checkbox" name="hobby" value="Spring全家桶">Spring全家桶
    <input type="checkbox" name="hobby" value="Servlet">Servlet<br>
    <input type="submit">
</form>
</body>
</html>

Start running:

 Fill in several sets of information and submit:

post request:

In class:

 @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        String[] hobby=req.getParameterValues("hobby");
        System.out.println("账号:"+username);
        System.out.println("密码:"+password);
        //Arrays.asList()返回的是一个数组
        System.out.println("爱好:"+ Arrays.asList(hobby));
    }

When method="post", in the post request, once there is Chinese, there will be garbled characters.

At this point, the set character set should be added to the body of the doPost method:

  req.setCharacterEncoding("UTF-8");

 Note : The set character set must be above the acquisition parameters, otherwise garbled characters will also appear.

 @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   //设置字符集
        req.setCharacterEncoding("UTF-8");
        //获取请求参数
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        String[] hobby=req.getParameterValues("hobby");
        System.out.println("账号:"+username);
        System.out.println("密码:"+password);
        //Arrays.asList()返回的是一个数组
        System.out.println("爱好:"+ Arrays.asList(hobby));
    }

 Run again:

Guess you like

Origin blog.csdn.net/weixin_60719453/article/details/122792404