Page #478 (35.5. Predefined Variables)

 
[Page 1215 ( continued )]

35.6. JSP Directives

A JSP directive is a statement that gives the JSP engine information about the JSP page. For example, if your JSP page uses a Java class from a package other than the java.lang package, you have to use a directive to import this package. The general syntax for a JSP directive is shown below:

   <%@ directive   attribute =   "value"   %>   , or   <%@ directive   attribute  1  =   "value  1  "   attribute  2  =   "value  2  "   ... attribute  n  =   "value  n  "   %>   


[Page 1216]

The possible directives are:

  • page lets you provide information for the page, such as importing classes and setting up content type. The page directive can appear anywhere in the JSP file.

  • include lets you insert a file to the servlet when the page is translated to a servlet. The include directive must be placed where you want the file to be inserted.

  • tablib lets you define custom tags.

The following are useful attributes for the page directive:

  • import specifies one or more packages to be imported for this page. For example, the directive <%@ page import="java.util.*, java.text.*" %> imports java.util.* and java.text.* .

  • contentType specifies the content type for the resultant JSP page. By default, the content type is text/html for JSP. The default content type for servlets is text/plain .

  • session specifies a boolean value to indicate whether the page is part of the session. By default, session is true .

  • buffer specifies the output stream buffer size . By default, it is 8KB. For example, the directive <%@ page buffer="10KB" %> specifies that the output buffer size is 10KB. The directive <%@ page buffer="none" %> specifies that a buffer is not used.

  • autoFlush specifies a boolean value to indicate whether the output buffer should be automatically flushed when it is full or whether an exception should be raised when the buffer overflows. By default, this attribute is true . In this case, the buffer attribute cannot be none .

  • isThreadSafe specifies a boolean value to indicate whether the page can be accessed simultaneously without data corruption. By default, it is true . If it is set to false , the JSP page will be translated to a servlet that implements the SingleThreadModel interface.

  • errorPage specifies a JSP page that is processed when an exception occurs in the current page. For example, the directive <%@ page errorPage="HandleError.jsp" %> specifies that HandleError.jsp is processed when an exception occurs.

  • isErrorPage specifies a boolean value to indicate whether the page can be used as an error page. By default, this attribute is false .

35.6.1. Example: Importing Classes

This example shows how to use the page directive to import a class. The example uses the Loan class created in §7.15, "Case Study: The Loan Class," to simplify Listing 35.3, ComputeLoan.jsp. You can create an object of the Loan class and use its getMonthlyPayment() and getTotalPayment() methods to compute the monthly payment and total payment. The new ComputeLoan.jsp is shown in Listing 35.4.

Listing 35.4. ComputeLoan1.jsp
(This item is displayed on pages 1216 - 1217 in the print version)
 1  <!-- ComputeLoan1.jsp -->  2   <html>   3   <head>   4   <title>   ComputeLoan Using the Loan Class   </title>   5   </head>   

[Page 1217]
 6 <   body>   7    <%@ page import   =   "chapter35.Loan"   %>    8   <% double   loanAmount = Double.parseDouble( 9 request.getParameter(   "loanAmount"   )); 10   double   annualInterestRate = Double.parseDouble( 11 request.getParameter(   "annualInterestRate"   )); 12   int   numberOfYears = Integer.parseInt( 13 request.getParameter(   "numberOfYears"   )); 14  Loan loan =  15    new   Loan(annualInterestRate, numberOfYears, loanAmount);  16   %>   17 Loan Amount:   <%=   loanAmount   %><br />   18 Annual Interest Rate:   <%=   annualInterestRate   %><br />   19 Number of Years :   <%=   numberOfYears   %><br />   20   <b>   Monthly Payment:    <%=   loan.getMonthlyPayment()   %>      <br />   21 Total Payment:    <%=   loan.getTotalPayment()   %>      <br /></b>   22   </body>   23   </html>   

To import a class, the class must be placed in a package explicitly. Create a new Loan class in package chapter35 as follows :

    package   chapter35;     public class   Loan  {  // Same as Listing 7.9, Loan.java, so omitted  } 

Compile it into c:\jakarta-tomcat-5.5.9\webapps\liangweb\WEB-INF\classes\chapter35.

Tip

The destination directory for Loan.class is c:\jakarta-tomcat-5.5.9\webapps\liangweb\WEB-INF\classes . You may create a subdirectory named chapter35 under the destination directory and then move Loan.class into it. As an alternative, you can use the following command to compile and save the .class directly into the destination directory:

   javac “d chapter35 Loan.java   

This command automatically creates the subdirectory chapter35, since it is in the package statement of the Loan class.


The directive <%@ page import ="chapter35.Loan" %> imports the Loan class in line 7. Line 14 creates an object of Loan for the given loan amount, annual interest rate, and number of years. Lines 20 “21 invokes the Loan object's getMonthlyPayment() and getTotalPayment() methods to display monthly payment and total payment.

35.6.2. Example: Using Error Pages

This example prompts the user to enter an integer (see Figure 35.5(a)) and displays the factorial for the integer (see Figure 35.5(b)). If a noninteger value is entered by mistake, an error page is displayed, as shown in Figure 35.5(c).

Create three files named FactorialInput.html (Listing 35.5), ComputeFactorial.jsp (Listing 35.6), FactorialInputError.jsp (Listing 35.7), and save the files into c:\jakarta-tomcat-5.5.9\webapps\liangweb . Display FactorialInput.html first, as shown in Figure 35.5(a).


[Page 1218]
Figure 35.5. You enter an integer to obtain its factorial in (a). The factorial of the integer is displayed in (b). An error page is displayed in (c) when an exception occurs.

Listing 35.5. FactorialInput.html
 1  <!-- FactorialInput.html -->  2   <html>   3   <head>   4   <title>   5 FactorialInput 6   </title>   7   </head>   8   <body>   9   <form method   =   "post"      action   =   "/liangweb/ComputeFactorial.jsp"    > 10 Enter an integer   <input name   =   "number"   /><br /><br />   11   <input type   =   "submit"   name   =   "Submit"   12   value   =   "Compute Factorial"   />   13   <input type   =   "reset"   value   =   "Reset"   />   14   </form>   15   </body>   16   </html>   

Listing 35.6. ComputeFactorial.jsp
(This item is displayed on pages 1218 - 1219 in the print version)
 1  <!-- ComputeFactorial.jsp -->  2   <html>   3   <head>   4   <title>   5 ComputeFactorial 6   </title>   7   </head>   8   <body>   9    <%@ page import   =   "java.text.*"   %>    10    <%@ page errorPage   =   "FactorialInputError.jsp"   %>    11 12   <%    NumberFormat format = NumberFormat.getNumberInstance();  13 int number = 14 Integer.parseInt(request.getParameter(   "number"   ));   %>   15 Factorial of   <%=   number   %>   is 16   <%=   format.format(computeFactorial(number))   %>   17 18   <%! private long   computeFactorial(   int   n) { 19   if   (n ==     ) 20   return   1   ; 

[Page 1219]
 21   else   22   return   n * computeFactorial(n -   1   ); 23 } 24   %>   25   </body>   26   </html>   

Listing 35.7. FactorialInputError.jsp
 1  <!-- FactorialInputError.jsp -->  2   <html>   3   <head>   4   <title>   5 FactorialInputError 6   </title>   7   </head>   8   <body>   9    <%@ page isErrorPage   =   "true"   %>    10 11   <b>   Error   </b>   ” ” Input is not an integer. 12 13   </body>   14   </html>   

FactorialInput.html is displayed first to prompt the user to enter an integer. Upon clicking the Compute Factorial button, the JSP page ComputeFactorial.jsp is invoked to compute the factorial for the number. If the user enters an integer, its factorial is displayed; otherwise , the error page is displayed.

In ComputeFactorial.jsp, the directive <%@ page import ="java.text.*" %> (line 9) imports the java.text package because the NumberFormat class is in this package. The directive <%@ page errorPage ="FactorialInputError.jsp" %> (line 10) specifies that FactorialInputError.jsp is processed when an exception in ComputeFactorial.jsp occurs. The directive <%@ page isErrorPage ="true" %> (line 9 in FactorialInputError.jsp) denotes that FactorialInputError.jsp can be used as an error page.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

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