JSP Directives

Directives are those which tell the JSP engine how to translate a JSP page into the corresponding servlet.

Directives provide directions and instructions to the container, telling it how to handle certain aspects of JSP processing.

JSP directive affects the overall structure of the servlet class. It usually has the following form:

<%@directiveattribute="value"%>

Page Directive:

This directive defines page-dependent attributes, such as scripting language, error page, and buffering requirements.

This directive is used to provide instructions to the container that pertain to the current JSP page. You may code page directives anywhere in your JSP page.

By convention, page directives are coded at the top of the JSP page.

Syntax of page directive:

<%@ page attribute="value" %>

 

There are many attributes for the page directive which are listed below.

  1. import
  2. contentType
  3. extends
  4. info
  5. buffer
  6. language
  7. isELIgnored
  8. isThreadSafe
  9. autoFlush
  10. session
  11. pageEncoding
  12. errorPage
  13. isErrorPage

We will see in detail about the details of the attributes shortly.

Include Directive:

Include directive is used to include the contents of any resource it may be jsp file, html file or text file.

Include directive includes the original content of the included resource at page translation time (the jsp page is translated only once so it will be better to include static resource).

This directive tells the container to merge the content of other external files with the current JSP during the translation phase.

 You may code include directives anywhere in your JSP page.

The filename in the include directive is actually a relative URL. If you just specify a filename with no associated path, the JSP compiler assumes that the file is in the same directory as your JSP.

Syntax of Include Directive:

<%@ include file ="footer.jsp" %>

In the above example footer.jsp should be present in the same directory as your JSP which you are creating now.

We will see in detail the Include directive with examples shortly.

Taglib Directive:

The taglib directive declares that your JSP page uses a set of custom tags, identifies the location of the library, and provides a means for identifying the custom tags in your JSP page.

The JSP API allows you to define custom JSP tags that look like HTML or XML tags and a tag library is a set of user-defined tags that implement custom behaviour.

Syntax of taglib directive:

<%@tagliburi="uri"prefix="prefixOfTag"%>

When you use a custom tag, it is typically of the form <prefix:tagname>. The prefix is the same as the prefix you specify in the taglib directive, and the tagname is the name of a tag implemented in the tag library.

We will see in detail the Include directive with examples shortly.

Posted on July 12, 2014 in Java Server Pages

Share the Story

Leave a reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top