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

Declaring global variables in JSP

Krysty Sullivan
Ranch Hand

Joined: Jun 06, 2002
Posts: 48
I need to have a string var declared in default.jsp page and the string var has to be accessible in the other jsp pages. Using the "public" modifier doesn't seem to work.
//var declaration in default.jsp
<% public String X="";
X += "<begin>";
%>
//accessing in test1.jsp
<% X += "test"; %>
//accessing in test2.jsp
<% X += "<end>"; %>
Thanks.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

You need to use the "declarative tag"

Note the exclamation mark...
Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

where's my brain today?
I also meant to mention that globals are a really bad idea in JSPs and Servlets - they tend to break the thread safety of the classes...
Dave (again).
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
David, will the use of the declarative tag make the variable available to other JSP ???
i thought it just puts that variable outside the service method, thus making it an instance variable.....
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Roy, you are corect. It doesnot make it available to other JSP pages. It has to be added as an attribute to the session and have the other pages access the variable from the session. it can also be added to the request object and then forward that to the other pages.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Sean MacLean
author
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
Don't forget that you can use application scope as well, for a truely global 'variable'.
Sean
Shubhrajit Chatterjee
Ranch Hand

Joined: Aug 23, 2001
Posts: 356
And don't forget to make the setter methods of this object synchronized
Originally posted by Sean MacLean:
Don't forget that you can use application scope as well, for a truely global 'variable'.
Sean


Shubhrajit
 
 
subject: Declaring global variables in JSP