JSP directives

In this JSP tutorial, you will learn about Directive tag with example, page directive, language, extends, import, session and buffer.There are three types of directives are as follows:

  • page directive
  • include directive
  • taglib directive

Syntax of JSP Directive

<%@ directive attribute="value" %>

Attributes of JSP page directive

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

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

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.*}, ..." %> 

Example of import attribute

			 
<html>
<body>

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

</body>
</html>

2-contentType

contentType attribute is used to set the mime type and character set of the JSP. The user can make use of any MIME(Multipurpose Internet Mail Extension) type or character set valid for the JSP container.

Syntax of contentType attribute available for page directive is:

	
		<%@ page contentType="mimeType [;charset=characterSet]" %>

In the above statement, page and contentType are keywords. The default, MIMEtype is text/html, and the default character set is ISO-8859-1.

For example, the user specifies the attribute contentType is:

	
			<%@ page	contentType="text/html;charset=ISO-8859-1" %>

Example of contentType attribute

<html>
<body>

<%@ page contentType=application/msword %>
Today is: <%= new java.util.Date() %>

</body>
</html>

3-extends

TThis 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

In the above statement page and extends are keywords.

				 <%@ page extends="packageclass"% >

4-info

Programmers make use of info attribute to place the information or documentation for a page. Details such as: author, version, copyright and date are placed in this attribute. This is actually text string that is written as input text in the compiled JSP page .

Syntax of info attribute available for page directive is:

				<%@ page info = "text" %>

The web container will create a method getServletInfo() in the resulting servlet.

In the above statement, page and info are keywords. The details of documentation or information are placed inside " ". This is actually text string written as input text in the compiled JSP page.

5-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 of buffer attribute

  <html>
<body>

<%@ page buffer="16kb" %>
Today is: <%= new java.util.Date() %>

</body>
</html>

6-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" %>

7)isELIgnored

isErrorPage attribute is used to specify whether or not a JSP page displays an error page by setting the value as true or false. By default, the value is set to false, meaning that the user cannot make use of the exception object in the JSP page. If the value is set to true, then it means that the user can make use of the exception object in the JSP page.

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

8-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:

The web container in such a case, will generate the servlet as:

9-errorPage

If the programmer wants to place errors in a different page then the URL to the error page can be mentioned in this attribute as errorPage.

Syntax of errorPage attribute available for page directive is as below:

<%@ page errorPage = "relativeURL" %>
 //index.jsp
<html>
<body>

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

 <%= 100/0 %>

</body>
</html>

10-isErrorPage

isErrorPage attribute is used to specify whether or not a JSP page displays an error page by setting the value as true or false. By default, the value is set to false, meaning that the user cannot make use of the exception object in the JSP page. If the value is set to true, then it means that the user can make use of the exception object in the JSP page.

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

Example of isErrorPage attribute

			 //myerrorpage.jsp
<html>
<body>

<%@ page isErrorPage="true" %>

 Sorry an exception occured!
The exception is: <%= exception %> </body> </html>