Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between attributes in Request, Session and ServletContext [duplicate]

I am having trouble understanding the differences between these 3 ways of setting attributes:

// String as attribute of request
req.setAttribute("name", "Sluggo");

// Integer as attribute of session
req.getSession().setAttribute("age", 10);

// Date as attribute of context
getServletContext().setAttribute("today", new Date());
  1. What are the differences?
  2. When should you use each?
like image 312
AustinT Avatar asked Mar 14 '13 00:03

AustinT


People also ask

What is the difference between request and session?

Session attributes are meant for contextual information, such as user identification. Request attributes are meant for specific request info, such as query results.

What is ServletContext attribute?

An attribute is an object and when such objects are associated with a ServletContext object, they are called context attributes and are available to the whole application i.e. all the Servlets in the application can access these attributes through the ServletContext object.

What are session attributes?

A session attribute is a pre-defined variable that is persistent throughout the life of a Tealeaf session. Session attributes can be used to store various data that may be referenced by events at any point during the session.

What is session attribute in Java?

Jakarta EE/Java JEE 8 Web Development(Servlet, JSP and JDBC) The session attribute indicates whether or not the JSP page uses HTTP sessions. A value of true means that the JSP page has access to a builtin session object and a value of false means that the JSP page cannot access the builtin session object.


1 Answers

A ServletContext attribute is an object bound into a context through the ServletContext.setAttribute() method and which is available to ALL Servlets (thus JSP) in that context, or to other contexts via the getContext() method. By definition a context attribute exists locally in the VM where they were defined. So, they're unavailable on distributed applications.

Session attributes are bound to a Session, as a mean to provide state to a set of related HTTP requests. Session attributes are available ONLY to those Servlets which join the session. They're also unavailable to different JVMs in distributed scenarios. Objects can be notified when they're bound/unbound to the Session implementing the HttpSessionBindingListener interface.

Request attributes are bound to a specific request object, and they last as far as the request is resolved or while it keeps being dispatched from Servlet to Servlet. They're used more as communication channel between Servlets via the RequestDispatcher Interface (since you can't add Parameters...) and by the container. Request attributes are very useful in web apps when you must provide setup information between information providers and the information presentation layer (a JSP) that is bound to a specific request and need not be available any longer, which usually happens with sessions without a rigorous control strategy.

IN SUMMARY, we can say that:

  • Context attributes are meant for infra-structure, such as shared connection pools.
  • Session attributes are meant for contextual information, such as user identification.
  • Request attributes are meant for specific request info, such as query results.

Source: Servlets Interview Questions by Krishna Srinivasan @ javabeat.net

like image 76
MikO Avatar answered Sep 20 '22 01:09

MikO