While expressions, scriptlets and declarations are used to define the logic to be executed at run-time(request processing time), directives are used to provide pages with information at translation time – when the jsp page is converted to a servlet.

Directives are used to specify the classes that are imported, to specify the error page used for catching exceptions, to import tag libraries and making them available to use on the page etc.

JSP directives provide general information about the jsp page to the JSP engine.
JSP directives do not generate code. They are not part of the logic within the JSP code. JSP directives provide directions and instructions to the container, telling it how to handle certain aspects of JSP specification. 

There are 3 types of directives according to jsp specification.

  •             Include directive
  •             Taglib directive
  •             Page directive

A JSP directive is of the form

<%@ directive attribute=value %>

Include Directive:- An include directive telis the JSP engine to include the contents of another file inline (exactly at the location of directive usage) in the current jsp. Include file can be another jsp or a HTML file. Include directive is of the following form 

<%@ include file = “filename” %>

The following example include the souce code of the header.jsp in the current.jsp


When the current jsp(the jsp in which this directive is used) receives the client request, the jsp enine reads the entire jsp before translation. Once include directive is encountered, the jsp engine copies the contents of the header.jsp into the current jsp inline. Then it translates the current jsp into a servlet. As this happens during the translation phase of the jsp, we say that directives are translation time instructions that are given to the jsp container(engine).
Taglib directive:- This directive is used to tel the container which tg library a specific JSP requires. It is also used to assign a prefix gthat is used within the JSP page to identify tags from a specific tag library. Now jsp engine can locate the code for these tag libraries and get them ready for use by the JSP page. Taglib directive is of the form

 <%@ taglib prefix=”name” url=”values”%>(More on this in custom tags).

Page Directive:- This directive informs the engine about the overall properties of a jsp page. This directive applies to the entire translation unit and not just to the page in which it is declared. This directive is of the following form.


Even though there are many attributes for the page directive, the most frequently used ones are discussed here.

Import:- This attribute is similar to the import statement in a normal java source code. Once this attribute is used , the jsp container inserts an import statement into the generated servlet for each of the packages declared using this attribute. We can import multiple packages in a single tag by using a comma-separated list of package names. We can also use multiple tags for readability.

Ex:- <%@ page import=”java.io.*”%>

Session:-  This attribute indicates to the jsp engine whether the current jsp takes part in a http session. The default value is true. If we don’t want the jsp to participate ina session, then we have to explicitly say

<%@page session = “false” %>

errorPage:- we can handle the exceptions in a jsp by writing try and catch blocks. However, the jsp specification provides cleaner approach. This approach separates the error hadling code from the main code from the main page and thus promotes resability of exception handling mechanism. In this approach, a jsp uses the error page attribute of the page directive to delegate the exception to another JSP page that has the error handling code.


isErrorpage:- This attribute conveys whether the current jsp can act as an error handler for any other jsp. By default this value is false. In the abouve example, when we write handler.jsp. In that file we must use the following statement.

<%@page isErrorPage=”true”>

Content Type:- This attribute specifies the MIME type of the output. The default values is text/html. If we want to change the MIME type we can say as follows.

<%@page contentType=”image/gif” %>

Post a Comment

 
Top