Link to home
Start Free TrialLog in
Avatar of pronane
pronane

asked on

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
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
Avatar of bobbit31
bobbit31
Flag of United States of America image

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)
Avatar of pronane
pronane

ASKER

how do you create a session for a user?
Avatar of TimYates
One is automatically created with JSPs
<%
    out.println( "<H1>" + session.getId() + "</H1>" ) ;
%>
SOLUTION
Avatar of MogalManic
MogalManic
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pronane

ASKER

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??
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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()
Avatar of pronane

ASKER

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???
> 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...
Avatar of pronane

ASKER

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);

?
> 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
Avatar of pronane

ASKER

sorry no i meant

request.setParameter("paul", paul);

not

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
Avatar of pronane

ASKER

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!
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)

Avatar of pronane

ASKER

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 :-)