• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic question about request and session  RSS feed

 
Hardy Chou
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as far as i know... session object lives from the moment that the user logins till logout which invalidates the session. next, request object is only instantiated whenever the user submits a form. and request object is short-lived..

and request object is heavier than the session object since a request contains session object.

am i right to say that?
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

and request object is heavier than the session object since a request contains session object.[/QB]


Now what exactly do you mean by "heavier" objects? Do you mean with respect to memory or something else?
 
Sachin Roham
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Hardy,
If u mean that "request" object is heavier than "session" because the former contains the latter, then i object.
Cause in java no object can contain another object, it can contain only the reference of another object.
And a session object is more heavier in a sense, because the container has to do a lot of work to mantain the various sessions, it has to maintain a kind of hashmap to store the session object with the corresponding
Session-ID...and much more.


-
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A request object is created whenever a request arrives at the application server.
A session object is created just once per session (at most).

The session is mainly a storehouse for information that should be maintained between requests, the request contains all information needed to handle it which is a lot.
 
Hardy Chou
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Vijayendra V Rao, yes in respect of memory usage.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The request does not contain the session. It has a method to obtain a reference to the session.

Why are you concerned about the 'weight' of a request instance?
 
Hardy Chou
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
another question regarding design:

lets say i can get the session from servlet controller. is it wise to pass the session to POJO instead of passing the necessary values?

as in.. instead doing below:

class.method((String) request.getAttribute("a"), ....

i do this

class.method(session)

and processing the attribute values later in the POJO

which one is a better approach?
 
satish sathineni
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basic difference between the Request object and the Session object is the way user uses it..

Session object created by the user upon his requirement.

Request object is created by the webserver.

the reason Request object contains information about the individual request and the other information like ipaddress, whole lot of other things which the browser sends as the headers ...

where as Session on the other hand contains only the user defined objects which are specially used by the user ....

 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by satish sathineni:

where as Session on the other hand contains only the user defined objects which are specially used by the user ....


As far as I know, the session object is used by the servlet container to identify a user between requests. I don't think there are "user defined" objects and they are used by the user. The session is used by the servlet container for unique user identification. Please correct me if I am wrong
 
satish sathineni
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As far as I know, the session object is used by the servlet container to identify a user between requests. I don't think there are "user defined" objects and they are used by the user. The session is used by the servlet container for unique user identification. Please correct me if I am wrong


Yes ur right Session is used by the server to uniquely identify the user.
but why a programmer uses the session for writing some user specific code and stuff that applies....
from programming point of view i said session holds user said variables
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by satish sathineni:


Yes ur right Session is used by the server to uniquely identify the user.
but why a programmer uses the session for writing some user specific code and stuff that applies....
from programming point of view i said session holds user said variables


Ofcourse!

Maybe the word "user-specific" attributes would be more appropriate than "user-said" attributes. Thats all I wanted to say
 
Hugh Jamieson
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Hardy Usman:
another question regarding design:

lets say i can get the session from servlet controller. is it wise to pass the session to POJO instead of passing the necessary values?

as in.. instead doing below:

class.method((String) request.getAttribute("a"), ....

i do this

class.method(session)

and processing the attribute values later in the POJO

which one is a better approach?


Hi, Hardy!
I don't think I saw a good answer to your question, so here are my ten cents: If you pass the request -or- session object to your POJO, you will be forced to bind your POJO to those container classes, which will require those classes anytime you use your POJO. You are better off passing the individual parameters, giving your POJO a chance at better reusability.

Ragards,
Hugh
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Hugh, and would add that not only does passing the session bind your class to the servlet environment (as he pointed out), it also spreads implied knowledge into the class in that it will need to know the keys to use in order to obtain the attributes. Not a great way to do things.
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by satish sathineni:


Yes ur right Session is used by the server to uniquely identify the user.
but why a programmer uses the session for writing some user specific code and stuff that applies....
from programming point of view i said session holds user said variables


You can add attributes to the request object also.
 
Rene Smith
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe request is better to use than session when at all possible. You don't have to worry about clean up of the session, the request object is gone once the request is over - period.

Also - as far as storing information about the user - that's true - but it can also store more information than just user information. If an app is not carefully constructed it can store a lot of crap that could hang around in memory until the user terminates his session or until the session expires - which potentially can be alot of time.

So - please, please use request whenever possible. And, if you need to use sessions (which all interactive apps are going to need to do to some point) just ensure clean up. Just think of 10 session variables each with 100 row arraylists times 100 users - AHHHH.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!