-1

Was going through some Java Servlets code.

I found attributes set and retrieved in 3 different ways.

req.getSession().setAttribute("var1","value1"); 
req.setAttribute("var2", "value2"); 
getServletContext().setAttribute("var3", "value3"); 

req is a variable of type HttpServletRequest I am a little confused as to what is the difference between the three?

user93353
  • 13,733
  • 8
  • 60
  • 122

2 Answers2

2

req.getSession().setAttribute("var1","value1"); - Is a Session Attribute.

req.setAttribute("var2", "value2"); - Is a Request Attribute.

getServletContext().setAttribute("var3", "value3"); - Is a Servlet Context Level (an Application) Attribute.

Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58
1

The first one is a session attribute, the second one is a request attribute and the third one is an attribute belonging to the ServletContext.

If you don't know what the session is and how it differs from the request, you should be studying how HTTP works.

The ServletContext is a separate place for the server to keep certain information. You'd more often be getting the attributes from there, rather than setting them.

Kayaman
  • 72,141
  • 5
  • 83
  • 121