JSP directives: page and include

JSP directives provide directions and instructions to the container, telling it how to handle certain aspects of JSP processing. In this tutorial, we will study the page directive and include directive in JSP.

Page directive: <%@ page attribute="value" %>

  1. A page directive defines page-dependent attributes, such as scripting language, error page, and buffering requirements.
  2. Some common used attributes are: import, contentType, errorPage and session.
  3. Open the jsplabs project created in previous tutorial.
  4. Right click on the WebContent folder, select New→JSP file.
  5. Name it pagedirective.jsp and click Finish.
  6. Add the following codes between the <body> tags.

    <%@ page import="java.util.*,java.text.*" %>
    <%
        Date date = new Date();
        // if we do not use the page import directive,
        // we will write: java.util.Date date = new java.util.Date();
    %>
    Hello!  The time is now <%= date %>
  7. Save the file.
  8. Run it by right clicking on it then selecting Run As→Run on Server. Select Finish.
  9. The current time will be presented on your web browser at the URL: http://localhost:8080/jsplabs/pagedirective.jsp
  10. Have a look on the first line of the pagedirective.jsp file:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
  11. It is also a page directive. Change text/html to text/xml and remove the meta tag. Then refresh the page http://localhost:8080/jsplabs/pagedirective.jsp to see what will change.
  12. Now, we are going to examin the errorPage directive.
  13. Change the text/xml back to text/html.
  14. Replace the current codes between the <body> tags by the following codes:

    <%! 
    private double toDouble(String value) {
    return(Double.parseDouble(value));
    }
    %>
    <%= toDouble("two") %>
  15. Save the file and refresh the URL: http://localhost:8080/jsplabs/pagedirective.jsp. You will get errors.
  16. Now, create in the WebContent folder a file: error.jsp and simply insert a text “Error!” between the <body> tags.
  17. Back to the pagedirective.jsp, insert the following line right after the first <body> tag.

    <%@ page errorPage="error.jsp" %>
  18. Save the file and refresh the URL: http://localhost:8080/jsplabs/pagedirective.jsp.

Include directive: <%@ include attribute="value" %>

  1. An include directive is used to include other JSP pages, HTML or text contents into a current JSP.
  2. Right click on the WebContent folder, select New→JSP file.
  3. Name it includedirective.jsp and click Finish.
  4. Add the following codes between the <body> tags.

    Page content is here
    <%@ include file="footer.jsp" %>
  5. Save the file.
  6. Create another JSP file in the same WebContent folder, name it footer.jsp.
  7. Remove all current content and insert the following codes:

    <%@ page import="java.util.Date" %>
    <%-- The following become fields in each servlet that
    results from a JSP page that includes this file. --%>
    <%! 
    private int accessCount = 0;
    private Date accessDate = new Date();
    private String accessHost = "<I>No previous access</I>";
    %>
    <P>
    <HR>
    This page &copy; 2008 <A HREF="http//www.my-company.com/">my-company.com</A>.
    This page has been accessed <%= ++accessCount %> times since server reboot. 
    It was most recently accessed from <%= accessHost %> at <%= accessDate %>. 
    <% accessHost = request.getRemoteHost(); %> <% accessDate = new Date(); %>
  8. Save the file.
  9. Run the includedirective.jsp file by right clicking on it then selecting Run As→Run on Server. Select Finish.
  10. The page including the footer will be presented on your web browser at the URL: http://localhost:8080/jsplabs/includedirective.jsp


Exercises

  1. Remove the code “<%@ page errorPage=“error.jsp” %>” in the pagedirective.jsp file. Then re-run it to see the errors.
  2. Assume that a client request the pagedirective.jsp with a parameter num in the URL (in format: pagedirective.jsp?num=ten). Add a function in the pagedirective.jsp file to check whether the parameter in the URL is a number or a string. If it is a string, open a page saying that: “The parameter should be a number.”
teaching_assistant/servlet-jsp/jsp-directive.txt · Last modified: 2013/07/08 08:44 (external edit)
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0