Issue
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());
- What are the differences?
- When should you use each?
Solution
Those three have different scopes:
request attributes live for the life of that request/response cycle
session attributes for the life of that session
ServletContext
is across the servlet context and it lives until context gets destroyed.
Answered By - jmj