ervlet summary
1. Servlet is responsible for processing user requests on the server side. When the client sends a request to the server, the server finds the corresponding Servlet through the web.xml configuration file to process the request. A servlet must be bound to a URL, and users access the servlet by accessing the URL.
2. Three ways to implement servlet
Java.servlet.Sertvlet class
Java.servlet.GenericServlet class
Java.servlet.HttpServlet class
3.serv in let interface Method
//Initialization
void init (ServletConfig config)
void service (ServletRequest request, ServletResponse response)
//Destruction
void destroy()
String getServletInfo()
ServletConfig getServletConfig ()
The client sends a request, and the server will find the corresponding Servlet through the URL. If the Servlet object does not exist, the server will create the Servlet object
After creating the object, the server immediately calls init (ServletConfig config) to complete the initialization work. The server calls the Servlet's service (ServletRequest req, ServletResponse res) method. At this time, it needs to pass the parameters request and response to it.
When the client accesses this Servlet again, because the Servlet object already exists, it will not be created. Servlet is a singleton.
(Note: The singleton pattern is a commonly used software design pattern. Its core structure only contains a special class called a singleton class. The singleton pattern can ensure that there is only one instance of a class in the system and This instance is easy to access from the outside, which facilitates control of the number of instances and saves system resources. If you want only one object of a certain class to exist in the system, the singleton mode is the best solution)
When the server needs it. When destroying the Servlet object, destroy() will first be called to complete the release of some resources, and then destroy the object
4. Configure the servlet in web.
HelloWorldServlet
The getServletContext(), getInitParameter() and other methods in the class can be used directly. If you want to do some initialization parameter work,
Don’t override the method init (ServletConfig config), but override the method init()
HttpServlet: It is a Servlet class specially used to handle Http protocol requests. We can implement Servlet by inheriting HttpServlet
This is the simplest way. We will do this in the future, because the request sent by the client only has get and post. So we only need to rewrite these two methods.
7.HttpServlet: It is a Servlet class specially used to handle Http protocol requests. We can implement Servlet by inheriting HttpServlet
This is the simplest way, we will do this in the future, because the client There are only two methods of sending requests: get and post. So we only need to rewrite these two methods.