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" %>
A page directive defines page-dependent attributes, such as scripting language, error page, and buffering requirements.
Some common used attributes are: import, contentType, errorPage and session.
-
Right click on the WebContent folder, select New→JSP file.
Name it pagedirective.jsp and click Finish.
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 %>
Save the file.
Run it by right clicking on it then selecting Run As→Run on Server. Select Finish.
-
Have a look on the first line of the
pagedirective.jsp file:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
-
Now, we are going to examin the errorPage directive.
Change the text/xml back to text/html.
Replace the current codes between the <body> tags by the following codes:
<%!
private double toDouble(String value) {
return(Double.parseDouble(value));
}
%>
<%= toDouble("two") %>
-
Now, create in the WebContent folder a file: error.jsp and simply insert a text “Error!” between the <body> tags.
Back to the
pagedirective.jsp, insert the following line right after the first
<body> tag.
<%@ page errorPage="error.jsp" %>
-
Include directive: <%@ include attribute="value" %>
An include directive is used to
include other JSP pages, HTML or text contents into a current JSP.
Right click on the WebContent folder, select New→JSP file.
Name it includedirective.jsp and click Finish.
Add the following codes between the
<body> tags.
Page content is here
<%@ include file="footer.jsp" %>
Save the file.
Create another JSP file in the same WebContent folder, name it footer.jsp.
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 © 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(); %>
Save the file.
Run the includedirective.jsp file by right clicking on it then selecting Run As→Run on Server. Select Finish.
-
Exercises
Remove the code “<%@ page errorPage=“error.jsp” %>” in the pagedirective.jsp file. Then re-run it to see the errors.
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.”
Back to top