Servlet class creating multiple objects
Hi
I am using WSAD 5.0. My servlet is acting as a controller but when when i m invoking the servlet from 2 JSPs , server is creating 2 objects for that servlet because of which i m not able to get the values which was set by the first servlet object..
Can somebody tell me why the server is creating 2 objects for my servlet.
Alok
# 5
No.. its is invoked only once.
If an instance of the servlet does not exist, the Web container
Loads the servlet class.
Creates an instance of the servlet class.
Initializes the servlet instance by calling the init method. Initialization is covered in Initializing a Servlet.
Invokes the service method, passing a request and response object. Service methods are discussed in the section Writing Service Methods.
http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets4.html
MeTitus
# 7
The reason I am saying init method is called twice is because I ve written
init()
{
sop("I am called");
}
destroy()
{
sop("I am destryoyed");
}
when i invoked the servlet from 2 JSPs the 2 times "I am called" is showing and when I stopped the server, its showing "I am destroyed" 2 times.
This shows the init method called twice.
do we have any kind of configuration in the server which can create multiple servlet object.
# 8
hi
I found the problem. The way of invoking the servlet from 2 JSPs were different. In the first JSP i was using the URL-PATTERN of the servlet from web-cofig.xml and in the second JSP I was hitting the servlet by using the direct path i.e servlet/packege.MyServlet which is in the class folder.
I guess this is the reason thats why server could not able to identify whether servlet object is same as the previous one or not.
Thanks
Alok