- Servlet main data structure
Servlet Interface: Primarily defines the servlet's life cycle approach
ServletConfig interface: Provides a number of important objects and methods for the servlet to use the container service.
ServletContext interface: Is the context object of the servlet that was created when the server was started and provides a number of important ways for the servlet to use the container service.
Genericservlet Abstract class: Provides a generic implementation for Servlets (including implementations of Servlet and ServletConfig two interfaces), An important object that holds a servletconfig type that the container passes to the servlet through the Init method.
HttpServlet Abstract class: Provides a generic implementation for servlets that handle HTTP requests, primarily by defining and implementing several service methods.
- Servlet Inheritance Relationship
The most important thing in the servlet program is the Servlet interface, which defines a subclass of Genericservlet under this interface, but generally does not inherit this class directly, but chooses Genericservlet subclass inheritance based on the protocol used. In general, we use HTTP protocol processing, so generally when we need to use the HTTP protocol operation, our custom servlet will inherit the HttpServlet class. The specific inheritance relationship is as follows:
Servlet-->genericservlet-->httpservlet--> our own servlet.
OK, now take a look at the Servlet interface:
public interface Servlet
This interface defines a Servlet: a Java class that inherits this functionality on a WEB server.
1. Init
public void init (ServletConfig config) throws servletexception;
The servlet engine calls the Init method exactly before it is placed into the service after the servlet is instantiated. The Init method must exit successfully before invoking the service method. If the Init method throws a servletexception, you cannot place the servlet into the service, and if the Init method does not complete within the timeout range, we can also assume that the servlet is not functional and cannot be placed into the service.
2. Service
public void Service (ServletRequest request, servletresponse response) throws Servletexception, IOException;
The servlet engine calls this method to allow the servlet to respond to requests. This method cannot be called until the Servlet is initialized successfully. The servlet engine can block pending requests before the servlet is initialized. After a Servlet object is unloaded until a new servelt is initialized, the servlet engine cannot call this method
3, Destroy
public void Destroy ();
The servlet engine calls this method when a servlet is removed from the service. The Destroy method cannot be called when the service method of this object does not fully exit all threads or is not considered by the engine to have a timeout operation.
4, Getservletconfig
Public ServletConfig getservletconfig ();
Returning a ServletConfig object, as a Servlet developer, you should store the ServletConfig object through the Init method so that this method can return the object. For your convenience, Genericservlet has done this when executing this interface.
5, Getservletinfo
Public String getservletinfo ();
Allows the servlet to provide information about itself to the host's servlet runner. The returned string should be in plain text format and should not have any flags (such as html,xml, etc.).
Package Linkin;import Java.io.ioexception;import Java.io.printwriter;import javax.servlet.servlet;import Javax.servlet.servletconfig;import Javax.servlet.servletexception;import Javax.servlet.servletrequest;import Javax.servlet.servletresponse;public class Linkinservlet implements Servlet{public void Destroy () {SYSTEM.OUT.PRINTLN ("The method of destruction is called ... ");} Public ServletConfig Getservletconfig () {return null;} Public String Getservletinfo () {return null;} public void init (ServletConfig arg0) throws Servletexception{system.out.println ("initialization method called ... ");} public void Service (ServletRequest arg0, Servletresponse arg1) throws Servletexception, IOEXCEPTION{//1. Writing output statements, proves that the current method is called SYSTEM.OUT.PRINTLN ("process request, return response ... //2. Send a response message to the browser via the PrintWriter object PrintWriter writer = Arg1.getwriter (); Writer.write ("Success jump ... "); Writer.close ();}}
<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "><display-name></display-name>< Welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><servlet ><servlet-name>LinkinServlet</servlet-name><!--Registering a Servlet implementation class with the full class name--><servlet-class> Linkin. linkinservlet</servlet-class></servlet><!--Establish a mapping relationship from the virtual path to the servlet component--><servlet-mapping ><!--referencing the servlet component name--><servlet-name>linkinservlet</servlet-name><!--mapped to the virtual path of the servlet: "/ Linkinservlet "--><url-pattern>/linkinservlet</url-pattern></servlet-mapping></web-app >
Servlet--servlet Interface