Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP + static variables  RSS feed

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hiya,
could someone please tell me how to use static variables with a JSP Page. I have declared a variable ie
<% static String myString;%>
then I have a function which is called recursively etc etc.
<%! private void myFunction(){
//get at myString
}
it just gives a compile error??? am I doing something fundamentally wrong??
Thanks for your help
Mike
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are doing something wrong. Your variable declaration is in a scriptlet <% String foo=null; %>. That means it has the scope of the JSP service() method. I'm pretty sure it needs to be a declaration just like your method, e.g. <%! static String foo=null; %> That would give it a scope equivalent to the entire class.
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't say what error you get, but for a start the static declaration needs an exclamation mark as well. I hope you know what you're doing -- beware of threading issues!
- Peter
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For completeness, the best way to understand how <% and <%! tags work is to look at the output of the jasper compiler (the one that turns a JSP into a java source file for a servlet.

In tomcat, you will find these in the 'work' directory.

Take a look at the *.java file that is created from your JSP. You will get a very good understanding of not only scope imposed by using <% and <%!, but also you'll understand where 'request' and 'session' and 'out' objects come from.
 
manoj bagul
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Mike ,
I think u can not define a static variable inside a jsp page.
This might help. Can u post what error is says.
Manoj.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can declare static variables in JSP, but the value of doing so is dubiuos.

The issue was that his method was declared inside JSP declaration tags ( <%! %> ) which made it a method of the servlet class. But his variable was defined inside scriptlet tags ( <% %> ) which meant the variable was scoped inside the service method
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That should give compiler error. You can't declare a variable as static within a method and all codes within <% .... %> scriptlets go within the service() method of the resulting jsp_servlet from a jsp. So you have to decalre static methods in declarative statements within <% .... ; %>. Look at the following code and the commented line
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!