Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

The Life Cycle of a Servlet in Java

Servlet life cycle management is the most vital one. A Servlet is handled via an obvious life cycle that characterizes the way it is loaded, instantiated ad initialized, handles requests from clients and the means by which it is taken out of service.

As we all know that Servlets come in the shape of Java objects hence there are plenty of variations as far as loading and unloading of them is concerned. The time when a server decides to load a specific Servlet, standard java class loading techniques are made use of to come up with a class instance. Making use of the same allows the Servlets to be loaded from any part of the network.

  • In cases where the server has established a connection with the internet, Servlets can be loaded from any corner of the world.

The server uses the init method of the Servlets to load the same. Prior to the client requests are taken care of, the initialization progression gets over and even before the Servlet is shattered.

Here is a simple and brief explanation on the process

Step1: Server is loaded and the Servlet is initiated.

Step2: There will be either no requests or quite a number of requests that the Servlet will be taking care of.

Step3: Servlet is removed by the Server. In some cases this step is performed only after the server gets shut down.

How the Servlet Work

The real loading and implementation of the Servlets is clear-cut enough--when a client association is established and the Servlet is not in memory, it is loaded. However, a Servlet can be loaded if it has not yet been tackled with. A servlet can be loaded moreover at start of the server or vigorously when it is contacted.

Refer to the below for the signature of the method adopted to load and unload the Servlet.

public void init(ServletConfig config) throws ServletException
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
public void destroy()

The Servlet persists in the server, and is not reinitialized for each request. The life cycle of a Servlet is:

init()
* process individual requests
* if persistence is necessary, handle processed information
* destroy()
*
* @see javax.servlet.GenericServlet#init()
*/
public void init() {
xqueryMap = new HashMap();
indentationProperty = new Properties();
indentationProperty.setProperty("indent", "yes");
try {
dataSource = new DDXQDataSource(new
FileInputStream(XQueryServlet.CONFIG_FILE));
connection = dataSource.getConnection();
}
catch (Exception exception) {
System.out.println("Could not initialize DataDirect " +
"XQuery due to an Exeption:");
exception.printStackTrace();
}
/**

Determination of Servlets loaded at the Start-up

In order to determine which Servlets are loaded at the start of the server, the java web server consists of the administration section where all this details are captured. This permits the Servlets to be prepared in memory for the first client request to come in.

“Servlets that are expected to be most commonly and frequently used are loaded or installed at the start of the server”.

The response time for all the requests at the time when the Servlet is loaded at the start of the server is kept to a minimum. This reduces the wait time for the Servlet to be loaded.

There can be situations when the unloading of a Servlet takes place just after the completion of the client requests and not like the usual method of doing the same which is after shut down of the server.

Multithreading issues

At the time of the request handing out phase, there can be numerous threads administering the service () method of a Servlet instance. The Servlet container can transmit several synchronized requests via the service method of the Servlet. Hence it is up to the developer to appropriately grip the concurrency.

  • In case the Servlet put into practice the SingleThreadModal interface, Servlet container assures that there will be simply single request thread at a time in service method. The Servlet container can persuade these requests by serializing requests to the service method or by preserving the team of Servlet instance.
  • On the other hand, in case the Servlet does not put into operation SingleThreadModal but any of service, doGet or doPost methods has been distinct with the corresponding keyword, the Servlet container cannot generate pool of Servlet instance but must serialize requests.

Making use SingleThreadModal and defining doGet, doPost or service methods with corresponding keywords is not appreciated due to performance factors.



I am a software developer from India with hands on experience on java, html for over 5 years.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login