1. What is a servlet
- A servlet is a Java class that inherits the HttpServlet class
- Servlets must be deployed on the Web server side to handle requests from clients
2.Servlet Operating Process
- The Web Client makes an HTTP request to the servlet container (Tomcat)
- The servlet container receives a request from the Web client
- The servlet container creates a HttpRequest object that encapsulates the information requested by the Web client into this object.
- Servlet container creates a HttpResponse object
- The servlet container invokes the service method of the HttpServlet object, passing the HttpRequest object and the HttpResponse object as arguments to the HttpServlet object.
- HttpServlet invokes the method of the HttpRequest object to obtain the HTTP request information. (In the service method of the servlet)
- HttpServlet calls the methods of the HttpResponse object to generate the response data. (In the service method of the servlet)
- The servlet container passes the results of the HttpServlet response to the Web Client.
If you want to separate the Get and post requests for the corresponding client, you need to override the Doget and Dopost methods of the HttpServlet object, and if all the requests are the same, you only need to override the service method.
creation of 3.Servlet
There are two opportunities to create a servlet instance:
- When a client requests a servlet for the first time, the container creates the servlet instance (most of the servlets are created in this way).
- A servlet instance, the Load-on-startup servlet, is created as soon as the Web app starts.
About the Load-on-startup parameter:
- The Load-on-startup element flags whether the container loads the servlet at startup (instantiates and invokes its init () method).
- Its value must be an integer that indicates the order in which the servlet should be loaded
- A value of 0 or greater than 0 o'clock indicates that the container loads and initializes the servlet when the app is started;
- When the value is less than 0 or is not specified, the container will not load until the servlet is selected.
- The lower the value of a positive number, the higher the precedence of the servlet, and the more loaded it will be when the app starts.
- At the same time, the container will be loaded in its own order of choice.
life cycle of 4.Servlet
- Instantiation: Container Creation Servlet Object
- Initialize: Call the servlet's init () method
- Service: invokes the Service () method of the Servlet instance to process the request
- Destroy: The container deletes the servlet instance (invoking the Destroy () method of the servlet instance)
Configuration of 5.Servlet
In order for the servlet to respond to the client's request, the servlet must also be configured in the Web app, and when the servlet is configured, it needs to be modified.
<servlet> <Servlet-name>Servletdemo</Servlet-name> <Servlet-class>Cn.dragon.servlet.ServletDemo</Servlet-class> </servlet> <servlet-mapping> <Servlet-name>Servletdemo</Servlet-name> <Url-pattern>/*</Url-pattern> </servlet-mapping>
Where the value of the Servlet-name property must be consistent in order to guarantee response to client requests.
6.Servlet and JSP
The nature of the JSP is the servlet, and after we deploy the well-written JSP file in the Web container, the Web container compiles the JSP into the corresponding servlet.
If the servlet is to generate a presentation layer page, all HTML tags need to be output using the servlet's output stream, which is cumbersome and detrimental to the artist's modification.
In the standard MVC pattern, the servlet is used only as a controller, and the JSP is used only as a presentation layer technique:
- M:model, i.e. model, corresponds to JavaBean.
- V:view, which is the view, corresponds to the JSP.
- C:controller, which is the controller, corresponds to the servlet.
Reference:
Http://www.cnblogs.com/xdp-gacl/p/3760336.html
Http://www.cnblogs.com/xuekyo/archive/2013/02/24/2924072.html
Http://www.cnblogs.com/goody9807/archive/2007/06/13/782519.html
Http://www.blogjava.net/xzclog/archive/2011/09/29/359789.html
Reprint please indicate the source
Javaweb Learning-servlet