• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7438
  • Last Modified:

Difference between session and request

What is the difference between session and request parameters, does session just last for any given users session whereas request lasts for any user?  how do you set the session and request etc

I know that with the request parameter everything is an object within the

is this the same with session


Also can you set the Parameter with request, and what are you really doing when you set the parameter/setAttribue?

Finally, when you do setAtrribute(test, "test");
String test = "tester";

which variable are u setting and which one will u be using in the page that gets the attribute, i.e where session.getAttribute(_what_goes_here_) is called.

_what_goes_here_ = ??

Im hoping that someone will actually explain this rather than just give me a site, if i get an explanation then i will give 100 points for the question.  As ive read it before but i dont fully understand, so an answer with a backup link would be great.


  • 8
  • 7
  • 2
  • +2
2 Solutions
request lasts for that http request (when a page is loaded (and only for that page) for any user)... the variable is then handled by the garbage collector.
session variables retain state throughout the users session (if you create one)
pronaneAuthor Commented:
how do you create a session for a user?
One is automatically created with JSPs
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

    out.println( "<H1>" + session.getId() + "</H1>" ) ;
Every time someone accesses your servlet, a request object is created by the Servlet API.  The servlet parses the request information from the client and stores it in the HTTPServletRequest object.  The request parameters are the strings stored on the URL or stored on the form posted on the client.  In addition, you can attach your own objects to the request.  These objects can be any object you desire and can be used by JSP pages you include or forward to.  When control is returned to the client, all objects attached to the request are garbage collected.

Sessions are created on demand by calling request.getSession(true)  (or as TimYates says it is automatically created for JSP pages).  The session actually exists on the Server and is attached to each user request via a Session ID.  The session ID is usually a cookie stored on the client that uniquily identifies that client.  If the client has cookies disabled, then the session is stored as a parameter on the request via URL rewriting.  Objects stored on the session remain in memory until specifically removed by your code OR when the user becomes inactive and the session expires.  So when you see a "Logoff" link or button on a web page, it is used to release the user session data.

Request and session attributes are accessed by the String key that you supply.  To set a Session attribute:

session.setAttribute("userName", loggedInUser);

To retrieve a session attribute:

String curUser= (String) session.getAttribute("userName");  //The cast is necessary because the return value is Object

To set a request attribute:

request.setAttribute("queryResults", resultList);

To retrieve request attribute:

List resultList=(List) request.getAttribute("queryResults");
pronaneAuthor Commented:
what is the point in passing this then


and this


if this is never accessed in the jsp that willl get the attributes??
> if this is never accessed in the jsp that willl get the attributes??

If you are storing them in the request, nothing...  Unless you are importing another JSP that does use them

If you are storing them in the session, then they can be read at a later date by another JSP or servlet
Request is something which comes in handy when you are moving from one page to another by submitting a form. Whereas session is something that exists unless you explicitely remove it or the browser gets closed.
the diff between request.getParameter() and session.getAttribute() is that the latter gives you access to the attribute value whereas the former one gives you the object reference for wat you would have set.
> the diff between request.getParameter() and session.getAttribute() is that the latter gives you access to the attribute value whereas the former one gives you the object reference for wat you would have set.

No, request.getParameter() gets a value out of the URL query (POST header or GET URL)

You mean request.getAttribute()
pronaneAuthor Commented:
no tim i dont think you understood what i was saying

why dont u just pass
"queryResults" and "userName"

because they are the variables that are always accessed

loggedInUser or ResultList

do you follow me?  what is the point in passing both names in???
> do you follow me?

Not sure....

> what is the point in passing both names in???

Into the setAttribute() method?

The first parameter is the name you want to give to the attribute

The second parameter is the attribute itself

int[] intarr = new int[ 3 ] ;
session.setAttribute( "ARR", intarr ) ;

intarr is the attribute, I have stored it in the session under the name "ARR"

so in another jsp, I can do:

int[] intarr = (int[])session.getAttribute( "ARR" ) ;

and get the array back out again...

Hope I understood the question right...
pronaneAuthor Commented:
ya thats what i was aksing alright

why cant u just pass the name of the attribute though without having to pass the both?, that is my real question.


String paul = "t"

session.setAttribute( paul)

and is it the same for setParameter etc?

i.e  request.getParameter("paul", paul);

> why cant u just pass the name of the attribute though without having to pass the both?, that is my real question.

Because at run time, java has no idea what you called your variables.  Variable names are there to help you keep track of what is what, but java doesn't care about what you called them, so that information is gone when the JSP is compiled...

Therefore, you have to give them a name.

> request.getParameter("paul", paul);

You mean

    request.getAttribute( "paul" );


request.getParameter( String parametername )

gets a parameter from a form post :-)

pronaneAuthor Commented:
sorry no i meant

request.setParameter("paul", paul);


request.getParameter("paul", paul);

> request.setParameter("paul", paul);

You can't do that, what you mean is:

request.setAttribute( String name, Object value ) ;

Attribute, not parameter...

Parameter is for gettting values posted to the page by a form
pronaneAuthor Commented:
ok tim you have answered sufficiently so i will give u the points, before i do i was just wondering if you could briefly explain what this is doing:

<input type="checkbox" name="linkDeleteCB<%=k%>"
      if ( linkDeleteCB.equals( "on" ) )

where k is any number, what does it mean by name = linkDeleteCB<=%k>   ?

this really is the last question!
Assuming k is an index in a for loop, the above code fragement will produce HTML to the client like this:

<input type="checkbox" name="linkDeleteCB1">
<input type="checkbox" name="linkDeleteCB2">
<input type="checkbox" name="linkDeleteCB3">
<input type="checkbox" name="linkDeleteCB4" checked="true">
<input type="checkbox" name="linkDeleteCB5">
<input type="checkbox" name="linkDeleteCB6">

(I cleaned up the whitespace in the actual results)

pronaneAuthor Commented:
ok thanks and checked = true on the 4th one
means assuming the the 4th one checkboxed is checked it will delete whatever value that represents?

Can i half the points or give assisted answers as both you and tim ahve answered in the questions?
There is a "Split points" link above the comment entry box on this page :-)

Then you can divide the points up between us :-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 8
  • 7
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now