JSP Directives

Directives are used to define information about your page to the translator, they do not produce any HTML output. All directives have the following syntax:

<%@ directive [ attr="value" ] %>

where directive can be page, include, or taglib.

The page and include directives are described later, and the taglib directive is described tomorrow (Day 14) when Tag Libraries are studied in details.

The include Directive

You use the include directive to insert the contents of another file into the JSP. The included file can contain HTML or JSP tags or both. It is a useful mechanism for including the same page directives in all your JSPs or reusing small pieces of HTML to create common look and feel.

If the include file is itself a JSP, it is standard practice to use .jsf or .jspf, as suggested in the JSP specification, to indicate that the file contains a JSP fragment. These extensions show that the file is to be used in an include directive (and does not create a well-formed HTML page). “.jsp" should be reserved to refer to standalone JSPs.

Listing 13.4 shows a JSP with an include directive to add an HTML banner on the page. The banner is shown in Listing 13.5

Listing 13.4. Full Text of dateBanner.jsp
 1: <HTML>
 2:   <HEAD>
 3:     <TITLE>JSP Date Example with common banner</TITLE>
 4:   </HEAD>
 5:   <BODY>
 6:     <%@ include file="banner.html" %>
 7:     <BIG>
 8:       Today's date is
 9:       <%= new java.util.Date() %>
10:     </BIG>
11:   </BODY>
12: </HTML>

Listing 13.5. Full Text of banner.html
1: <TABLE border="0" width="600" cellspacing="0" cellpadding="0">
2:   <TR>
3:     <TD width="350"><H1>Temporal Information  </H1> </TD>
4:      <TD align="right" width="250"><IMG src="clock.gif"> </TD>
5:    </TR>
6: </TABLE>
7: <BR>
						

Remember that you must add any include files into the Web application as well as the JSP file. In this example, you will need to add banner.html and the image file clock.gif to the Web Application.

The page Directive

Page directives are used to define page-dependent properties. You can have more than one page directive in the JSP. A page directive applies to the whole JSP, together with any files incorporated via the include directive. Table 13.2 defines a list of the more commonly used page directives.

Table 13.2. JSP Page Directives
Directive Example Effect
Info <%@ page info="my first JSP Example" %> Defines text string that is placed in the Servlet.getServletInfo() method in the translated code
import <%@ page import=" java.math.*" %> A comma-separated list of package names to be imported for this JSP. The default import list is java.lang.*, javax.servlet.*, javax.servlet.jsp.*, and javax.servlet.http.*.
isThreadSafe
<%@ page isThreadSafe="true" %>
<%@ page isThreadSafe="false" %>

If set to true, this indicates that this page can be run multithreaded. This is the default, so you should ensure that access to shared objects (such as instance variables) is synchronized.
errorPage <%@ page errorPage="/agency/error.jsp" %> The client will be redirected to the specified URL when an exception occurs that is not caught by the current page.
isErrorPage
<%@ page isErrorPage="true" %>
<%@ page isErrorPage="false" %>

Indicates whether this page is the target URL for an errorPage directive. If true, an implicit scripting variable called “exception” is defined and references the exception thrown in the source JSP. The default is false.

Using the Agency case study as an example, you can now study a JSP that needs to use page directives. The program in Listing 13.6 shows an example that displays the name of the Job Agency.

Listing 13.6. Full Text of name.jsp
 1: <%@page import="java.util.*, javax.naming.*, agency.*" %>
 2: <%@page errorPage="errorPage.jsp" %>
 3: <HTML>
 4: <TITLE>Agency Name</TITLE>
 5: <BODY>
 6: <%
 7:         InitialContext ic = null;
 8:         ic = new InitialContext();
 9:         AgencyHome agencyHome = (AgencyHome)ic.lookup("java:comp/env/ejb/Agency");
10:         Agency agency = agencyHome.create();
11: %>
12: <H1><%= agency.getAgencyName() %> </H1>
13: </BODY>
14: </HTML>
						

Listing 13.6 uses the agency Session bean you developed on Day 5, “Session EJBs,” to obtain the name of the agency. Because this JSP uses JNDI and EJB features, it must import the relevant Java packages by using a page directive. This example also uses an error page that is displayed if there is an uncaught exception on the page.

Note

The JSP page does not catch the obvious NamingException and EJBException exceptions that can be thrown by the code in the scriptlet—the JSP error page is used for this purpose.


The error page contains JSP features that you have not yet encountered and will be shown later.

Deploying this example is a little more complex than the simple date example because it must include the additional class files required for the agency Session bean.

To deploy the example in Listing 13.6, perform the following steps:

1.
Create a new Web Component in the simple WAR file you created earlier and use the wizard to define the information shown in step 2.

2.
Add the following files for this Web component:

  • name.jsp

  • errorPage.jsp

  • agency/Agency.class

  • agency/AgencyHome.class

  • agency/DuplicateException.class

  • agency/NotFoundException.class

Figure 13.8 shows the files in the WAR file for the simple Web application.

Figure 13.8. Component files for the simple Web application.


3.
Select name.jsp as the JSP page.

4.
Set the page alias as /name.

5.
Add an EJB Reference for the agency EJB. Figure 13.9 shows the EJB Reference page with the required settings.

Figure 13.9. Agency EJB Reference for the simple Web application.


The new name.jsp Web page can now be deployed and accessed using the URL http://localhost:8000/simple/name.

Note

You must have previously deployed the agency application; otherwise, the Web interface will not be able to find the agency Session bean. Any version of the agency application will suffice, the one from Day 10, “Message-Driven Beans,” is the one with most functionality.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.124.21