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 processing.
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 tells the JSP engine to include the contents of another file inline (exactly at the location of directive usage) in the current jsp. Included file can be another jsp or a HTML file. Include directive is of the following form. <%@ include file = "filename" %> The following example includes the source code of the header.jsp in the current jsp.
<%@ include file="header.jsp" %> When the current jsp (the jsp in which this directive is used) receives the client request, the jsp engine 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 tell the container which tag library a specific JSP requires. It is also used to assign a prefix that 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" uri = "tld" %> {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. <%@ page attribute="value" attribute="value" …. %>
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. Example: -
<%@ page import="java.io.*, java.util.*" %>
OR
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
The key point is that it is the only attribute of the page directive that can occur multiple times in a translation unit.
session: -This attribute indicates to the jsp engine whether the current jsp takes part in an Http session. The default value is true. If we don’t want the jsp to participate in a 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-handling code from the main page and thus promotes reusability of exception handling mechanism. In this approach, a jsp uses the errorPage attribute of the page directive to delegate the exception to another JSP page that has the error handling code.
<%@ page errorPage="handler.jsp" %>
Once the above instruction is encountered jsp engine delegates the exception handling to handler.jsp if at all an exception is generated in the current jsp.
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 above example, when we write handler.jsp, in that file we must use the following statement.
<%@ page isErrorPage="true" %>
contentType:- This attribute specifies the MIME type of the output. The default value is text/html. If we want to change the MIME type we can say as follows.
<%@ page contentType = " image/gif" %>
