Understanding JSP Actions


Java™ 2 Primer Plus
By Steven Haines, Steve Potts

Table of Contents
Chapter 23.  Component-Based JavaServer Pages

JSP actions are special tags that perform functions such as transferring control from one JSP to anther, including external HTML into a JSP, generating output, and manipulating JavaBeans.

The syntax of an action is strictly XML, which means that it is more restrictive than HTML. The additional strictness is in the following areas:

  • All names used in actions are case sensitive.

  • All tags must be terminated with a />.

  • Quotes must be used around values.

The simplest action is forward. When a forward is encountered, the JSP translator discards all information obtained from this JSP and transfers control to another URL, which may or may not be a JSP. Listing 23.1 shows a JSP that contains a forward action.

Listing 23.1 The ForwardTest.jsp File
 <html>  <head>  <title>Forward Test</title>  <body>  This is a forward test  <jsp:forward page="ForwardTest2.jsp" />  </body>  </html> 

When this JSP is called, all the normal processing begins, but it is interrupted and never displayed after the forward tag is encountered. The directory that searched for this new JSP is, by default, the same directory where the original JSP is located. Listing 23.2 shows the JSP page that was the object of the forward command.

Listing 23.2 The ForwardTest2.jsp File
 <%!      private java.util.Date sinceDate = new java.util.Date();      private java.util.Date getSinceDate()      {          return sinceDate;       }  %>  <html>  <head>  <title>Forward Test2</title>  <body>  <h2>Forward Test2</h2>  This page was first run on <%= getSinceDate() %>.  </body>  </html> 

This listing is just a simple date display, as shown here in Figure 23.2.

Figure 23.2. The JSP forward action transfers control immediately to an other JSP.


Notice that the URL in the address box of the browser didn't change, only the contents did.

There are four more actions that we will use in this chapter to provide the logical separation of the applications logic into layers:

  • include This action performs a text substitution that accepts parameters.

  • usebean This action instantiates a JavaBean (if not yet instantiated) and assigns it a name.

  • setProperty This action is used to set the values of a property in a JavaBean.

  • getProperty This action is used to get the value of a property in a JavaBean.

These actions will be used to support the inclusion of components in the JSPs covered in this chapter.


    Java 2 Primer Plus
    Java 2 Primer Plus
    ISBN: 0672324156
    EAN: 2147483647
    Year: 2001
    Pages: 332

    Similar book on Amazon

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