aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes doubt in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "doubt in servlet " Watch "doubt in servlet " New topic
Author

doubt in servlet

raja ram
Ranch Hand

Joined: Mar 02, 2008
Posts: 169
consider the example below

public class a extends HttpServlet{

protected LinkedList l=new LinkedList();

public void init(ServletConfig config){

for(int i=0;i<10;i++)
l.add(i);
}

public void doget() throws ServletExcetion,IOException{}

}


in this case the protected variable l were we are making add operation in init() will these not affect when multiple thread are executing together

say thread-1 will create the object and will be doing add operation.
in this time if thread-2 instantiates variable l will it not affetc the state of thread-1.

according to me it should affect both the threads but i am not able to find any difference when i execute the servlet it runs with out any problem.


for me code is working with out any problem even when i submit multiple requests.

I know init() will be executed only once say for the first request only that time i will built the list of items. and in dopost() i will only read it.

This will work fine for first thread. say now i will submit second request means a second thread now it will defnitely re instantiate my LinkedList l
and init() will not execute this time even though i am only accessing the elements from LinkedList l it should not have previous request value that it got populted for the very first request when my init methode got executed. am i correct.

but in my case it works fine with out any problems.and displays values that got populated when init() got executed

what i guess even though i have LinkedList l declared as instance variable but i am building it only once in the very first request through init() servlet will save the state of the object some where and later for other requests even though it instantaties the LinkedList l when executing dopost() at the time of accessing it will use the one it as already stored when it executed init() and not the one it newly instantiated for this thread.

is there any concept like this,can any one throw some more light on this
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Originally posted by raja ram:
...say thread-1 will create the object and will be doing add operation.
in this time if thread-2 instantiates variable l will it not affetc the state of thread-1.

...I know init() will be executed only once say for the first request only that time i will built the list of items. and in dopost() i will only read it.

...This will work fine for first thread. say now i will submit second request means a second thread now it will defnitely re instantiate my LinkedList

Whoa whoa whoa! The container reuses the same Servlet object. No object (Servlets included) would ever reinstantiate its variables simply because a new thread is calling methods on it.

Your Servlet variable is thread safe because the only method that alters it is the init() method, which only runs at the start of the Servlet's lifecycle. If you were to alter the list in a doGet, doPost, or similar method, then your variable would no longer be thread safe.


A good workman is known by his tools.
raja ram
Ranch Hand

Joined: Mar 02, 2008
Posts: 169
Hi,

Thanks for the reply, when we say no object will be reinstantiated that means even the line LinkedList l=new LinkedList() will not be exceuted for the next request is that correct.

because instance variables will be exceuted for every instance creation but in case of servlets the container will instantiate the servlet only once for the request so the instance variables l will be instantieated only once and for all the other request it will simply use the same as told in the above comment is my understanding correct.

Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
the line LinkedList l=new LinkedList() will not be exceuted for the next request

Correct.

because instance variables will be exceuted for every instance creation

Correct.

in case of servlets the container will instantiate the servlet only once for the request

No. The servlet (like any other object) is instantiated exactly once. Nothing is instantiated (or initialized or re-initialized) for each request. The doGet (or doPost) method is called for handling a request, that's all.
[ March 12, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - updated with new look and Ping home screen widget
Gunnar Maluf
Ranch Hand

Joined: Dec 29, 2007
Posts: 39
Well, i just read that every request runs in a separate thread even if it comes from the same client. Is it wrong?


Sun Certified Associate for Java Platform
Sun Certified Java Programmer 5.0
Sun Certified Web Component Developer 5.0
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
every request runs in a separate thread even if it comes from the same client. Is it wrong?

No, that's correct.
raja ram
Ranch Hand

Joined: Mar 02, 2008
Posts: 169
Hi,

Thanks for the reply i understood the concept.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in servlet
 
Similar Threads
few intrvw questions
When is a Servlet destroyed?
JSP Thread question
Doubt in having instance varibales in servlets
when init() method is called ?