Table of contents
7. Servlet urlParrten configuration
1. JavaWeb technology stack
B/S architecture: Browser/Server, browser/server architecture mode, its characteristic is that the client only needs a browser, and the logic and data of the application are stored on the server. The browser only needs to request the server to obtain web resources, and the server sends the web resources to the browser.
Benefits: Easy maintenance and upgrade: After the server is upgraded, the client can use the new version without any deployment
Static resources: HTML, CSS, JavaScript, images, etc. Responsible for page display
Dynamic resources: Servlet, JSP, etc. Responsible for logic processing
Database: responsible for storing data
HTTP protocol: defining communication rules
Web server: responsible for parsing HTTP protocol, parsing request data, and sending response data
Two, HTTP
1 Overview
Concept: HyperText Transfer Protocol, hypertext transfer protocol, specifies the rules for data transmission between browsers and servers
Features of the HTTP protocol:
1. Based on the TCP protocol: connection-oriented, secure
2. Based on the request-response model: one request corresponds to one response
3. The HTTP protocol is a stateless protocol: it has no memory for transaction processing. Each request-response is independent.
Disadvantages: Data cannot be shared between multiple requests. (cookie and session can solve this problem)
Advantages: fast
2.HTTP request data format
Request Line: The first line of the request data. Among them, GET indicates the request method, / indicates the request resource path, and HTTP/1.1 indicates the protocol version.
Request header: the second line starts, and the format is key: value.
Request body: The last part of the POST request, storing request parameters
GET / HTTP/1.1
Host: www.itcast.cn
Connection: keep-alive
User-Agent: Mozilla/5.0 Chrome/91.0.4472.106
…
POST / HTTP/1.1
Host: www.itcast.cn
Connection: keep-alive
Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 Chrome/91.0.4472.106
username=superbaby&password=123456
The difference between a GET request and a POST request:
The GET request request parameters are in the request line, and there is no request body. POST request request parameters in the request body
GET request request parameter size is limited, POST does not
Common HTTP request headers:
Host: Indicates the host name of the request
User-Agent: Browser version, for example, the logo of Chrome browser is similar to Mozilla/5.0 ... Chrome/79, and the logo of IE browser is similar to Mozilla/5.0 (Windows NT ...) like Gecko; Accept: Indicates the resource type that the browser can receive, such as text/*, image/* or */* means all; Accept-Language: Indicates the language preferred by the browser, the server can According to this, web pages in different languages are returned; Accept-Encoding: Indicates the compression type supported by the browser, such as gzip,
deflate
,
etc.
3.HTTP response data format
The response data is divided into 3 parts:
Response line: The first line of the response data. Among them, HTTP/1.1 indicates the protocol version, 200 indicates the response status code, and OK indicates the status code description. Response
header: the second line starts, and the format is key: value.
Response body: The last part. store response data
HTTP/1.1 200 OK
Server: Tengine
Content-Type: text/html
Transfer-Encoding: chunked…
<html>
<head>
<title></title>
</head>
<body></body>
</html>
Common HTTP response headers:
Content-Type: Indicates the type of the response content, such as text/html, image/jpeg; Content-
Length: Indicates the length (number of bytes) of the response content; Content-
Encoding: Indicates the response compression algorithm, such as gzip; Cache-
Control: Indicates how the client should cache, for example, max-age=300 means that it can be cached for up to 300 seconds
3. Tomcat
1. Web server
A web server is an application program (software) that encapsulates the operations of the HTTP protocol, so that programmers do not need to directly operate the protocol, making web development more convenient. The main function is to "provide online information browsing services"
2.Tomcat
Official website: https://tomcat.apache.org/
effect:
Encapsulate HTTP protocol operations to simplify development
The web project can be deployed to the server to provide online browsing services to the outside world
Tomcat is a lightweight Web server that supports Servlet/JSP with a small amount of JavaEE specifications, also known as Web container, Servlet container
Use other methods to learn by yourself later
4. Servlets
1 Overview
Servlet is a dynamic web resource development technology provided by Java
Servlet is one of the JavaEE specifications, which is actually an interface. In the future, we need to define the Servlet class to implement the Servlet interface, and run the Servlet by the web server
2. Quick Start
①Create a web project and import Servlet dependencies
② Define a class to implement the Servlet interface, and rewrite all the methods in the interface, you need to define the implementation method in the service method
③Use the @WebServlet annotation on the class to configure the access path of the Servlet
④Start Tomcat, enter the URL+access path in the browser, and access the Servlet
3. Servlet execution process
http://localhost:8080 + /project name + /Servlet access path
Servlets are created by web servers and servlet methods are called by web servers
The server calls the service method in the Servlet
4. Servlet life cycle
Servlet runs in the Servlet container (web server), and its life cycle is managed by the container, which is divided into 4 stages:
1. Loading and instantiation: By default, when the Servlet is accessed for the first time, the Servlet object is created by the container
2. Initialization: After the Servlet is instantiated, the container will call the Servlet's init() method to initialize the object, and complete some initialization tasks such as loading configuration files and creating connections. This method is called only once
3. Request processing: Every time a Servlet is requested, the Servlet container will call the Servlet's service() method to process the request.
4. Service termination: When the memory needs to be released or the container is closed, the container will call the destroy() method of the Servlet instance to complete the release of resources. After the destroy() method is called, the container will release the Servlet instance, which will then be recycled by Java's garbage collector
5. Servlet method
Initialization method, executed when the Servlet is created, only executed once
void init(ServletConfig config)
Provide a service method, which will be called every time the Servlet is accessed
void service(ServletRequest req, ServletResponse res)
Destroy method, when the Servlet is destroyed, this method is called. Destroy Servlet on memory deallocation or server shutdown
void destroy()
Get the ServletConfig object
ServletConfig getServletConfig()
Get Servlet information
String getServletInfo()
6. Servlet architecture
We develop web projects with B/S architecture, all for HTTP protocol, so we customize Servlet, which will inherit HttpServlet
In HttpServlet, different methods are called according to different request methods
HttpServlet principle
In the HTTP protocol, the data format of the GET and POST request methods is different. If you want to process the request parameters in the Servlet in the future, you must judge the request method in the service method, and process it separately according to the different request methods:
//获取请求方式
String method = req.getMethod();
//判断请求参数,不同请求方式,进行不一样的处理逻辑
if("GET".equals(method)){
//执行GET请求方式的处理逻辑
doGet(req, resp);
}else if("POST".equals(method)){
//执行POST请求方式的处理逻辑
doPost(req, resp);
}
HttpServlet usage steps:
① Inherit HttpServlet
② Rewrite the doGet and doPost methods
7. Servlet urlParrten configuration
To be accessed, the Servlet must configure its access path (urlPattern)
A Servlet can be configured with multiple urlPatterns
//例如:
@WebServlet(urlPattern = {"/demo1","/demo2"})
urlPattern matching rules:
① Exact match: configuration path and access path are exactly the same
② Directory matching: the configuration path ends with ' /* '
③Extension matching
④ Any matching
Precedence: exact path > directory path > extension path > /* > /