aspose file tools*
The moose likes Servlets and the fly likes why do we usually define variables in doGet() or doPost()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "why do we usually define variables in doGet() or doPost()?" Watch "why do we usually define variables in doGet() or doPost()?" New topic
Author

why do we usually define variables in doGet() or doPost()?

Joshua Cloch
Ranch Hand

Joined: Apr 27, 2006
Posts: 95
why do we usually define variables in doGet() or doPost()?

I have viewed a lot of soucecode,most of them define most of the variables in these two methods.Why not define the variables just like what we do in normal java class?Say,use private scope for the virable out of these methods?

Is there some particular reason for that? Or, that is because even if we use private scope for the virables,it may cause multi-thread problems?


truehh@hotmail.com
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8971
    
    9

The correct answer is "declare variables where they make sense".
It just so happens that in servlets, most of what one is operating on is specific to the request at hand, so we end up creating many variables in the do* methods. You are correct in your analysis that this prevents threading problems that would occur with class or instance scope variables, though there are still cases in which you would use those scopes as well (i.e. read only properties like configuration values).


[How To Ask Questions On JavaRanch]
Joshua Cloch
Ranch Hand

Joined: Apr 27, 2006
Posts: 95
Thanks very much.

Theoretically speaking,do you think defining private variables out of these two methods will cause threading problem or not?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Personally, I think if you are defining a lot of variables in your servlets, you are going about it incorrectly in the first place. Meaning you are trying to do too much in your servlet that should be delegated to different helper classes. Servlets should receive requests, hand information off to some processing class(es) and send a response.

That aside, I never use class / instance scoped variables in servlets.


GenRocket - Experts at Building Test Data
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Joshua Cloch:
Thanks very much.

Theoretically speaking,do you think defining private variables out of these two methods will cause threading problem or not?


It can.

As a general rule of thumb: never create instance variables in a servlet unless you really understand how servlets work in regards to threading.

Containers will create one instance of your servlet and call your servlet's service methods (doGet, doPost, etc...) from within a separate thread for each request.
This means that any instance varaibles will be shared among all of your requests.
[ August 17, 2006: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61657
    
  67

Originally posted by Ben Souther:

It can.


I'm going to emphasize that a little more and answer "almost certainly".

The vast majority of "my servlet is acting weird" posts in this forum turn out to be threading issues.

Gregg's point is also quite pertinent: in a properly structured web application, servlets should be controllers, not work-horses.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rusty Smythe
Ranch Hand

Joined: Aug 09, 2006
Posts: 93

Originally posted by Bear Bibeault:
I'm going to emphasize that a little more and answer "almost certainly".


This has been my experience as well, however there are appropriate uses for private variables, as Joe Ess mentioned.

I use private variables for logging (log4j) and storing a hibernate SessionFactory, as neither of these items will cause issues if they are co-opted by another thread (the SessionFactory is the same across all threads).
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Rusty Smythe:


This has been my experience as well, however there are appropriate uses for private variables, as Joe Ess mentioned.

I use private variables for logging (log4j) and storing a hibernate SessionFactory, as neither of these items will cause issues if they are co-opted by another thread (the SessionFactory is the same across all threads).


Yes, there are plenty of good reasons for creating instance variables in a servlet; you just need to know what you're doing.
This is why I said:
As a general rule of thumb, until you know exactly how intstance variables will be handled in a servlet environment, don't use them.
Joshua Cloch
Ranch Hand

Joined: Apr 27, 2006
Posts: 95
Thanks very much,guys!
Quite helpful!
 
 
subject: why do we usually define variables in doGet() or doPost()?