Tuesday, October 25, 2005

Why do we need Attributes? What’s it’s purpose?

Explain

context.setAttribute(....)
session.setAttribute(....)
request.setAttribute(....)

An attribute is an object usually bound into one of the three servlet API object’s. ServletContext, HttpServletRequest(ServletRequest), HttpSession. You can think of it as simply a name/value pair( where name is a String and value is an Object). All we care about when looking at attributes is about the scope in which attributes exists.

context.setAttribute(....)

These attributes are accessible for all requests within the current application and usable throughout the life of the web application. Scope of these attributes are lifetime of deployed application. If server or application goes down, the context is destroyed.

Context attribute are accessible to all the request from any client, as context attributes are common to all the components in the web application.

session.setAttribute(....)

These attributes are stored and retrieved across multiple requests from the same client and accessible throughout the life of this user's session .
They are isolated from other sessions (i.e. one user's session is completely separated from another’s). Scope of session attributes are life of the session. A session can be destroyed programatically or can simply time-out.

Session attribute are accessible to all the request from same client, as all the request from the same client will use a single session object on the server side.


request.setAttribute(....)

These attributes are only valid and usable for the duration of one request (i.e. from the user requests some resource, until the server has finished responding), but allows different components within this request (look up: MVC or requestDispatcher) access to objects bound to it. Scope of request attributes are life of the request. In other words, for the life of the thread handling this request.

Request attribute are accessible only to those component(JSP/Servlet) to which request object has been forwarded. These attributes are valid and usable only for the duration of one request because http is a stateless protocol.


0 Comments:

Post a Comment

<< Home