aspose file tools*
The moose likes JSP and the fly likes Static methods and 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 "Static methods and variable in jsp" Watch "Static methods and variable in jsp" New topic
Author

Static methods and variable in jsp

Mary chavez
Greenhorn

Joined: Apr 24, 2006
Posts: 3
Hi,
Can we declare the static method and variables in JSP? If no then why? and if Yes then How? pls answer me asa possible...
kishor chandra
Greenhorn

Joined: Aug 04, 2005
Posts: 6
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.
Manesh Kumar
Ranch Hand

Joined: Mar 21, 2006
Posts: 94

Can we declare the static method and variables in JSP? If no then why? and if Yes then How? pls answer me asa possible...


I think it is possible to create static methods and variables in JSP.

If you happen to use
<%! %>

this will place the code in class level and not in the _jspService method.

The static fields are actually doesnt matter since JSP Servlet instances are singleton unless it is declared thread-safe.
[ April 27, 2006: Message edited by: Dilip Kumar Jain ]

Manesh
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Yes it is possible.
No, is is not advised.

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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

Originally posted by Stefan Evans:
Yes it is possible.
No, is is not advised.


Listen to Stefan for he speaks wisely.

Do yourself a favor and wean yourself away from coding any Java in JSP pages. You will live a happier life.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Rosario
Greenhorn

Joined: Mar 10, 2009
Posts: 2
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

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.

Ridiculous!
David Rosario
Greenhorn

Joined: Mar 10, 2009
Posts: 2
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.
Manan Sharma
Greenhorn

Joined: Dec 26, 2008
Posts: 2
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:

web.xml:


static.jsp:


when accessed through the URLs below, the 2 instances seem to run independent values of "i", even though it is a static/class variable.
http://localhost:8080/multi-jsp/staticB.jsp
http://localhost:8080/multi-jsp/staticA.jsp

Source Code of entire web app:
http://manan.50webs.com/multi-jsp.jar
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

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.
Manan Sharma
Greenhorn

Joined: Dec 26, 2008
Posts: 2
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

Surely you can find better things to investigate than technology that's been discredited for 7 years?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static methods and variable in jsp