Engineering Full Stack Apps with Java and JavaScript
Directives are elements that provide the JSP translator with directions on how it should treat the JSP page being translated.
Our simplejsp from previous demo had the below page directive in it:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
A directive starts with <%@ followed by the directive name (e.g. page), some attributes with values (e.g. language="java") and ends with %>
Important directives are page, include and taglib directives.
The page directive provides information about the page to the translator.
Important attributes for the page directive
import
to create import statements in the generated servlet source produced by the JSP container’s translation phase.
We can specify comma-separated list of classnames or packages as value of the import statement.
Import attribute can be specified more than once—either across separate page directives that contain import once or even using the import attribute more than once in the same page directive.
Ex: <%@ page import="java.util.StringTokenizer" %>
Few packages are already available in the generated servlet source produced by the JSP container’s translation phase and you don’t have to import again:
java.lang
javax.servlet
javax.servlet.http
javax.ervlet.jsp
session
used to determine whether an HttpSession object is available within your JSP page source through an implicit object session.
The default value is true.
If your JSP page genuinely doesn’t need access to the session, there’s a small performance gain to be made with disabling this as we can eliminate the time spent on creating or obtaining an HttpSession object.
Ex: <%@ page session="true" %>
contentType
used to set the content type. Same as calling response.setContentType() from a servlet.
Ex: <%@ page contentType="image/gif" %>
isELIgnored
you can switch off EL evaluation and have the text be treated as template text.
The JSP 2.0 sets a default of isELIgnored="true" for backward compatibility.
However if the deployment descriptor web.xml is at the version level of 2.4, the default of isELIgnored is set to “false”.
Ex: <%@ page isELIgnored="true" %>
language
Denotes the scripting language. “Java” is the only value supported.
Ex: <%@ page language=“Java” %>
buffer, autoFlush
You can use these attributes to control whether or not you have a buffer (you can specify a size in kilobytes), and how this buffer is flushed.
Ex: <%@ page buffer=“none” autoFlush=“true” %>
isThreadSafe
Is set with the true which enables for accessing multiple request and give multiple responses simultaneously by generating multiple threads for your JSP application and developer is responsible for implementing thread safety.
Is set to false means container is responsible for thread safety, it makes the servlet to implement the SingleThreadModel with this every client request will have their own instances of the servlet hence making the JSP thread safe.
Ex: <%@ page isThreadSafe=“true” %>
errorPage, isErrorPage
Use errorPage to set a URL pointing to another JSP within the same web application. Should an exception occur, your users will be forwarded to this other JSP.
The page you forward to must have “isErrorPage” set to true.
Ex: <%@ page errorPage=“errorPage.jsp” %>
Ex: <%@ page isErrorPage=“true” %>
extends
override the base servlet that your container provides when generating servlets
info
publish information about your JSP page, accessible through the getServletInfo() method
Other important things about page directives to remember are:
Include directive can be used to import contents of another file statically into a jsp page, and may be used for adding a header, footer etc. Here the page is included at compile time and not request time, and container is not required to include any changes made to the included file after the inclusion. Most containers may have support for recompiling pages which has an include file that has changed, but this is not guaranteed by the specification.
Example and syntax
<%@ include file="stubs/header.html" %>
Here file is the only mandatory parameter.
The file type can be anything whose contents make sense to the JSP translation process once included. It can be JSP or HTML or XML documents, or document fragments.
Other important things about include directive to remember are:
The taglib directive makes custom actions available in the JSP page by referencing a tag library.
<%@ taglib prefix="mytags" uri="http://www.abc.com/taglibs/mytags" %>
Attributes of taglib directive are:
uri
absolute or unique URI to uniquely identify tag library
tagdir
directory within the war of the library of tag files which this page references.
Prefix
Prefix to be associated with this tag library in this JSP.
Only one of uri or tagdir attributes may be specified or translation error will occur.
We will see taglib directive and its attributes in detail later when we see custom tag development.