aspose file tools*
The moose likes JSP and the fly likes Life cycle of Jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Life cycle of Jsp" Watch "Life cycle of Jsp" New topic
Author

Life cycle of Jsp

Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
Hello sir, madam

I have written the following jsp page. Name of the jsp page is myJsp.jsp
Although one should not write the constructor but just for debugging purpose I have written the following constructor.

<html>
<%!
static int counter = 0;
public myJsp_jsp()
{
System.out.println("Counter = "+(++counter));
}
public void jspInit()
{
System.out.println("In jsp init ...."+this.getClass());
}
public void init()
{
System.out.println("init of servlet = "+(++counter));
}
public void jspdestroy()
{
System.out.println("des 2");
}
%>

<body>
</b>Servlet Name:</b> <%=config.getServletName()%>
</b>Country:</b> <%=config.getInitParameter("Country")%>
</body>

</html>


In my web.xml file I have written the following lines

<servlet>
<servlet-name>HelloServlet</servlet-name>
<jsp-file>/myJsp.jsp</jsp-file>
<init-param>
<param-name>Country</param-name>
<param-value>India</param-value>
</init-param>
</servlet>

Now I try to access myJsp.jsp page using the following urls.
http://localhost:8080/Test/myJsp.jsp
and
http://localhost:8080/Test/servlet/HelloServlet

If we try to access both the above urls then the request is going to jsp page as it is indicated by web.xml.

Information : It is said that if a jsp page is accessed as a servlet [indicated by the second url above] then a separate servlet instance is created.If we access it as a jsp page then again a new instance is created.

Question : If I access both the urls then the static variable counter in myJsp.jsp should get incremented each time whenever a new instance is created. However I get the following results

URL : http://localhost:8080/Test/myJsp.jsp
Output :
Counter = 1
init of servlet = 2
In jsp init ....class org.apache.jsp.myJsp_jsp

URL : http://localhost:8080/Test/servlet/HelloServlet
Output :
Counter = 1
init of servlet = 2
In jsp init ....class org.apache.jsp.myJsp_jsp

Now can anyone comment on why the counter value is not 3 and 4 in the second output. Why it is again 1 and 2 when http://localhost:8080/Test/servlet/HelloServlet is accessed ?

counter variable is static.
Also I tried to use jspDestroy method in the above myJsp.jsp page. But nothing was printed on the console.So the class is not getting unloaded.

Can anyone please suggest me as to why this is happening ? Or Am I wrong at some place ?

Waiting for your valuable response..

Thanks and Regards
Rohit.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
First of all the constructor is not at all needed, not even for debugging. init()/jspInit() is a the first method called to initialize a servlet or a JSP.

Secondly, destroy() method is called by the container. You should not call that method in your code. Otherwise, nothing will happen.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Now can anyone comment on why the counter value is not 3 and 4 in the second output. Why it is again 1 and 2 when http://localhost:8080/Test/servlet/HelloServlet is accessed ?


I'm fairly certain that Tomcat (not sure about other containers) uses a seperate class loader for JSPs to allow them to be recompiled and reloaded on the fly. There is probably one instance of the static counter per class loader.

Also calling the destroy method does nothing.
It is merely a place holder for code that you want to have executed when the container destroys the servlet.
Try putting a println statement in there and then reload your app.


[Klunk!]
[ November 22, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Rohit Bhagwat:
Information : It is said that if a jsp page is accessed as a servlet [indicated by the second url above] then a separate servlet instance is created.If we access it as a jsp page then again a new instance is created.


Never heard before.
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
Information : It is said that if a jsp page is accessed as a servlet [indicated by the second url above] then a separate servlet instance is created.If we access it as a jsp page then again a new instance is created.

Not sure what you mean here. JSPs are translated into servlets, and like all servlets, are multi threaded. A new thread is created for each request regardless of the way the request was made. Sometimes I hear people talk about multiple instances of a servlet when they really mean multiple threads from the same servlet.

There is always only one instance of a particular servlet (or one instance per JVM if you are in a clustered environment...)


“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I think what he means is that you can have multiple instances of a servlet class if you create multiple servlet entries in the deployment descriptor.

I believe accessing a generated servlet from both a jsp-file entry in web.xml and hitting the JSP directly will cause two instances of the servlet (with different servlet names) to be created.
Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
Adeel Ansari -
init()/jspInit() is a the first method called to initialize a servlet or a JSP.
Even before these methods are called, the constructor gets called. Although I agree with you that these cannot be written by a developer.

Secondly, destroy() method is called by the container. You should not call that method in your code. Otherwise, nothing will happen
I cannot call destroy method in case of tomcat as it is a final method. This is not specified in the specification. However I can write a jspDestroy so that I can write the code that will perform some action that can be necessary before container destroys the servlet. Similarly I can write a destroy method in servlets for the same purpose.

Ben ,
Not only making entries in web.xml cause creation of multiple instance of servlets but also if you execute the above code you will notice the init method of the same servlet getting called more than once which indicates that multiple instances of the same servlet are created.

Following is the abstract from Sun Certified Web Component Developer (SCWCD) book by Hanumant Deshmukh (page 217)
The JSP engine creates two different instances of the generated servlet class- one for accessing it as a named servlet and one for accessing it as a jsp page.

Also Ben I will think on the following suggestion made by you
"There is probably one instance of the static counter per class loader."

Thanks all for your valuable responses.It was indeed an overwhelming response. Thanks once again

Regards
Rohit.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

I believe accessing a generated servlet from both a jsp-file entry in web.xml and hitting the JSP directly will cause two instances of the servlet (with different servlet names) to be created.


Aren't we supposed to specify the JSP Page name inside the jsp-file element. can you throw more light on this.

I like to know one more thing. Where to find the generated servlet from the JSP Page.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
Originally posted by Vishnu Prakash:


Aren't we supposed to specify the JSP Page name inside the jsp-file element. can you throw more light on this.

I like to know one more thing. Where to find the generated servlet from the JSP Page.


I have specified the jsp-file in the above web.xml file.
If you are using Tomcat then you will find the generated servlet in work directory.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

If you are using Tomcat then you will find the generated servlet in work directory.


What do you mean by work directory ?
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Rohit Bhagwat:
I cannot call destroy method in case of tomcat as it is a final method. This is not specified in the specification. However I can write a jspDestroy so that I can write the code that will perform some action that can be necessary before container destroys the servlet. Similarly I can write a destroy method in servlets for the same purpose.


You can always call a final method. No one is stopping you. We just cant over-ride a final method.

According to the j2ee docs destory is not a final method. It should not be otherwise it cant be overridden and this would be the death of the idea of performing task that can be necessary before destroying the servlet.

We used to over-ride destory method in our servlet. But we don't call it, the container is the one who calls it. If we call this method by ourselves all the tasks given there would be performed but servlet is not gonna be destoryed.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Vishnu Prakash:
What do you mean by work directory ?


TOMCAT_HOME/work
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
Thanks Adeel Ansari
Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
Originally posted by Rohit Bhagwat:
I cannot call destroy method in case of tomcat as it is a final method.


Yes You can call a final method but you cannot override it. I am sorry for this mistake.
Actually when I wrote destroy method [provided implementation of destroy]in the jsp page then I got error that destroy method is final and cannot be overridden. This is because Tomcat has provided a class that provides a default implementation for all methods in servlet interface as well as all methods of JspPage interface. I dont know why they have made it final.We know that jspDestroy method is avaliable for us to override and as such container calls this method from destroy method.So no need to use destroy method, we can use jspDestroy method instead. Thats why they might have made destroy method as final.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Rohit Bhagwat:
We know that jspDestroy method is avaliable for us to override and as such container calls this method from destroy method.So no need to use destroy method, we can use jspDestroy method instead. Thats why they might have made destroy method as final.


You got it right, Rohit.

destroy() method in HttpJspBase is final because jspDestory() is there to override. But in servlets we override destory() method of GenericServlet because its not marked as final.

Thanks.
Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
Originally posted by Rohit Bhagwat:
Also I tried to use jspDestroy method in the above myJsp.jsp page. But nothing was printed on the console.So the class is not getting unloaded.


I was unaware that a class can be loaded more than once,which is exactly what is happening here. The generated servlet class is loaded twice and thats why the counter is printing the value as if those values are reset.
[ November 24, 2005: Message edited by: Rohit Bhagwat ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Life cycle of Jsp