Provide Best Programming Tutorials

JSP Directives

You can use JSP directives to instruct JSP engine on how to process the JSP code.

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 attribute1 = "value1"
attribute2 = "value2"
attributen = "valuen" %>


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 into the servlet when the page is translated to a servlet.The include directive must be placed where you want the file to be inserted.

taglib lets you define custom tags.

page attribute

The following are useful attributes for the page directive:

import specifies one or more packages to be imported for this page. For example, 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. plain.

buffer specifies the output stream buffer size. By default, it is 8KB. For example,the directive <%@ page buffer="10KB" %> specifies that the output buffer sizeis 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.

Below gives an example that shows how to use the page directive to import a class. The

<!-- ComputeLoan1.jsp -->
    <title>ComputeLoan Using the Loan Class</title>
    <%@ page import = "chapter40.Loan" %>
    <% double loanAmount = Double.parseDouble(
       double annualInterestRate = Double.parseDouble(
       int numberOfYears = Integer.parseInt(
       Loan loan = 
         new Loan(annualInterestRate, numberOfYears, loanAmount);
    Loan Amount: <%= loanAmount %><br />
    Annual Interest Rate: <%= annualInterestRate %><br />
    Number of Years: <%= numberOfYears %><br />
    <b>Monthly Payment: <%= loan.getMonthlyPayment() %><br />
    Total Payment: <%= loan.getTotalPayment() %><br /></b>

The directive <%@ page import ="chapter38.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 invoke the Loan object’s monthlyPayment() and totalPayment() methods to display monthly payment and total payment.

Leave a Reply

Close Menu