Servlet
- Java programs running on a Web server or application server
- Create dynamic content (effective and powerful) solutions on the Web
- The container to manage the lifecycle "load (create) program, initialize, service, terminate (destroy), uninstall" and interact with the server
- Features by Sun specification
Servlet life cycle
- Load: Specifies the location (package and class name) of the servlet through the Web. XML configuration, and after success, it is instantiated by the reflection servlet.
- Initialize: Invokes the Init () method to initialize the instantiated object. Do some initialization work (establish database link, read source file information, etc.) before processing the request. Initial failure, directly uninstalling the servlet.
- Service: invokes the Service () method to process the request. ServletRequest processes the request, Servletresponse sets the response information.
- Destroy: The Web container automatically calls the Destroy () method when the servlet is removed from the container.
- Uninstall: After the servlet calls the Destroy () method, this instance is reclaimed by the garbage collector and needs to be reused again to initialize Init ().
It is normally initialized, destroyed only once, and is called multiple times under special circumstances, such as if the servlet is not in use for a long time.
Servlet Basic Configuration
- Exact match:
<url-pattern>/servlet/MyServlet.do</url-pattern> '
- Directory Matching:
<url-pattern>/servlet/*</url-pattern>
- Name extension matches:
<url-pattern>*.do</url-pattern>
Container lookup: exact match > directory match (starting from longest directory match) > extension match
ServletContext: Application Context
只有一个,所有对象都可以访问它。(全局的)
ServletConfig
只有一个Servlet对应。作用域比ServletContext小。
Convert virtual path to physical path
getResourcePaths(java.lang.String path):getResourceAsStream(java.lang.String path):getResource(java.lang.String path)
Status line: HTTP protocol, status code, status description
How the browser sends requests to the server
- Browser Input URL Address
- Click the hyperlink
- Form submission
How the browser sends requests to the server
- Browser input URL address (GET method)
- Click the hyperlink (Get method)
- Form submission (Get, POST method)
Get, post method differences:
|
get |
post |
Data type |
Text |
Text, binary text |
Data length |
No more than 255 characters |
No Limits |
Visibility of data |
Displayed as part of the URL address in the browser address bar |
As the requested message body, not visible |
Data caching |
Cached in the browser URL history state |
Will not be cached by the browser |
HTTP request Settings Request redirection:
- You can select a different server-side program to process the request
- Redirect the request to a completely different URL
Request forwarding and request REDIRECT differences:
- Redirect is the client browser to complete, request forwarding is the server side to complete
- The URL address of the redirected browser will change, and request forwarding will not
- Redirect the client browser to make two requests and responses, and request forward only one request and response
To set up automatic refresh and wait pages: Response.setheader ("Refresh", "time; Url=url "); "Refresh" has a browser compatibility issue.
Web state Management state Management solution:
- Hidden Fields (Cons: Must be dynamic pages to be valid)
- Cookie (disadvantage: The data is saved on the client, the user can disable the cookie, and the cookie can be erased, visible, unsafe. )
- session (sessions, stored on server): Extinction->1. Timeout (can be set time) 2. Manual shutdown 3. System crashes when the Web server shuts down
Java EE Learning record 3