Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | Directives - JavaServer Pages (JSP)

Chapter: Internet & World Wide Web HOW TO PROGRAM - JavaServer Pages (JSP): Bonus for Java™ Developers

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Directives - JavaServer Pages (JSP)

Directives are messages to the JSP container that enable the programmer to specify page settings (such as the error page), to include content from other resources and to specify cus-tom-tag libraries for use in a JSP.

Directives

 

Directives are messages to the JSP container that enable the programmer to specify page settings (such as the error page), to include content from other resources and to specify cus-tom-tag libraries for use in a JSP. Directives (delimited by <%@ and %>) are processed at translation time. Thus, directives do not produce any immediate output, because they are processed before the JSP accepts any requests. Figure 31.26 summarizes the three directive types. These directives are discussed in the next several subsections.

 

1 page Directive

 

The page directive specifies global settings for the JSP in the JSP container. There can be many page directives, provided that there is only one occurrence of each attribute. The only exception to this rule is the import attribute, which can be used repeatedly to import Java packages used in the JSP. Figure 31.27 summarizes the attributes of the page directive.






 

2. include Directive

 

The include directive includes the content of another resource once, at JSP translation time. The include directive has only one attribute—file—that specifies the URL of the page to include. The difference between directive include and action <jsp:in-clude> is noticeable only if the included content changes. For example, if the definition of an XHTML document changes after it is included with directive include, future invo-cations of the JSP will show the original content of the XHTML document, not the new content. In contrast, action <jsp:include> is processed in each request to the JSP. Therefore, changes to included content would be apparent in the next request to the JSP that uses action <jsp:include>.

 

JavaServer Page includeDirective.jsp (Fig. 31.28) reimplements JavaServer Page include.jsp (Fig. 31.10) using include directives. To test includeDirec-tive.jsp in Tomcat, copy includeDirective.jsp into the jsp directory created in Section 31.3. Open your Web browser and enter the following URL to test include-Directive.jsp:

 

http://localhost:8080/advjhtp1/jsp/includeDirective.jsp

 

      <?xml version = "1.0"?>

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

 

    <!-- Fig. 10.28: includeDirective.jsp -->

 

    <html xmlns = "http://www.w3.org/1999/xhtml">

 

      <head>

<title>Using the include directive</title>

    

     <style type = "text/css">

     body {

     font-family: tahoma, helvetica, arial, sans-serif;

     }

    

     table, tr, td {

     font-size: .9em;

     border: 3px groove;

     padding: 5px;

     background-color: #dddddd;

     }

     </style>

     </head>

 

     <body>

     <table>

     <tr>

     <td style = "width: 160px; text-align: center">

     <img src = "images/logotiny.png"

     width = "140" height = "93"

     alt = "Deitel & Associates, Inc. Logo" />

     </td>

    

     <td>

    

     <%-- include banner.html in this JSP --%>

     <%@ include file = "banner.html" %>

    

     </td>

     </tr>

    

     <tr>

     <td style = "width: 160px">

    

     <%-- include toc.html in this JSP --%>

     <%@ include file = "toc.html" %>

    

     </td>

    

     <td style = "vertical-align: top">

    

     <%-- include clock2.jsp in this JSP --%>

     <%@ include file = "clock2.jsp" %>

    

     </td>

     </tr>

     </table>

     </body>

     </html>

 


 

Fig. 31.28 JSP includeDirective.jsp demonstrates including content at translation-time with directive include


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.