JSP page directive

In this post, you will learn:

  • What is the use of page directive in JSP?
  • What are the commonly used attributes of page directive?
  • What is centralized error handling of JSP?

JSP page directive:

The page directive in JSP is used by programmers to get the structure of the auto generated servlet modified according to their requirements. It has following syntax:

<%@ page attributeName=”value” … %>

Commonly used attributes of the page directive are:

1. import: This attribute is used to get the specified packages imported in the auto generated servlet on behalf of the programmer. It can be used in the following ways:

<%@ page import=”pkgName.className” %>

<%@ page import=”pkgName.className,pkgName.className,…” %>

<%@ page import=”pkgName.*,pkgName.*,…” %>

2. extends: This attribute is used to get the specified class inherited in the auto generated servlet. By default, auto generated servlet extends a vendor’s class of type JSPBase. Sometimes an application programmer need to inherit his/her class in the auto generated servlet specially when some common functionality is required in more than on JSP pages. The common functionality is defined in a class which extends vendor’s class and this class is used as the super class of auto generated servlet. This attribute provides a means to the experienced programmers to add their customized functionality to the JSP pages. It is used as follows:

<%@ page extends=”className” %>

3. contentType: This attribute is used to set the contentType of the response in the _jspService() method of the auto generated servlet. It can be used in the following ways:

<%@ page contentType=”MIMEType” %>

<%@ page contentType=”MIMEType; charset=someCharacterSet” %>

4. session: This attribute is used to disable the creation of session implicit object in the _jspService() method of the auto generated servlet. As you already know that session is one of the 9 implicit objects which are made available in the _jspService() method. If some pages of an application can be viewed by the users as visitors i.e. without user login as you are doing right now then session object must not be created on the server. To disable the creation of session in such scenarios this attribute is provided, it is used as follows:

<%@ page session=”false” %>

5. isErrorPage: This attribute is used to mark a JSP page as error handler. It is used to manage exceptions which occurs in the processing of other pages of the application. Exceptions which occurs in the processing of other JSP pages, are made available to it as the exception implicit object. It is be used as follows:

<%@ page isErrorPage=”true” %>

6. errorPage: This attribute is used to provide the url of the error handler page to other JSP pages. Whenever an exception occurs in those pages, request is forwarded to the specified error handler page. It is be used as follows:

<%@ page errorPage=”URL of errorHandlerPage” %>

With the help of isErrorPage & errorPage attributes, the page directive supports centralized error handling. One page of the application is designated as error handler and its URL is made available to all the other pages. When an exception occurs in those pages, the request is forwarded to the error handler page and the exception is made available to it as implicit object. Following practical example, demonstrate the centralized exception handling. In this example, handler.jsp is the error handler page which manages the exceptions of divider.jsp page.

First, the index.html :


<form method="post" action="divider.jsp">
First No: <input type="text" name="num1"><br/>
Second No: <input type="text" name="num2"><br/>
<input type="submit" value="divide">
</form>

Second, the divider.jsp in which NumberFormatException & ArithmeticException may occur:

<%@ page errorPage="/handler.jsp" %>
<%
int a=Integer.parseInt(request.getParameter("num1"));
int b=Integer.parseInt(request.getParameter("num2"));
int c=a/b;
out.println("Result is: "+c);
%>
<hr/>
<%@ include file="index.html" %>

Third, the handler.jsp which manages the exceptions of divider.jsp:

<%@ page isErrorPage="true" %>
<%
if (exception instanceof ArithmeticException)
out.println("Second number must be non zero.");
else
out.println("Input must be numeric.");
%>
<hr/>
<%@ include file="index.html" %>

Its time to recap the main concepts:

  1. The page directive is used to get the structure of the auto generated servlet modified.
  2. It supports multiple attributes most commonly used among them are: import, extends, contentType, session, errorPage and isErrorPage.
  3. With the help of errorPage & isErrorPage attributes, centralized exception handling is supported in JSP.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *