Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference in request and session attributes and parameters

 
Harry Wordsworth
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello all,

I am doing more studying of servlets, jsps etc and I am currently trying to understand some differences between sessions and requests.

Lets say in the context of a simple page with a form which can be submitted to a servlet and then a response goes back to a jsp.

The way I "think" I understand this is...and please bear with me on this, I realize it's probably more detailed and complex then what I am about to describe but I am just looking for a bare-boned understanding at this point...

Basically a "Request" will come from say a form submission, this request will have parameters with it. If the form is submitted to a servlet then the values in the request can be obtained via methods such as

request.getParameter

The information in this request will only be available for this one "page to servlet" interaction.
For something more permanent I can use a "Session".

The session will be available from the moment the servlet or jsp is initiated and with that I can use methods like this...

session.getAttribute
session.setAttribute

...to set and get values throughout the user's interaction with the servelt and associated pages.

Is that basically right?
If so then I am fine with my understanding to this point but what is throwing me is that there is also

request.getAttribute
request.setAttribute

From what I can tell this would be used when you want to, say from a servlet, create a request and say use a requestDispatcher to forward a request to another jsp or servlet. You can then get and set attributes on this request? but this request attribute would only be available in this single jsp/servelet to jsp/servlet interaction?

I searched online a bit and found someone else who has the same interpretation as I do on when you should use something like request.(get/set)Attribute vs session.(get/set)Attribute. is this correct?

He wrote...
I want to confirm the differences between request.setAttribute(key, value) versus session.setAttribute(key, value).

My understanding is that request.setAttribute only make the key available in the following jsp page. But session.setAttribute will make the key available in many pages, as long as in the same session, and browser not close.

For example, the flow like page1->page2->page3->page4. session.setAttribute will make the key available in all pages. But if we use request.setAttribute in page2, then only page3 can get the key value set in page2.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, first lets address parameters versus "attributes". Paramters, as you surmised, are submitted along with the request from a client. They are frequently form inputs, but can also be gathered from the query string on the request. Usually a GET will have the params in the query string, and a POST or PUT will have them in the request body.

The important thing to know about parameters, is that they are a means for the client to send information to the server.

"Attributes" on the other hand, are completely a server-side concept. Essentially they are just a way for your server-side code to store data. Think of them as an alternative to global or instance variables for the servlet, both of which cannot be used in order to avoid disaster.

"Attributes" are more properly called "scoped variables" so if you see the alternative term, it's referring to the same thing.

Scoped variables are so called because they can be placed at four scopes: page, request, session and application; all using setAttribute() and getAttribute() on the respective scope objects.

Page scope is limited to within JSPs and invalidates when the JSP is finished processing. It's not available in a servlet.

Request scope exists for the duration of a single request. That's why it's most often used to carry data from a servlet to a forwarded JSP.

Session scope is associated with a browser instance and so is per-user and per-browser. This makes it useful to remember info for a single user over the course of multiple requests.

Application scope is "global" to the application and is valid as long as the web app is active. It's a good way to store universal read-only data set at app startup.

That help?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And with regards to
Harry Wordsworth wrote:
and please bear with me on this, I realize it's probably more detailed and complex then what I am about to describe but I am just looking for a bare-boned understanding at this point...
perhaps this article might prove helpful.

If you're a novice with JSP as well, I also recommend this article.
 
Harry Wordsworth
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes thanks! That did help!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic