JSP Elements Part 1 - Directives

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"


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.


Page directive

The page directive provides information about the page to the translator.


Important attributes for the page directive

  1. import

    1. to create import statements in the generated servlet source produced by the JSP container’s translation phase.

    2. We can specify comma-separated list of classnames or packages as value of the import statement.

    3. 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.

    4. Ex: <%@ page import="java.util.StringTokenizer" %>

    5. 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:

      1. java.lang

      2. javax.servlet

      3. javax.servlet.http

      4. javax.ervlet.jsp

  2. session

    1. used to determine whether an HttpSession object is available within your JSP page source through an implicit object session.

    2. The default value is true.

    3. 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.

    4. Ex: <%@ page session="true" %>

  3. contentType

    1. used to set the content type. Same as calling response.setContentType() from a servlet.

    2. Ex: <%@ page contentType="image/gif" %>

  4. isELIgnored

    1. you can switch off EL evaluation and have the text be treated as template text.

    2. The JSP 2.0 sets a default of isELIgnored="true" for backward compatibility.

      1. However if the deployment descriptor web.xml is at the version level of 2.4, the default of isELIgnored is set to “false”.

    3. Ex: <%@ page isELIgnored="true" %>

  5. language

    1. Denotes the scripting language. “Java” is the only value supported.

    2. Ex: <%@ page language=“Java” %>

  6. buffer, autoFlush

    1. 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.

    2. Ex: <%@ page buffer=“none” autoFlush=“true” %>

  7. isThreadSafe

    1. 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.

    2. 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.

    3. Ex: <%@ page isThreadSafe=“true” %>

  8. errorPage, isErrorPage

    1. 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.

    2. The page you forward to must have “isErrorPage” set to true.

    3. Ex: <%@ page errorPage=“errorPage.jsp” %>

    4. Ex: <%@ page isErrorPage=“true” %>

  9. extends

    1. override the base servlet that your container provides when generating servlets

  10. info

    1. publish information about your JSP page, accessible through the getServletInfo() method


Other important things about page directives to remember are:

  • You can include a page directive anywhere in your JSP page source: beginning, middle, or end.
  • You can we have more than one page directive in a page


Include Directive

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:

  • You cannot include a file from another context using the include directive.
  • There is also a standard action named <jsp:include>. The key difference between <jsp:include> and the include directive is that <jsp:include> executes afresh with every new request to the including JavaServer Page. The include directive happens at translation time.
  • You may also RequestDispatcher to include web components dynamically at runtime. But it is more resource intensive as it is invoked during every request.


Taglib Directive

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:

  1. uri

    • absolute or unique URI to uniquely identify tag library

  2. tagdir

    • directory within the war of the library of tag files which this page references.

  3. 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.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)