JavaWeb — Servlet(Server Applet)

Servlet(Server Applet)

Full name Java Servlet, No Chinese translation. It is written in Java Server-side program. Its main function is to interactively view and modify data, generate dynamic Web content.

Refers to a narrow Servlet interface to the Java language, broadly refers to any Servlet class implements the Servlet interface, under normal circumstances, people will understand Servlet for the latter.

Servlet running on Java-enabled application servers. From the implementation speaking, Servlet response to any type of request, but only for the most part extend Servlet Web server based on HTTP protocol.

Servlet modes of operation :

● receiving request data (client sends a request to the server)

● processing request (call server starts and Servlet, Servlet generated in response to the content request based on the client server and pass it)

● completion response (the server returns the response client)

Servlet API Overview:

① javax.servlet; contains contract between Servlet and Servlet containers defined classes and interfaces.

② javax.servlet.http; which comprises define the relationships between the HTTP Servlet and Servlet containers.

③ javax.servlet.annotation; wherein the label comprises a Servlet, Filter, Listener labels. It also defines data element annotation.

④ javax.servlet.descriptor; contains configuration information provider of log on the Web application type.

Servlet use: servlet three ways servlet interfaces

Servlet technology is the core of Servlet, it is an excuse for all Servlet class must directly or indirectly to achieve. In preparing the realization of Servlet Servlet class, directly implement it. When extension implementation of this interface, it indirectly.

Servlet working principle :

● Servlet interface defines Servlet with a contract between the Servlet container. This contract is: Servlet container Servlet class loaded into memory, and produces Servlet examples and call its specific method. However, to note that, in an application, each Servlet type can only have one instance.

● user requests resulting Servlet container Servlet mobilization of Servic () method, passing in a ServletRequest object and a ServletResponse object. Both objects are made Servlet container (such as Tomcat) packaged, the programmer does not need to achieve, a programmer can directly use these two objects.

ServletRequest encapsulates current Http request, and therefore, the developer does not have to parse and manipulate Http original data. 

ServletResponse Http response indicates that the current user, the programmer only be able to directly manipulate objects ServletResponse response back to the user

For each application, Servlet container also creates a ServletContext object. This object encapsulates details environmental context (application). Each application has only one ServletContext. Each object can also have a Servlet Servlet ServletConfig object configuration package.

 

The method defined in Servlet interface:

Package the javax.servlet;
 Import java.io.IOException;
 public  interface the Servlet {
     // initialize the servlet, which is the servlet lifecycle methods called by a web container 
    void the init (the ServletConfig var1) throws ServletException;
     // for incoming requests provide, every request which is called by the Web container 
    void -Service (the ServletRequest var1, var2 the ServletResponse) throws ServletException, IOException;
     // immediately be called once, and table names being destroyed servlet 
    void the destroy ();
     // returns ServletConfig object 
    getServletConfig the ServletConfig ();
     // returns information about the servlet, such as author, copyright, version, etc. 
    String getServletInfo (); 
}

Servlet life cycle :

The init ① () , when first requested Servlet, Servlet containers will begin to call this method to initialize a Servlet object, but this method will not be called again in subsequent requests Servlet container , just like people only " born "once the same. We can use the init () method to perform the corresponding initialization. When you call this method, Servlet containers will be passed in order to come to a ServletConfig object Servlet object is initialized .

Service ② () , each time a request Servlet, Servlet container will call this method . Like people, you need to constantly accept the boss's instructions and "work." The first request, Servlet containers will first call the init () method to initialize an object out, then it calls its Service methods work, but Servlet container subsequent requests will only call the service method.

The destroy ③ () , when to destroy Servlet, Servlet container will call this method . Like everyone else, to the stage it will die. When you uninstall the application or turn off the Servlet container, it will happen, in this method generally will write some cleanup code.

 
 
 

Guess you like

Origin www.cnblogs.com/Dm920/p/11695719.html