Redirects and servlet life cycle

Redirect
(1) What is a redirect
The server notifies the browser to send a request to a new address.
Note: A 302 status code and a Location header can be sent. (The message header contains an address, which is called a redirection address). After the browser receives it, it will immediately send a request to the redirection address.
(2) How to redirect
response.sendRedirect(String url);
Note:
a.url is the redirection address.
b. Before redirecting, the container will first clear the data stored in the response object.
(3) Features
a. The redirection address is arbitrary.
b. After redirection, the address in the browser address bar will change.


1. Problems that need to be paid attention to when dealing with Chinese in the
database Some drivers in the database will use "iso-8859-1" to encode by default, and garbled characters will occur for Chinese.
Solution:
mysql:
jdbc:mysql://localhost:3306/database name?
useUnicode=true&characterEncoding=utf-8  2. servlet

life cycle
(1) what is the life cycle of
servlet, how does the container create servlet objects, and how to deal with them The whole process of doing initialization processing, how to call its methods to handle requests, and how to destroy the object.
(2) What are the stages of the life cycle
1) Instantiation
a. What is instantiation?
The container calls the servlet's constructor to create the corresponding object.
b. When is it instantiated?
Case 1: After the container receives the request.
Case 2: After the container is started, it is created immediately.
The <load-on-startup></load-on-startup>
parameter value is required to be an integer greater than or equal to 0. The smaller the value, the higher the priority (ie, the first creation).
Note: The container will only create one instance.
2) Initialization
a. What is the initialization
container calls the init method of the servlet object. This method will only be executed once.
How to implement the init method of b.GenericServlet.
Save the easily passed ServletConfig object, and provide a method (getServletConfig) to get the object.
c. Only need to override the init() method provided by override GenericServlet.
d. Initialize parameter
step1, configure
<init-param>
<param-name>company</param-name>
<param-value>Northwestern Polytechnical University</param-value>
</init-param>
step2, read
String ServletConfig .getInitParameter(String paramName);
3) Ready
4) Destroy
(3) Related and various interfaces
1) Servlet interface
init
service
destory 
2) The GenericServlet abstract class
implements some methods in the Servlet interface (init, destory)
3) The HttpServlet abstract class
inherits the GenericServlet abstract class and implements the service method.

3. How the container handles the request resource path
For example, enter in the address bar of the browser: http://ip:port/web04-3/abc.html
step1, the container finds the application location based on the application name ("/web04-3"). folder.
step2, the container will think that the call is a servlet by default, go to web.xml to find out if there is a servlet that matches "/abc.html".
(1) Exact matching
<url-pattern>/abc.html</url-pattern>
(2) Wildcard matching
Use "*" to match 0 or more characters, such as <url-pattern>/*</url-pattren >
(3) Suffix matching
starts with "*." followed by multiple characters, such as <url-pattern>*.do</url-pattren> will match all requests ending with .do.
step3, if there is no matching servlet, the container will look for the corresponding file.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325279091&siteId=291194637