aspose file tools*
The moose likes JSP and the fly likes class variable in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "class variable in jsp" Watch "class variable in jsp" New topic
Author

class variable in jsp

Ivan Turner
Ranch Hand

Joined: Feb 27, 2012
Posts: 37
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

Joined: Jan 10, 2002
Posts: 61773
    
  67

Yes, it is (but of course, neither should be used in modern JSPs).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ivan Turner
Ranch Hand

Joined: Feb 27, 2012
Posts: 37
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

Joined: Jan 10, 2002
Posts: 61773
    
  67

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

Joined: Jul 06, 2005
Posts: 1030
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class variable in jsp