Solved

Difference between session and request

Posted on 2004-09-14
19
5,473 Views
Last Modified: 2007-12-19
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
request.getParameter()

is this the same with session

session.getAttribute().

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");
where
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.

Thanks,

Paul
0
Comment
Question by:pronane
  • 8
  • 7
  • 2
  • +2
19 Comments
 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
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)
0
 
LVL 1

Author Comment

by:pronane
Comment Utility
how do you create a session for a user?
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
One is automatically created with JSPs
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
<%
    out.println( "<H1>" + session.getId() + "</H1>" ) ;
%>
0
 
LVL 21

Assisted Solution

by:MogalManic
MogalManic earned 40 total points
Comment Utility
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");
0
 
LVL 1

Author Comment

by:pronane
Comment Utility
what is the point in passing this then

 loggedInUser

and this

 resultList

if this is never accessed in the jsp that willl get the attributes??
0
 
LVL 35

Accepted Solution

by:
TimYates earned 35 total points
Comment Utility
> 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
0
 

Expert Comment

by:pradeepbkumar
Comment Utility
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.
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
> 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()
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:pronane
Comment Utility
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
not

loggedInUser or ResultList

do you follow me?  what is the point in passing both names in???
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
> 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...
0
 
LVL 1

Author Comment

by:pronane
Comment Utility
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.

i.e

String paul = "t"

session.setAttribute( paul)

and is it the same for setParameter etc?

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

?
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
> 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 :-)

Tim
0
 
LVL 1

Author Comment

by:pronane
Comment Utility
sorry no i meant

request.setParameter("paul", paul);

not

request.getParameter("paul", paul);

0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
> 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
0
 
LVL 1

Author Comment

by:pronane
Comment Utility
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" ) )
      {
%>
                checked="true"
<%
      }
%>

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

this really is the last question!
0
 
LVL 21

Expert Comment

by:MogalManic
Comment Utility
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)

0
 
LVL 1

Author Comment

by:pronane
Comment Utility
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?
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
There is a "Split points" link above the comment entry box on this page :-)

Then you can divide the points up between us :-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Veeam Backup & Replication has added a new integration – Veeam Backup for Microsoft Office 365.  In this blog, we will discuss how you can benefit from Office 365 email backup with the Veeam’s new product and try to shed some light on the needs and …
A procedure for exporting installed hotfix details of remote computers using powershell
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now