• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in having instance varibales in servlets

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I think we should not have instance variables in servlets because multipl thread share the instance variables and it will affect the data consistency
am i correct.

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.

let me know if i am missing some thing.

Thanks
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct.

You would need to either synchronize access to this shared list or find a way to avoid using instance variables altogether.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I think there is a option for single threaded model.
That make your servlet instance unshared.
But this is applicable in the situation where memory is not an issue.
And criticality of system is more then performance issue.
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SingleThreaModel should not be touched. Ben has already given some suggestions.
 
raja ram
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply
 
raja ram
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

Thanks
[ March 11, 2008: Message edited by: raja ram ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems to be correct. Here is my logic.

Object will be instantiated only once and init will be called only once.

Servlet will be loaded by container either by startup or first request

Startup or First Request

1.Object Instantiation(Plain Java Object)
2.Servlet Config Formed
3.Init will be called by passing the Servlet Config(Now it is Servlet Object)
4.New Thread will be created for request to Service Method.(Thread1)

Second Request

5.New Thread will be created for request to Service Method.(Thread2)

So Obviously it does take the linkedlist with populated values by the Thread1.

New variable wont be created for linked list again. So it make sense that servlet should not have instance variable(For good Design)as it is shared by multiple thread.

While Second Thread modifies the value, by the time any more request comes, then Synchronisation problem.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're only reading your instance variables from the service methods (doGet, doPost, etc) and never writing to then you shouldn't have any problems.
 
T Ponraj
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
100% Agree with Ben Souther, Normally Logger object will be created as instance veriable as we dont do any modification on it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic