© Copyright Marcus Green 2007

Objective 2) JSP Directives

6.2)Write JSP code that uses the directives: (a) 'page' (with attributes 'import', 'session', 'contentType', and 'isELIgnored'), (b) 'include', and (c) 'taglib'.

What are directives (JSP1.10)

To quote from the JSP specification

Directives are messages to the JSP container. Directives have this syntax:

<%@ directive { attr=”value” }* %>”

You will almost certainly get questions on directives in the exam so make sure you are familiar with what they do and their syntax.

The page directive

The page directive affects the processing of the whole page. The page directive can be used more than once but you can only use each attribute once. It is syntactically correct for the page directive anywhere in the page but convention (and ease of reading) normally places it at the top of the page.

Import

The import directive acts similarly to the import statement in an ordinary Java program, i.e. It indicates where Java should look for library files. The following example code

<%@ page import=”java.util.*,java.io.*” %>
The xml equivalent tag is
<jsp:directive.page import="java.util.*"/>

As is illustrated in the first example you where more than one package is being imported, the list of imports can be a comma separated list. In addition to the default classes that are imported with any Java program (Java.lang.*) you also get javax.servlet.*, javax.servlet.jsp.*, and javax.servlet.http.* by default.

isELIgnored

The expression language was a new introduction with JSP2, and thus there is a possibility that pages authored for earlier versions of Java may have clashes with EL tags. For this reason JSP supplies the isELIgnored tag which turns Expression language parsing on and off on a page by page basis. The following directive turns off the parsing of EL tags. As you might guess, changing from true to false will mean that EL tags will be recognised.

<%@ page isELIgnored="true" %>

session

The session directive if set to false makes the session unavailable to the page. Setting up a session is done automatically for you “behind the scenes”, and you are fairly unlikely to want to turn it off. Possible reasons for turn off the session are to gain a slight performance edge on heavily used pages that do not need to track between page requests.

<%@ page session="false" %>

contentType

The contentType directive tells the page what MIME type the page is. MIME originally stood for Multipurpose Internet Mail Extensions and was used to indicate the type of data being sent as an attachment via email. Today its use has been extended to other purposes such as indicating a data type such as pdf, ocx or msword.

include

The include page directive was covered in objective 6.1 and acts to bring in the content of another page at translation time, i.e. The content is bought in only on the first time the page is translated into a servlet.

taglib

The taglib directive tells the page you will be using some custom tags. That is tags that act like standard JSP but have been created and made available directly for this page. Because of the value of taglibs in uncluttering JSP pages from containing excessive scriptlets there has been a move to creating a standard, known wittily as the JSP Standard Tag Library (JSTL).

<%@ taglib uri="URIForLibrary" prefix="tagPrefix" %>

And the following code shows how it can actually be used

<%@ taglib uri="/examulator_tag" prefix="question" %>

<html>
  <head>
   <title>Examulator Tags</title>
  </head>
  <body>
   <h1>Tag Lib</h1>
    <question definition="Tags" parameter="6.1"/>
   </body>
</html>

Other Sources

The page directive according to Sun
http://java.sun.com/products/jsp/tags/11/syntaxref11.fm7.html

The page directive according to ** Baldwin
http://www.developer.com/java/other/article.php/626391

According to Mikalai Zaikin
http://java.boot.by/wcd-guide/ch06s02.html