Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

context attribute vs session attribute vs request attribute  RSS feed

 
harish goyal
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone help me in understanding the concept of
context attribute
session attribute
request attribute

i know that much context attribute is for whole web application
session attribute is for some interval . (any talks between two for some time)

request attribute( just request and reply)


can anyone help me to understand the real concept of these
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a nutshell, these are the various places you can store objects that you want to keep around for a while in the server's memory.
You've already told us how long the objects will last in each scope.
What else do you need to know?

It may take some coding to get a full grasp of the concept.
Try building a small app that binds variables to the various scopes.
Then build some servlets or jsps that read the variables.
Try closing the browser and opening a new instance to call the servlets again.
See which variables still exist and which are null.
 
Choon-Chern Lim
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess Harish may know what it is about, but wasn't sure what to do with it.

Anyway, Harish, this is some examples:

Request - Normally used for passing data from jsp to your servlet when you submit the form. When you get redirected to another jsp, your request dies. ie: this attribute lives per user request.

Session - Good example will be for storing user credential (like LAN ID) once user is authenticated. Sometimes you may want to check if the user has right access to do on some database operations like add/delete/edit. Once user closes the browser or the session goes idle for x amount minutes (depending on your server setup), the session dies and all info in it will be gone.

Context - Only store info here if it is global and applies to every user.

If it is application specific, consider using context.
If it is user specific, consider using session.
If it is request specific (ex: jsp form submission), consider using request.

Hope this helps.
 
harish goyal
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks BEN AND mike

hi i know request attribute.
The doGet method of the servlet responds to an HTTP GET request

please can u give idea of one simplest application.
of session attribute .
so that i can deploy myself.
and gets the real meaning of it .
please help!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The typical textbook example is a shopping cart.


Another one, more commonly used in real world apps, would be a userBean.

Create a Bean that holds a user's information (firstName, lastName, email, screenName, mailing address, etc....).

When the user logs in, pull that information from a database and load it into the userBean and bind it to session.

Create a series of pages that all have the user's full name and address displayed on top.


[added]
You can take it a step further by creating a mock database:
Create a object with a map full of userBeans and bind it to contextScope.
Give it a method that takes the userID and password as arguments and returns the proper instance of UserBean. You can then use that database to get the user's information when they log in.
[ August 30, 2005: Message edited by: Ben Souther ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!