you can not have any static fields or method in jsp. i think you should go throu JSP spec. when you code any java programm you do it inside scriplets which automatically changed into service method of converted servlet. all java methods or variable has the same scope in jsp page . so there is no need to make them static.
Why put java code in a JSP? If you have a static method that you want to declare, create a java class for it and declare it there. There is absolutely no motivation to put them into a JSP, whereas by putting it into a java class, you can get compile time checking, all the benefits of developing java in an IDE, and you can test it independantly of a server.
>The static fields are actually doesnt matter since JSP Servlet instances >are singleton unless it is declared thread-safe So what you are saying here, is that there is no difference between a static field, and a class attribute in a JSP. I agree with that much.
However neither static fields, or class attributes are desirable in a JSP as they are not thread safe. Basically, my rule is don't use the <%! %> declaration tags in JSP. If you are writing your JSPs correctly, you don't need them.
This seems like an old thread, but I have the very need that is being discouraged in this thread. Why? Because I am writing code for a site for which I have forbidden to use servlets and can only write jsp's.
David Rosario wrote:Because I am writing code for a site for which I have forbidden to use servlets and can only write jsp's.
What insane person put this restriction on you?
Without servlets, using modern best-practice web application constructs is impossible. It's like telling you to cut diamonds wearing welding gloves.
My advice would be to push back on this ridiculous restriction. I'd be interested to hear what kind of justification this person has for such stupidity.
That said, if this is an immovable restriction (insane! I may add), look into "Model 1" best practices where the bulk of "action" takes place in beans managed by the JSP and keep the JSPs as view-centric as possible under the restrictions placed upon you.
Joined: Mar 10, 2009
It is corporate, shared infrastructure. There ARE servlets on this site, but only the creators/maintainers of the site are allowed to use them. Their excuse was so far beyond ridiculous ("may destabilize the site") that no logic can explain it. The bottom line is that this is THE corporate site and I have no choice.
How does declaring a variable "static" in a JSP affect the scenario where there are multiple instances of the same JSP configured through the web.xml?
Using a simple counter variable declared static, I assumed that all JSP instances would share the class variable's value, but found that they had independent values! Could someone explain why static variables are not being shared in this case? Below is the code, deployed on Tomcat 5.5.27:
If you need shared data, do it the right way and store it in the session or application contexts as appropriate. It's 2009 -- well past the point where scriptlets should no longer be used in JSPs.
Joined: Dec 26, 2008
Bear Bibeault wrote:If you need shared data, do it the right way and store it in the session or application contexts as appropriate. It's 2009 -- well past the point where scriptlets should no longer be used in JSPs.
Thanks. But this is more for the enlightenment of the inquisitive mind rather than the current practicability aspect. Any pointers to what i might be missing above would be highly appreciated.