Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class variable in jsp

 
Ivan Turner
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I correct in making the analogy of a declaration variable being a static class variable and script variable being a instance variable? I believe it's true but I want affirmation.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is (but of course, neither should be used in modern JSPs).
 
Ivan Turner
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Scoped variables are not the same as scripting variables. Scoped variables are not members of the servlet class created for the JSP, unlike scripting variables which are members.
 
Stefan Evans
Bartender
Posts: 1722
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic