JSP Page Directive ELements

Introduction

In this article we discuss the JSP page directive element.

JSP directives element

JSP directive elements translate JSP pages into corresponding servlets. There are three types of directives:

  • Page directive elements
  • taglib directive elements
  • include directive elements

Syntax

<%@ directive attribute="value" %>

JSP page directive element

They apply to an entire JSP page.

Syntax

<%@ page attribute="value" %>

Attributes of JSP page directive elements

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

1. info

This attribute simply gives the information of the JSP page, this information is received using the getServletInfo() method of the Servlet interface.

Example

date1.jsp

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<%@ page info="composed by Sandeep" %>

Date & Time of Today is: <%=new java.util.Date() %>

</body>

</html>

Output

fig-1.jpg

2. extends

This attribute shows/defines the parent class that will be inherited by the generated servlet. It is rarely used.

3. import

This attribute is used to import interfaces and classes or all the members of a package. It is similar to the import keyword in a Java class or interface.

Example

date1.jsp

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

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

Date & Time of Today is: <%=new Date() %>

</body>

</html>

Output

Fig-2.jpg

4. contentType

This attribute defines a HTTP response of a MIME type. The default value is "text/html;charset=ISO-8859-1".

Example

date1.jsp

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<%@ page contentType=application/msword %>

Date & Time of Today is: <%=new java.util.Date() %>

</body>

</html>

Output

fig-3.jpg

5. buffer

This attribute sets the buffer size in kilobytes to handle output generated by the JSP page. The default size of the buffer is 8kb.

Example

date1.jsp

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

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

Date & Time of Today is: <%=new java.util.Date() %>

</body>

</html>

Output

fig-4.jpg

6. language

The language attribute specifies the scripting language used in the JSP page. The default value is "Java".

7. isELIgnored

We can ignore the Expression Language (EL) in JSP by the isELIgnored attribute. By default its value is false, in other words the Expression language is embedded by default. We will see the Expression Language later.

<%@ page isELIgnored="true" %>

8. isThreadSafe

JSP and Servlet both are multithreaded. If we need to control this behaviour of a JSP page then we need to use the isThreadSafe attribute. The value is set always true. If we make it false then the web container will serialize the multiple requests, in other words it will wait until the JSP finishes responding to a request before passing another request to it. If we make the value of the isThreadSafe attribute like:

<%@ page isThreadSafe="false" %>

Then in that case the web container will generate the servlet as:

public class OnePage_jsp extends HttpJspBase

implements SingleThreadModel

{

...........

}

9. errorPage

This attribute defines the error page; if an exception occurs in the current page then it will be redirected to the error page.

Example

date2.jsp

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

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

<%=100/0 %>

</body>

</html>

Output

fig-5.jpg

10. isErrorPage

This attribute sets the current page as the error page.

Example

date2.jsp

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<%@ page isErrorPage="true" %>

Sorry an exception generated!<br>

The exception generated is: <%=exception %>

</body>

</html>

Output

fig-6.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all