content
Set the response timing of the servlet:
Servlet in server: singleton, thread unsafe
3. servlet inheritance relationship
1. Three abstract methods in the Servlet interface
The solution to http statelessness: session tracking
6. Internal server forwarding and redirection
1. Server internal forwarding:
1. Set the encoding
In the xml file configuration, a servlet can correspond to multiple mappings. The meaning is that you can see which path the user uses to access
a mapping. It can only correspond to one servlet
before tomcat8:
(1. If it is a post delivery method:
request.setCharacterEncoding( "UTF-8");
(2. If it is the get transfer method
1. First convert the data into a byte array
String name = request.getParameter("name");
byte[] by = name.getBytes("ISO-8859 -1");
2. Convert the byte array into a string according to the set encoding format
name = new String(by,"UTF-8");
After tomcat8:
only for the post method: request.setCharacterEncoding("UTF -8 -8");
2. servlet life cycle
The server is turned on and the server is turned off.
By default:
when the request is accepted for the first time, the servlet will be initialized (init()), instantiated (servce()),
and then every time it refreshes and closes the web page, it calls servce() The service method is
until the server is shut down, the life cycle of the servlet ends, and when the destroy method (destory())
is requested for the first time, tomcat will be instantiated, initialized, and then serviced, and then each service is executed by this instance object.
Advantages :
Create objects only once, improve efficiency and reduce server burden
Disadvantages :
The first service takes a long time
- if you need to improve the startup speed of the system, the current default state is fine.
- If you want to improve the response speed of the servlet, you should set the response time of the servlet.
Set the response timing of the servlet:
Set <load-on-startup>1</load-on-startup> in the servlet in xml to set the time
of initialization and instantiation. The smaller the time, the earlier the time of initialization and instantiation.
With this tag, initialization and instantiation The instantiation is executed when tomcat starts.
When the first user accesses, the direct service will not be initialized and instantiated, the startup speed will be slower, but the service response speed will become faster, improving the user experience
Servlet in server: singleton, thread unsafe
Singleton: There is only one servlet instance
. Thread unsafe: When multiple threads access, if one thread modifies a variable in the servlet, it may cause the logical path of other threads to change.
Therefore, do not define logic-related in the servlet class. variable, and do not modify the variable
3. servlet inheritance relationship
HttpServlet abstract class Subsequent name ——> javax.servlet.GenericServlet abstract class implementation ——> Servlet interface
1. Three abstract methods in the Servlet interface
init(ServletConfig var1);Initialization method
void service(ServletRequest var1, ServletResponse var2);Service method
void destroy();Destruction method
2. The methods in the javax.servlet.GenericServlet abstract class implement three abstract methods in Servlet, But it is still an abstract method. 3. The method
in the interface is implemented in the HttpServlet abstract class;
404: Error and the corresponding page is not found
405: The default method of method is doget. Error 405, method error
500: internal server error
200: normal response
302: Redirect
4. Http protocol
http protocol: hypertext transfer protocol
http is stateless
http including request and response
1. request: including 1). request line 2). request header 3). request body
1). request line:
1. request method
2. request url
3. Requested protocol: (usually http1.1)
2). Request header:
including the browser version, port number, request content, etc. that the client wants to send from the server
3). Request body:
1). Get method : No request body with queryString
2). Post method: With request body form data
3). JSON method: request payload
2. Response:
1). The response line
includes: protocol, response code (200), response status
2). Response The header
includes: server information (version information, ), the size of the response content, format, etc.
3). Response body
The content of the specific response and the specific page information.
5. Session
http is stateless:
It means that the server cannot distinguish whether the request comes from the same client
or not. It is stateless: confusion may occur, and the server responds to the request sent by client A to client B.
The solution to http statelessness: session tracking
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
HttpSession id = req.getSession();
System.out.println("创建的session id为:"+id);
}
- When the server requests a client, it will assign a session ID, sessionId to the client, each client has a unique id, and return the created id to the client
- The next time the client accesses the server, it will respond with sessionId The result is returned to the responding client, thus solving the confusion.
Session save scope:
Different sessions cannot access the content saved by other sessions
. The scope of the session: from session creation to browser closing.
6. Internal server forwarding and redirection
1. Server internal forwarding:
//测试内部请求转发
public class DemoServlet02 extends HttpServlet {
@Override
protected void service(HttpServletRequest requuest, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo02....");
}
}
public class DemoServlet01 extends HttpServlet {
@Override
protected void service(HttpServletRequest requuest, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo01....");
//demo01跳转到demo02
requuest.getRequestDispatcher("demo02").forward(requuest,response);
}
}
2. Client redirection:
public class DemoServlet01 extends HttpServlet {
@Override
protected void service(HttpServletRequest requuest, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo01....");
//测试内部请求转发
// requuest.getRequestDispatcher("demo02").forward(requuest,response);
//测试重定向
response.sendRedirect("demo02");
}
}
public class DemoServlet02 extends HttpServlet {
@Override
protected void service(HttpServletRequest requuest, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo02....");
}
}