JSP directives

JSP directives

Directive Tag gives special instruction to Web Container at the time of page translation. how to translate a JSP page into the corresponding servlet.

Directive tags are of three types:

  • page directive
  • include directive
  • taglib directive

 

Directive Description
<%@ page ... %> defines page dependent properties such as language, session, errorPage etc.
<%@ include ... %> defines file to be included.
<%@ taglib ... %> declares tag library used in the page

 

The Page directive defines a number of page dependent properties which communicates with the Web Container at the time of translation.

Syntax of JSP page directive

 <%@ page attribute=“value” %>  

Attributes of JSP page directive

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

import attribute

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. For example

<html>  
<body>  
<%@ page import="java.util.Date" %>  
Today is: <%= new Date() %>    
</body>  
</html>  

language attribute

The language attribute specifies the scripting language used in the JSP page. The default value is “java”.


extends attribute

extends attribute defines the class name of the superclass of the servlet class that is generated from the JSP page.


session attribute

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

Default value for this attribute: true

Syntax of session attribute:

<%@ page session="value"%>

Examples of session:

<%@ page session="true"%>

The above code would allow a page to have session implicit objects.

<%@ page session="false"%>

If this code is specified in a JSP page, it means session objects will not be available for that page. Hence session cannot be maintained for that page.


isThreadSafe attribute

isThreadSafe attribute declares whether the JSP is thread-safe.The value is either true or false


isErrorPage attribute

This attribute is used to specify whether the current JSP page can be used as an error page for another JSP page. If value of isErrorPage is true it means that the page can be used for exception handling for another page.

Syntax of isErrorPage attribute:

<%@ page isErrorPage="value"%>

Here value is either true OR false.

Example of isErrorPage:

<%@ page isErrorPage="true"%>

Note: The exception object can only be used in the error page.


errorPage attribute

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.

Syntax of errorPage attribute:

<%@ page errorPage="value"%>

Example of errorPage:

<%@ page errorPage="error.jsp"%>

contentType attribute

contentType attribute defines the MIME type for the JSP response.This attribute is used to set the content type of a JSP page.

Default value: text/html

Syntax of contentType attribute:

<%@ page contentType="value"%>

Example of contentType:

<%@ page contentType="text/html"%>

for text/xml based pages:

<%@ page contentType="text/xml"%>

autoFlush attribute

autoFlush attribute defines whether the buffered output is flushed automatically. The default value is “true”.


buffer attribute

buffer attribute defines how buffering is handled by the implicit out object.

Leave a comment