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. Typically JSP directives are included before the HTML <HEAD> tag of your JSP.

The page and include directives are described here, and the taglib directive is described tomorrow when Tag Libraries are studied in detail.

The include Directive

You use the include directive to insert the contents of another file into the Web page as the page is translated. 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.6 shows a JSP with an include directive to add an HTML banner on the page. The banner is shown in Listing 13.7

Listing 13.6. Full Text of dateBanner.jsp
<HTML>
  <HEAD>
    <TITLE>JSP Date Example with common banner</TITLE>
  </HEAD>
  <BODY>
    <%@ include file="banner.html" %>
    <BIG>
      Today's date is
      <%= new java.util.Date() %>
    </BIG>
  </BODY>
</HTML>

Listing 13.7. Full Text of banner.html
<P><TABLE border="0" width="600" cellspacing="0" cellpadding="0">
  <TR>
    <TD width="350"><H1>Temporal Information  </H1> </TD>
     <TD align="right" width="250"><IMG src="clock.gif"> </TD>
   </TR>
</TABLE></P>

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 WAR file.

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
DirectiveExampleEffect
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.*.
errorPage<%@ page errorPage="/agency/error.jsp" %>The server will return the indicated error page to the client should an uncaught exception be thrown by this page.
isErrorPage
<%@ page isErrorPage="true" %> 
lt;%@ 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 refer ences 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 uses page directives. The program in Listing 13.8 shows an example that displays the name of the Job Agency.

Listing 13.8. Full Text of name.jsp
<%@page import="java.util.*, javax.naming.*, agency.*" %>
<%@page errorPage="errorPage.jsp" %>
<%
   InitialContext ic = null;
   ic = new InitialContext();
   AgencyHome agencyHome = (AgencyHome)ic.lookup("java:comp/env/ejb/Agency");
   Agency agency = agencyHome.create();
%>
<HTML>
  <HEAD><TITLE>Agency Name</TITLE></HEAD>
  <BODY>
    <H1><%= agency.getAgencyName() %> </H1>
  </BODY>
</HTML>

NOTE

This is not the best way of coding this JSP. You will see in the next section how to use JavaBeans to remove the Java code and simplify this JSP.


Listing 13.8 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; these will be explained 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.8, perform the following steps:

1.
Add the Web components to the examples WAR file you created earlier or create a new WAR file. Use the New Web Component 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

3.
Select name.jsp as the JSP page.

4.
Set the page alias as /name.

5.
Add an EJB Reference for the agency EJB by selecting the examples Web Application JAR in the left hand pane and choosing the EJB Refs dialogue page. Figure 13.5 shows the EJB Reference page with the required settings. To add your EJB reference click on the Add button and in the popup Add New EJB Reference dialogue enter the coded Name ejb/Agency. Select the session bean and remote interface options and enter agency.AgencyHome and agency.Agency for the home and remote interfaces. When you have added the Agency EJB reference select this reference and choose the JNDI option at the bottom of the page and enter ejb/Agency as the EJB JNDI name.

Figure 13.5. Adding agency EJB Reference.


The new name.jsp Web page can now be deployed and accessed using the URL http://localhost:8000/examples/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.


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

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