Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sessions / instance variables / static variables

 
swapna Kadali
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I recently started servlets. I have a very basic question. Please clarify ..
1) The documentation says that there will be only 1 instance of the servlet in the servlet engine and any number of requests will have access to the instance variables of the same instance. In such a case what is the difference between the instance variables and static variables of a servlet class. ( I know the difference between these two in the case of regular class )..
2) On top of that, how do each variable behave when used in session management..
3) Why and when do we have to use cookies? What internally happens when HttpSession is used for session handling?
I would be thankful in case any one clarifies these questions at the earliest..
Thanks,Swapna
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is only one instance of the servlet unless it implements SingleThreadModel. Then there is one instance for each request.
If you dont implement SingleThreadModel, your only instance variables should be local variables. In other words, your only class scoped variables should be static(and usually final).
------------------
Dont blindly believe everything I say.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by swapna:
Hi,
I recently started servlets. I have a very basic question. Please clarify ..
1) The documentation says that there will be only 1 instance of the servlet in the servlet engine and any number of requests will have access to the instance variables of the same instance. In such a case what is the difference between the instance variables and static variables of a servlet class. ( I know the difference between these two in the case of regular class )..
2) On top of that, how do each variable behave when used in session management..
3) Why and when do we have to use cookies? What internally happens when HttpSession is used for session handling?
I would be thankful in case any one clarifies these questions at the earliest..
Thanks,Swapna

1) The only thing I would like to add to Randall's reply is that in a clustered set-up you probably have one instance per JVM, so in the real world you might have to be careful making wild assumptions about the number of copies in existance.
In a single JVM, a threadsafe servlet is a Singleton. You may want to read up on that. Not sure if "your only class scoped variables should be static". From an OO-purist point of view, I would not make servlet state static even though it is a Singleton. You would have to be very careful about threading issues obviously.
2) What do you mean with "when used in session management.."? The lifetime of your servlet has nothing to do with sessions or session lifetimes. Anything to do with the session is either part of the HttpSession object itself, or bound to it as an attribute (HttpSession.setAttribute, etc).
3) You use cookies when (a) the client isn't authenticated but you want to recognise him when he next visits the site or (b) when you want to store some client state but cannot store it in a database.
The application server will usually use cookies to identify the session. When a new session is created, a cookie is called JSESSIONID sent to the client with the next response. The client will send that cookie with every subsequent request, and the server will map it to the client's HttpSession object.
Other ways to track the session are SSL (for https connections) or URL rewriting (if the client doesn't support cookies).
HTH
- Peter
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter,
I used some static final Strings for use in println statements to output the html. I used the same Strings from more than one place in the servlet.
swapna,
If the servlet implements SingleThreadModel you can get away with member instance variables, but I would discourage it anyway.
Also, Your name does not comply with the Javaranch naming guidelines which can be found at http://www.javaranch.com/name.jsp
please register again with a valid name.
 
Rahul Rathore
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall, can you explain why you discourage the use of member instance variables? What point are you making when you mention your use of static final variables? Why should class scoped variables be static only?
As I recall the official Java Tutorial recommends a way to ensure that destroy() waits till all the threads spawned by the servlet instance finish execution. It makes use of instance (not static) variable in the Servlet to keep a track of the total number of threads spawned for that servlet which are currently executing. The destroy() method is forced to wait till the instance variable indicates that currently executing threads are 0.
In fact the Singleton pattern may not be an accurate reflection of the reality. The same servlet may be registered under different aliases. Then there will be one instance for every name (and NOT one instance per class). This means that a static variable could not be used to keep a count of the currently executing threads for the servlet instance. Because a static variable would be incremented /decremented not only by 'this' servlet instance- but by every servlet instance of the same class.
I get the impression that if at all we need to define member variables in a servlet (not SingleThreadModel), they must be instance and not static.
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im not the worlds greatest expert or anything. My understanding is that if you dont implement SingleThreadModel you should not use member instance variables as it is not threadsafe to do so.
 
Rahul Rathore
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I did not mean to imply anything. I just wanted to learn.
As my understanding is- the objection to thread safety would apply equally to ANY non-final member variable whether instance or static, because all threads would share the same variable.
It is for this reason that we need to make access to member variables synchronized.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It usually doesn't matter for static variables, because they are shared anyway.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"swapna",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.
 
Tiger Scott
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall,
In you earlier post you mentioned a servelet has a single instance unless it implements the STM. My understanding is to the contrary- I am a newbie into servlets. Could you explain this.
And also what are the situations in which we would have multiple JVMs.
Thanks
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sanjay, With an ordinary servlet one instance is created when you start the Servlet Container(server) and all incoming requests go to that one object. If you implement SingleThreadModel, a new instance is created for each request so you dont have to worry about synchronizing(at least not as much).
Rahul, I see your point. I guess if access is synchronized it is ok. I was really just repeating something I heard. It is always a good idea to use local variables when possible though.

[This message has been edited by Randall Twede (edited March 12, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic