JSP directives

  • The JSP directives are messages that tell the web container how to translate a JSP page into the corresponding servlet.
  • There are three types of directives:
    • page directive
    • include directive
    • taglib directive
Syntax:
<%@ directive attribute="value" %>

JSP page directive

  • The page directive defines attributes that apply to an entire JSP page.
  • Syntax

<%@ page attribute="value" %>

  • Attributes of JSP page directive
import
  • The import attribute is used to import class,interface or all the members of a package.It is similar to import keyword in java class or interface.
Example:
in body tag
<%@ page import="java.util.Date" %>
Today is: <%= new Date() %>

contentType
  • The contentType attribute defines the MIME(Multipurpose Internet Mail Extension) type of the HTTP response.The default value is "text/html;charset=ISO-8859-1".
Example:
in body tag
<%@ page contentType=application/msword %>

extends
  • The extends attribute defines the parent class that will be inherited by the generated servlet.It is rarely used.
info
  • This attribute simply sets the information of the JSP page which is retrieved later by using getServletInfo() method of Servlet interface.
Example: 
in body tag
<%@ page info="composed by CDPatel" %>

buffer
  • The buffer attribute sets the buffer size in kilobytes to handle output generated by the JSP page.The default size of the buffer is 8Kb.
Example:
in body tag

<%@ page buffer="16kb" %>

language
  • The language attribute specifies the scripting language used in the JSP page. The default value is "java".
isELIgnored
  • We can ignore the Expression Language (EL) in jsp by the isELIgnored attribute. By default its value is false i.e. Expression Language is enabled by default. We see Expression Language later.
Example:
<%@ page isELIgnored="true" %>

isThreadSafe

  • Servlet and JSP both are multithreaded.
  • If you want to control this behaviour of JSP page, you can use isThreadSafe attribute of page directive.
  • The value of isThreadSafe value is true.
  • If you make it false, the web container will serialize the multiple requests, i.e. it will wait until the JSP finishes responding to a request before passing another request to it.
  • If you make the value of isThreadSafe attribute like:

Example:
in body tag
<%@ page isThreadSafe="false" %>

autoFlush

  • autoFlush attribute defines whether the buffered output is flushed automatically. The default value is "true".

session

  • session attribute defines whether the JSP page is participating in an HTTP session. The value is either true or false.

errorPage

  • The errorPage attribute is used to define the error page, if exception occurs in the current page, it will be redirected to the error page.

Example:
in body tag
<%@ page errorPage="myerrorpage.jsp" %>

isErrorPage

  • The isErrorPage attribute is used to declare that the current page is the error page.

Example:
in body tag
<%@ page isErrorPage="true" %>

JSP Include Directive
  • The include directive tells the Web Container to copy everything in the included file and paste it into current JSP file. 
  • Syntax of include directive is:
<%@ include file="filename.jsp" %>

  • Example of include directive
welcome.jsp
in body tag
        <%@ include file="header.jsp" %>
        Welcome, User

header.jsp
in body tag
AMPICS

JSP taglib Directive
  • The JavaServer Pages API allow 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 behavior.
  • The taglib directive declares that your JSP page uses a set of custom tags, identifies the location of the library, and provides means for identifying the custom tags in your JSP page.
Syntax 
<%@ taglib uri = "uri" prefix = "prefixOfTag" >
  • The prefix is used to distinguish the custom tag from other library custom tag. 
  • Prefix is prepended to the custom tag name. 
  • Every custom tag must have a prefix.
  • The URI is the unique name for Tag Library.
  • To use the JSTL in your application you must have the jstl.jar in your web apps /WEB-INF/lib directory. Download the jar file from page.
  • There are many readymade JST Libraries available which you use to make your life easier. 
  • Following is a broad division on different groups of JST libraries :
    • Core Tags - URI → http://java.sun.com/jsp/jstl/core
    • Formatting Tags - URI → http://java.sun.com/jsp/jstl/fmt
    • SQL Tags - URI → http://java.sun.com/jsp/jstl/sql
    • XML Tags - URI → http://java.sun.com/jsp/jstl/xml
    • JSTL Functions - URI → http://java.sun.com/jsp/jstl/functions
Example:
<%@ taglib uri = "http://www.procdpatel.blogspot.com/custlib" prefix = "mytag" %>

in body

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form