Java Web-servlet (5) Three ways to develop Servlets, configuration servlet details, servlet lifecycle (2)

Source: Internet
Author: User

Third, The life cycle of the Servlet

A Java servlet has a life cycle that defines how a servlet is loaded and initialized, how it receives a request and responds to the request, and how it is purged from the service. the life cycle of the Servlet is defined by the javax.servlet.Servlet interface.

     all java Servlet javax.servlet.servlet interface so that it can be in a servlet run in the engine. servlet engine is web server follow Java Servlet API custom extensions. servlet engine provides network services to understand mime request and provide a run servlet

The Javax.servlet.Servlet interface defines a method that is called at a specific time in the lifetime of the servlet and in a particular order.

Servlet life cycle:servlet load ---> instantiation ---> services ---> destroyed.

The servlet program is called by the Web serverafter theWeb server receives a Servlet Access request from the client:

(the web server resolves the host name,theWeb app, and then finds the resource, which is the XML query.)

) Perform the following procedure:

init () : Using the reflection mechanism, if the servlet init The method loads the instance into memory (called only once), which is initialized.

Service(): The Web Server encapsulates the received Http request to httpservletrequest Object (various information), passed in as a service parameter, theservice function is called multiple times (each time a Servlet is accessed , theservice will be called once)

The response(the information that responds to the server) is disassembled to form an http response format.

Destroy(): executes only once, and executes the method when the server-side stops and the Servlet is unloaded. when a Servlet object exits its lifecycle, it is responsible for releasing the resource that is occupied. A Servlet may produce additional threads when running the service () method, so it is necessary to confirm that the call to destroy () method, these threads have been terminated or completed.

The main function of a Servlet is to interactively browse and modify data to generate dynamic Web content.

The process is summarized as follows:

(1) The client sends the request to the server side.

(2) The server requests the information content and passes it to the server.

(3) The Servlet generates the response content and passes it to the Server.

(4) The response content is generated dynamically, usually depending on the client's request.

(5) The server returns the response to the client.

Iv. succession of Genericservlet

The difference between inheriting Genericservlet and Implementing a Servlet interface is to put the init() and destroy () encapsulated in the service abstraction method, that is, inheriting genericservlet can only implement one method. The other steps are the same as implementing the Servlet interface.

To add a file to the classes directory, create the file Mygenericservlet.java code as follows:

Package com.focus;

Import javax.servlet.*;

Import javax.servlet.http.*;

Import java.io.*;

public class Mygenericservlet extends Genericservlet

{

public void Service (ServletRequest request, Servletresponse

Response) throws Servletexception, IOException

{

Response.getwriter (). println ("Hello World!!!!!");

}

}

Configure the Servlet

<servlet>

<servlet-name>MyGenericServlet</servlet-name>

<display-name>MyGenericServlet</display-name>

<servlet-class>com.focus.MyGenericServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>MyGenericServlet</servlet-name>

<url-pattern>/Generic</url-pattern>

</servlet-mapping>

In the browser input:

The effect is as follows:

V. Succession of Httpservle

To add a file to the classes directory, create the file Myhttpservlet.java code as follows:

Package com.focus;

Import javax.servlet.*;

Import javax.servlet.http.*;

Import java.io.*;

public class Myhttpservlet extends HttpServlet

{

protected void DoPost (HttpServletRequest request,

HttpServletResponse response) throws Servletexception, IOException

{

Response.setcharacterencoding ("GBK");

Response.getwriter (). println (" Welcome " +request.getparameter ("username") + " visit this website ");

}

protected void doget (HttpServletRequest request,

HttpServletResponse response) throws Servletexception,ioexception

{

Response.getwriter (). println ("I am Doget");

}

}

Configure the Servlet

<servlet>

<servlet-name>MyHttpServlet</servlet-name>

<display-name>MyHttpServlet</display-name>

<servlet-class>com.focus.MyHttpServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>MyHttpServlet</servlet-name>

<url-pattern>/MyHttpServlet</url-pattern>

</servlet-mapping>

Create a go.html code under the servlettest directory as follows:

<title> Login Page </title>

<body>

<form action= "/servlettest/myhttpservlet" method= "POST" >

Name:<input type= "text" name= "username"/><br/>

Password:<input type= "password" name= "Userpass"/><br/>

<input type= "Submit" value= " submission " />

</form>

</body>

In the browser input:

The effect is as follows:

Prove that httpservlet default call is doget

In the browser input:

The effect is as follows:

Press the submit button to jump to the page

Java Web-servlet (5) Three ways to develop Servlets, configuration servlet details, servlet lifecycle (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.