Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Difference between session and request

Posted on 2004-09-14
19
5,804 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
ID: 12056067
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
ID: 12056094
how do you create a session for a user?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12056160
One is automatically created with JSPs
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 35

Expert Comment

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

Assisted Solution

by:MogalManic
MogalManic earned 40 total points
ID: 12061210
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
ID: 12062201
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
ID: 12062288
> 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
ID: 12062308
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
ID: 12062407
> 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
 
LVL 1

Author Comment

by:pronane
ID: 12062424
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
ID: 12062456
> 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
ID: 12062481
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
ID: 12062507
> 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
ID: 12062527
sorry no i meant

request.setParameter("paul", paul);

not

request.getParameter("paul", paul);

0
 
LVL 35

Expert Comment

by:TimYates
ID: 12062554
> 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
ID: 12062664
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
ID: 12062730
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
ID: 12062743
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
ID: 12062812
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
This article describes how to reset your Windows 10 password when you've forgotten it.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 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