File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declaring global variables in JSP

 
Krysty Sullivan
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to use the "declarative tag"

Note the exclamation mark...
Dave
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 732
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1510
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sean MacLean
author
Ranch Hand
Posts: 621
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't forget that you can use application scope as well, for a truely global 'variable'.
Sean
 
Shubhrajit Chatterjee
Ranch Hand
Posts: 356
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic