JSP Directives

JSP Directives

In this JSP tutorial, you will learn about Directive tag with example, page directive, language, extends, import, session and buffer.

Directive tag:

The directive tag gives special information about the page to JSP Engine. This changes the way JSP Engine processes the page. Using directive tag, user can import packages, define error handling pages or session information of JSP page.

General notation of directive tag is as follows:

There are three types of directive tag.

  • page
  • Include
  • Tag Lib

Syntax and usage of directive tag

page directive:

General syntax for the page directive is

<%@ page optional attribute … %>

There are many optional attributes available for page directive. Each of these attributes are used to give special processing information to the JSP Engine changing the way the JSP Engine processes the page. Some of the optional attributes available for page directive are:

  • language
  • extends
  • import
  • session
  • buffer
  • autoFlush
  • isThreadSafe
  • info
  • errorPage
  • IsErrorPage
  • contentType

Syntax and usage of some of the optional attributes available for page directive discussed below.

language:

This attribute is used to denote the language used by a file. Language denotes the scripting language used in scriptlets, declarations, and expressions in the JSP page and any included files.

Syntax of language attribute available for page directive is

<%@ page language = "lang" %>

In the above statement page and language are keywords and one places whatever language the file uses inside " ".

For example if one wants to mention the language as java which is generally mentioned for all it is done as shown below:

<%@ page language = "java" %>

extends:

This is used to signify the fully qualified name of the Super class of the Java class used by the JSP engine for the translated Servlet.

Syntax of extends attribute available for page directive is

<%@ page extends = "package.class"%>

In the above statement page and extends are keywords.

import:

The import attribute is used to import all the classes in a java package into the current JSP page. With this facility, the JSP page can use other java classes.

Syntax of import attribute available for page directive is 

<%@ page import = "java.util.*" %>

In the above statement page and import are keywords.

If there are many Java packages that the JSP page wants to import, the programmer can use import more than once in a JSP page or separate the Java packages with commas, as shown below:

<%@ page import="{package.class | package.*}, …" %> 

session:

The session attribute, when set to true, sets the page to make use of sessions.

NOTE: by default, the session attribute value is set to true therefore, all JSP pages have session data available. If the user sets the session attribute to false, it should be performed in this section. When the session attribution is set to false, the user cannot use the session object, or a <jsp:useBean> element with scope=session in the JSP page which, if used, would give error.

Syntax of session attribute available for page directive is 

<%@ page session="true|false" %> 

In the above statement page and session are keywords. And either true or false value can be setted and by default the value is true.

buffer:

If a programmer likes to control the use of buffered output for a JSP page then the buffer attribute can be made use of for achieving this.

Syntax of buffer attribute available for page directive is 

<%@ page buffer = "none|8kb|sizekb" %>  

In the above statement page and buffer are keywords. The size of buffer size is mentioned in kilobytes.  This is used by the out object to handle output sent from the compiled JSP page to the client web browser. The default value is 8kb. If a user specifies a buffer size then the output is buffered with at least the size mentioned by the user.

For example one can specify as:

<%@ page buffer = "none" %>

[catlist id=154].

Related posts