JavaEE - servlet details

Disclaimer: The materials used in this column are written by VIP students of Kaige Academy. Students have the right to remain anonymous and have the right to final interpretation of the article. Kaige Academy aims to promote VIP students to learn from each other based on public notes.

Servlet life cycle
Servlet has only one object in the entire web project.
Servlet cannot be created by itself. It is created by the server.

The servlet life cycle is divided into three phases:
1. The init() method is called in the initialization phase;
2. The service() method is called in the response to the client request phase;
3. The destroy() method is called in the termination phase.

There are two ways in the initialization phase:
1. When the object is first accessed, the object is created.
2. You can set the servlet object to be initialized after the server is started. (Add loadonstartup=1 in the comments)

Two ways to destroy sevlet
1. Web project redeployment (copy in)
2. Normal tomcat shutdown

image

The http protocol is the hypertext transfer protocol, and the underlying protocol used is tcp. Both thhp and https are developed on the basis of tcp.

image

Both the request header and the response header are created by the server, and the server group writes code to implement the interface.
Tomcat's optimization of request and response

image


Common methods of HttpServletRequest request
1. Obtain client information The
getRequestURL method returns the complete URL when the client sends a request.
The getRequestURI method returns the resource name part of the request line.
The getQueryString method returns the parameter part in the request line.
The getPathInfo method returns additional path information in the request URL. The extra path information is the content in the request URL after the servlet's path and before the query parameters, which starts with "/". The getRemoteAddr method returns the IP address of the requesting client.
The getRemoteHost method returns the full host name of the requesting client.
The getRemotePort method returns the network port number used by the client.
The getLocalAddr method returns the ip address of the web server.
The getLocalName method returns the hostname of the web server.

2. Get the client request header
getHeader(string name) method: String
getHeaders(String name) method: Enumeration
getHeaderNames() method

3. Get client request parameters (data submitted by client)
getParameter(String) method (commonly used)
getParameterValues(String name) method (commonly used)
getParameterNames() method (not commonly used)
getParameterMap() method (commonly used when writing frameworks)

image

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326645319&siteId=291194637