Friends who understand Servlet http

Servlet annotation configuration

No need to configure web.xml.
Method 1 : Write directly on the class: @WebServlet(urlPatterns="/demo")
Method 2 : Or: The value attribute is very special. Generally, the most important attribute is represented by value. When there is only one attribute, The value can be written as
urlPatterns is very important, so the above can be written as @WebServlet(value="/demo"), and the value can be omitted, so it can be written as @WebServlet("/demo")

A Servlet can define multiple access paths: @WebServlet({"/demo1","/demo2"}), because it is an array, generally define one

IDEA and tomcat related configuration

(1) IDEA will create a separate configuration file for each Tomcat deployment project.
View the console log: Using CATALINA_BASE: "C:\Users\kenslu.IntelliJIdea2019.2\system\tomcat_servletProce"
(2) Workspace project and The web project of the tomcat deployment project
tomcat really accesses the "web project deployed by tomcat", and the "web project deployed by tomcat" corresponds to all the resources in the web directory of the "workspace project".
The resources in the WEB-INF directory cannot be directly accessed by the browser.
(3) Breakpoint debugging: start with "small bug", that is, dubug start.

Servlet architecture

Servlet --Interface
|
GenericServlet --Abstract class
|
HttpServlet --Abstract class

GenericServlet: The other methods in the Servlet interface are implemented by default, and only the service() method is abstracted
. When the Servlet class is defined in the future, GenericServlet can be inherited and the service() method can be implemented (not needed for real development)

HttpServlet : an encapsulation of the http protocol, simplifying the operation,
defining the class, inheriting the HttpServlet
replication doGet/doPost method

HTTP:

Concept : Hyper Text Transfer Protocol
What is a transfer protocol: Defined, when the client and the server communicate, the format
characteristics of the data sent :
(1) Advanced protocol based on TCP/IP
(2) Default port number: 80
(3) Based on the request/response model: one request corresponds to one response
(4) Stateless: each request is independent of each other, and data cannot be exchanged.
Historical version:
1.0: A new connection is established for each request and response (each The CSS of the web page and each picture are a request)
1.1: Reuse connection (after receiving the response, it will wait a while, if there are still requests to send, just use the previous one, if not, then disconnect)

Request message data format

1. Request line
Request method Request url Request protocol/version
GET /login.html HTTP/1.1

Request method:
There are 7 request methods in the HTTP protocol, and 2 commonly used
GET:
(1) The request parameters are in the request line, after the url
(2) The requested url length is limited
(3) Not safe
POST:
( 1) The request parameters are in the request body
(2) The requested URL length is not limited
(3) Relatively safe

2. Request header: The browser tells the server some information
Request header name: Request header value
Common request headers:
(1) User-Agent: The browser tells the server that I am accessing the version information of the browser you are using.
Function: It can be on the server side Get the information of this header and solve the compatibility problem of the browser
(2) Referer: http://localhost/login.html
tell the server where I (the current request) comes from.
Function: anti-hotlinking and statistics
3. Request blank line
Blank line. It is used to separate the header and body of a POST request.
4. The request body (body)
encapsulates the request parameters of the POST request message

Guess you like

Origin blog.csdn.net/qq_42524288/article/details/103721948