File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Joshua Cloch
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Joe Ess
Bartender
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Joshua Cloch
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64175
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rusty Smythe
Ranch Hand
Posts: 93
Mac Objective C Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much,guys!
Quite helpful!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic