File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes JSP variables -> variable scope in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP variables -> variable scope in servlet" Watch "JSP variables -> variable scope in servlet" New topic
Author

JSP variables -> variable scope in servlet

Paula Davis
Greenhorn

Joined: Apr 16, 2003
Posts: 16
A variable that is created within a JSP scriptlet... in the compiled servlet, is this variable local to the service() method? i.e, if I declare a separate method within a declaration tag in the same JSP, can it access the same variables (variables declared/used within a plain ol' scriptlet) without having to have them passed in as arguments? If not, would it make them global if I declare the variable(s) within a declaration tag rather than a scriptlet, or is there another way to make those variables global in the servlet (besides editing the servlet directly)? I realize that it is bad form to have a large amount of straight Java code in a JSP, but I am actually modifying someone else's code, and there is a limit to how much shuffling I'm allowed to do. I'm hoping to at least take a page and a half of "if" code and another page and a half of "else" code and parcel them out to separate methods so that the actual "if... else" will not be so strung out... i.e,
if (condition)
{
doStuff();
}
else
{
doOtherStuff();
}
and move the other 3 pages of code into declarations for doStuff() and doOtherStuff()
Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

Hi Paula,
Welcome to the Ranch!
A variable that is created within a JSP scriptlet... in the compiled servlet, is this variable local to the service() method?

Yes.
One thing I would suggest doing before too much longer is to find out where your servlet container is writing the Java files that it creates from your JSPs, and taking a gander at them to see how a servlet is created from a JSP. Very informative, and will help you be a better JSP writer.
if I declare a separate method within a declaration tag in the same JSP, can it access the same variables (variables declared/used within a plain ol' scriptlet) without having to have them passed in as arguments?

No. The method will be declared as a method of the servlet but will have no access to the local variables defined in service(); just as with any other method.
If not, would it make them global if I declare the variable(s) within a declaration tag rather than a scriptlet

Yes, but you don't want to do that. Each servlet (whether generated from a JSP or not) has a single instance created for it, and creating instance variables for the JSP will introduce a major mess of multi-threading issues. I'd suggest just bucking up and passing info along as params.
Another alternative is to stick them as attributes on the request, but passing them as params will be a lot more straight-forward.
hth,
bear
[ May 19, 2003: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paula Davis
Greenhorn

Joined: Apr 16, 2003
Posts: 16
Thanks, Bear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP variables -> variable scope in servlet
 
Similar Threads
request.getParameter scope issue
New forum for Sun Certified Web Component Developer
Sun Certified Web Component Developer
Mock exams for JSP
Variable defined in one JSP scriptlet not visible in another