File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes JSP scriplet concept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "JSP scriplet concept " Watch "JSP scriplet concept " New topic
Author

JSP scriplet concept

Rajesh Pathak
Ranch Hand

Joined: Aug 15, 2002
Posts: 56
Hi friends,
I was trying this simple JSP code
<%int x=1;%>
Value of x is<%= x %>
I thought that I will get some error like "Undefined variable: x" ,because x is defined in scriplet and it will be accessible only within its own <% and %> .
But the code works fine and I get the value can someone help.
One other curiosity, is there any way in JSP so that I can make a variable accessible ONLY in limited portion of the page(similar to defining local variable inside methods in Java ).
Thanks in advance,


Rajesh(Bangalore,India)<br />SCJP2, SCWCD, SCEA, IBM-XML, UML-OOAD, IBM-Enterprise Connectivity with J2EE.
Ajai
Ranch Hand

Joined: May 29, 2002
Posts: 198
Hi ,
You can access any variable from expressions that you have declared in scriptlets as after translation code within both scriptlet as well as the expression goes into _jspService() method.

Yes u can declare variable with a local scope like this ....
<%
if(true)
{
String site="javaranch";
}
out.print(site);//will give error Undefined variable
%>
<%=site%>//again will give error

HTH....
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
A variable you define within a scriptlet will be a local variable in JSP's _jspService() method.
You can actually define a method inside your JSP by using <%! %>.
Mark Howard
Ranch Hand

Joined: Feb 14, 2001
Posts: 285
I'm totally new to the JSP world, but my early understanding is that you are able to declare Java variables within either scriptlet <% %> or declaration <%! %> tags. Java methods, on the other hand, can only be defined within declaration <%! %> tags.
- Mark
[ August 19, 2002: Message edited by: mark howard ]
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Originally posted by mark howard:
I'm totally new to the JSP world, but my early understanding is that you are able to declare Java variables within either scriptlet <% %> or declaration <%! %> tags.

Yes, but there's a difference. If you declare a variable inside a scriptlet, it's a local variable, but if it's declared inside the <%! %> tag, it's an instance variable of the translated JSP.
Rajesh Pathak
Ranch Hand

Joined: Aug 15, 2002
Posts: 56
Thanks, it was helpful!
Bernd Stransky
Ranch Hand

Joined: Nov 20, 2001
Posts: 47
look at the different JSP tag elements which fall into the category 'script code':
Declaration <%! code which goes into the servlet implementation class created by the JSP container. So this creates instance variables and methods of a class.
Scriptlet <% this code becomes part of the _jspService() method. Variables are allowed and they will become local variables of this method, i.e. the order is relevant (declare it before using it), initialize it before using it, and they will be re-initiliazed for each request (instance variables will only be once). Methods are not allowed here as Java does not allow methods inside methods (not regarding inner local classes).
Expression <%= they becomes part of _jspService() as well and are wrapped inside out.println().
I try to think of a class like the following when writing a JSP page:
class abc {
<%! declare variables and methods. %>
public void _jspService() {
<% expressions, including local variables %>
<%= single expression which generates output %>
}
}
Hope this helps,
Bernd
 
Consider Paul's rocket mass heater.
 
subject: JSP scriplet concept