File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Explain Scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Explain Scope " Watch "Explain Scope " New topic

Explain Scope

Tejas Kansara
Ranch Hand

Joined: Dec 14, 2000
Posts: 38
Could anyone please explain me regarding scope of variables and object in a jsp file.
I am getting confused.
Dominic Steng�rd
Ranch Hand

Joined: Feb 05, 2001
Posts: 186
Hello Tejas!
When your jsp file is being converted into a servlet the jsp container puts all the code from the expressions, scriptlets and html in a service method called _jspService(). This method then talks to the service method of the created servlet. To understand this you need a basic understanding for how servlets work.
This implies that all the variables being declared in scriptlets in a jsp file, are put into the same _jspService() method in the created servlet, and therefor they are local to that method. This also means that a variable that has been declared in a scriptlet in a jsp file, will be reachable from all the other scriptlets and expressions in the same file.
If you need a global variable, then you should use declarations instead. Because when you declare a variable in a declaration, that variable will be put outside the _jspService() method and will therefor be global in this entire servlet.
Hope this was helpful for you.
Dominic Steng�rd
Sun Certified Java 2 Programmer

[This message has been edited by Dominic Steng�rd (edited May 30, 2001).]

Dominic Steng�rd<br />Sun Certified Java 2 Programmer
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

An excellent way of learning the how and why of implicit objects in JSP is to view the java source file of the servlet (the one that gets made by translating your JSP).

That's how I learned where request, response, application, etc... came from. It is also useful to see the scope of variables you define in scriplets and declaration tags.
I agree. Here's the link:
subject: Explain Scope
It's not a secret anymore!