scriptlet Tag

scriptlet Tag

The scriptlet tag is the most important tag in the JSP specification since it enables the actual embedding of Java code within an HTML page. The Java code within the scriptlet tag is put by the JSP parser in the service() method of the resultant servlet generated.

Structure of the scriptlet Tag

The structure of the scriptlet tag is as follows:

 <%  // write all the Java statements %> 

or

 <jsp:scriptlet>  // java code </jsp:scriptlet> 

The Java code within the scriptlet tag is placed in the service() method of the servlet generated from the JSP. Any HTML code before or after these tags is converted into out.println(...) statements.

This example represents the true power of JSP tags:

 <BODY>  <!--list the names of the books in the shop --> <% for(int i=0; i < bookVector.size(); i++) {      out.println((String)bookVector.elementAt(i)); } %> 

or

 <BODY>  <!--list the names of the books in the shop --> <jsp:scriptlet> for(int i=0; i < bookVector.size(); i++) {      out.println((String)bookVector.elementAt(i)); } </jsp:scriptlet> 

As you can see, there is a Vector of book names named bookVector. You will be printing the contents of the bookVector on the browser. The entire Java code is put within the scriptlet tags. This Java code will be put in the service() method of the JSP's servlet. Note that the following statement used a new technique to print the output in the browser:

 out.println((String)bookVector.elementAt(i));  

The out variable is the PrintWriter stream of the HttpResponse object that can be used to send data to the browser. The out variable is called an implicit variable in JSP. You will be studying implicit variables in the section "Implicit Variables" later today.



Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net