Tuesday, September 4, 2012

Servlet Methods





In the java servlet life cycle, the first phase is called ‘Creation and intialization’.
The java servlet container first creates the servlet instance and then executes the init() method. This initialization can be done in three ways. The default way is that, the java servlet is initialized when the servlet is called for the first time. This type of servlet initialization is called lazy loading.

The other way is through the <load-on-startup>non-zero-integer</load-on-startup> tag using the deployment descriptor web.xml. This makes the java servlet to be loaded and initialized when the server starts. This process of loading a java servlet before receiving any request is called preloading or preinitialization of a servlet.
Servlet are loaded in the order of number(non-zero-integer) specified. That is, lower(example: 1) the load-on-startup value is loaded first and then servlet with higher values are loaded.
Example usage:

<servlet>
       <servlet-name>Servlet-URL</servlet-name>
       <servlet-class>com.javapapers.Servlet-Class</servlet-class>
       <load-on-startup>2</load-on-startup>
</servlet>

-----------------------------------------------------------------------------------------------

init(
ServletConfig)

Initializes the servlet.


service(ServletRequest, ServletResponse)

Carries out a single request from the client


 destroy()

Cleans up whatever resources are being held (e.g., memory, file handles, threads) and makes sure that any persistent state is synchronized with the servlet's current in-memory state.


 getServletConfig()

Returns a servlet config object, which contains any initialization parameters and startup configuration for this servlet.


 getServletInfo()

Returns a string containing information about the servlet, such as its author, version, and copyright.


 -----------------------------------------------------------------------------------------------

Question: What will happen if the destroy() method is called in the init() method of a Servlet?

Answer: Nothing strange! The destroy() method will simply be called/executed and the control will return back to the next instruction in the init() method.

One of the misconceptions about the destroy() method is that it destroy the servlet object and hence one can think what will happen to the init() method, which is still under execution on an object (the servlet object) which just got destroyed. Huh! as I mentioned that it's a misconception and destroy() method doesn't really destroys the servlet object. Instead it is called just before the Web Container (Servlet Engine to be precise) removes the servlet object from the memory while unloading the servlet. BTW, destroy() method is NOT supposed to be called by the servlet writer. Though you can call it like any other method, but it's not a good programming practice to do this.

So, if we call the destroy() method from init() then the code written in the destroy() method will be executed and after the completion of the execution of this method, the control will return to the next instruction of the init() method. If you've NOT overriden destroy() method to put some custom code then it won't be a problem as by default it does nothing, but if you have put some code then you may end up doing something unexpected. 

For example: Suppose you are reading some value in the init() method from a file stored on the disk and in the destroy()method you're writing the current value in the same (or some other) file. If the call ofdestroy() method precedes the block of code which reads value from the file then the execution of the destroy() method will end up writing an incorrect value to the file on disk. This is just a sample example, you may think of other more complicated scenarios as well. The bottom line is that the servlet writers are not supposed to calldestroy() explicitly and hence they should avoid calling it




No comments:

Post a Comment