Java
  Home arrow Java arrow Page 3 - JavaServer Pages
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

JavaServer Pages
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2005-11-03

    Table of Contents:
  • JavaServer Pages
  • Writing JSP Pages
  • Scriptlets
  • Template Data
  • Try It Out: Deploying the Web Application in J2EE
  • Try It Out: Deploying the Web Application in Tomcat
  • Action Elements

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    JavaServer Pages - Scriptlets


    (Page 3 of 7 )

    Scriptlets contain Java code statements. The code in the scriptlet appears in the translated JSP, but not in the output to the client. Any legal Java code statements can appear within a scriptlet. For example, to repeat the phrase "Hello, World!" ten times in the output page, you could use this scriptlet:

      <%
       
    for (int i = 0; i < 10; i++) {
      %>
      Hello, World!
      <%
       
    }
      %>

    As in this code snippet, we can freely interleave Java code and HTML and/or text data. Everything between the scriptlet markers (<% and %> ) is script code; everything outside the markers is template data, which is sent to the client as written. Notice that in the above example the Java code block does not need to begin and end within the same scriptlet element. This allows you complete freedom to mix Java code and HTML elements as needed within the page.

    The above example is relatively simple. However, as your application gets more complicated and involved, youll get more and more code mixed in with the HTML and the page will tend to get complicated. In the next chapter, we will see how tag libraries can give the same rich behavior as above, but using only XML tags.

    Since scriptlets can contain Java statements, the following is a legal scriptlet:

      <%
      Vector v = new Vector();
      // more code...
      %>

    This looks very similar to the code snippet in the declaration section that preceded this section. This might lead you to wonder what the difference between scriptlets and declarations is, since they appear to be the same. Despite that seeming similarity, they are different in the following ways:

    • Scriptlets cannot be used to define a method; only declarations can be used for that.
    • Variables declared in a declaration are instance variables of the JSP page implementation class. These variables are visible to all other code statements or methods in the page.
    • Variables declared in a scriptlet are local to a method in the JSP page implementation class. They are visible only within their defining code block.

       

    Expressions

    Expressions are used to output the value of a Java expression to the client. For example, this code fragment in a JSP:

      The number of tokens in this statement is <%= countTokens   ("The number of
      tokens in this statement is n") %>.

    would result in the text "The number of tokens in this statement is 9." being displayed in the browser. The code snippet above calls the hypothetical countTokens(String) method that was shown in the declaration section previously. To count the number of tokens in the statement, a literal copy of the statement is passed to the method. In the code snippet above, the method call returned an int value, which was printed to the clients browser. Here is the same expression using XML style:

      The number of tokens in this statement is 
      <jsp:expression>
         countTokens("The number of tokens in this statement 
      is n")
      </jsp:expression>.

    Any legal Java expression can be used with an expression element. An expression could contain a method call, as shown above, or a literal expression such as '2 + 2', or an expression using Java variables or keywords such as 'v instanceof Vector', or any combination of these. Notice also that because declarations and scriptlets contain Java code, the lines of Java code must be terminated with a semicolon. Expressions, however, will not necessarily be legal code statements (but they will be valid expressions), so they do not need a terminating semicolon.

    Comments

    You can use standard HTML comments within the JSP and those comments will appear in the page received by the client browser. Standard HTML comments have this form:

     <!-- This comment will appear in the client's browser >

    You can also include JSP-specific comments that use this syntax:

      <%-- This comment will NOT appear in the client's 
      browser %>

    JSP comments will not appear in the page output to the client.

    More Java Articles
    More By Apress Publishing


       · This article is an excerpt from the book "Beginning J2EE 1.4 From Novice to...
     

    Buy this book now. This article is excerpted from chapter three of Beginning J2EE 1.4 From Novice to Professional, written by James L. Weaver, Kevin Mukhar, and Jim Crume (Apress, 2004; ISBN: 1590593413). Check it out at your favorite bookstore today. Buy this book now.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek