This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
The text I'm reading is obviously out dated though it is still popular in educational institutions. From what I gather statements such as, session.setAttribute(), session.getAttribute(), and in action tags scope="session" are used for instance variables and the application object is used for static class variables. Is that correct?
They are not the same things, but there are parallels that can be drawn
Static class variable - <%! public static String CONSTANT = "theSameValueAtTheClassLevel" %> - value is available to all users across all requests
Class variable - <%! String classLevelAttribute = "sharedAcrossRequestsOnThisJSP" %> - value is available to all requests on this JSP
local variable - <% String localVariable = "availableOnlyToThisRequestOfTheJSP" %> - value is available only to the current request of the current JSP
With regards to attributes:
pageContext attributes are available only on that call of that JSP
request attributes are available only on the current request (so if you jsp:forward or jsp:include other resources, it is available there)
session attributes are available for the current session across multiple requests.
application level attributes are available for all requests across all users.
so you can draw a parallel between
- pageContext attributes and local variables - both are available only on the current request on the current JSP
- session attributes and class variables - The value is retained across multiple requests. The difference is, with a session, each individual user has their own copy of the value, whereas a class variable is shared with all requests/users. Having a class level attribute is a common cause of multi-threading errors with JSP/Servlet - as everybody tries to use the same variable.
- static class variables and application variables. Top level. Shared by all users.
I'm not saying that they ARE the same thing, just that they are comparable in their scopes/use/availability.
However as Bear has already pointed out, use attributes to store your application data, rather than variables.
They are much more suited to writing web applications.