logo

JSP Directives


Show

How to translate a JSP page into the corresponding servlet, is told by the JSP directives messages that let the web container know about it.

Three types of directives are given below:

  • page directive
  • include directive
  • taglib directive

Syntax of JSP Directive

<%@ directive attribute="value" %>  

JSP Page Directive

The page directive explains features that are requested to a whole JSP page.

Syntax of JSP Page Directive

<%@ page attribute="value" %>

Attributes of JSP Page Directive

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

1. Import

The import attribute is made in use to import class, interface, or all the persons of a package. It is the same process to import keywords in the java class or interface.

Example of Import Attribute

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

</body>  
</html>  

2. Content Type

The contentType attribute explains the Multipurpose Internet Mail Extension (MIME) kind of the HTTP replies. Its default value is "text/html;charset=ISO-8859-1".

Example of the Content-Type Attribute

<html>  
<body>  

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

</body>  

</html> 

3.Extends

The extends attribute explains the parent class that will become into by the produced servlet. But it is hardly ever used.

4.Info

This attribute easily sets the information of the JSP page which is recovered later by making use of the getServletInfo() method of the Servlet interface.

Example of Info Attribute

<html>  
<body>  

<%@ page info="composed by Sonoo Jaiswal" %>  
Today is: <%= new java.util.Date() %>  

</body>  
</html>

The web container will produce a way to getServletInfo() in the resulting servlet. For instance

public String getServletInfo() {  
  return "composed by Sonoo Jaiswal";   
}  

5. Buffer

The buffer attribute puts down the buffer size in kilobytes to manage the output produced by the JSP page. 8Kb is the default size of the buffer.

Example of Buffer Attribute

<html>  
<body>   

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

</body>  
</html>

6. Language

The language features describe the scripting language that is used on the JSP page. "Java" is the default value.

7. isELIgnored

We can disregard the Expression Language (EL) in JSP by the isELIgnored feature. By default its value is false, that is Expression Language is authorized by default. We will check expression languages after some time.

<%@ page isELIgnored="true" %>//Now EL will be ignored

8. isThreadSafe

Both Servlet and JSP are multithreaded. If you need to control this behavior of the JSP page, you can make use of isThreadSafe feature of the page directive. The amount of isThreadSafe value is true. If you do it false, the web container will list down many types of requests, that it will wait up until the JSP to complete responding to a request before passing some other request to it. If you create the value of isThreadSafe feature for example this:

<%@ page isThreadSafe="false" %>

Then, The web container in this type of case will create the servlet as given below:

public class SimplePage_jsp extends HttpJspBase   
  implements SingleThreadModel{  
.......  
}

9.ErrorPage

The errorPage feature is made in use to explain the error page, if an exception takes place in the current page, then it will be redirected to the error page.

Example of ErrorPage Attribute

//index.jsp

<html>  
<body>  
  
<%@ page errorPage="myerrorpage.jsp" %>  
  
 <%= 100/0 %>  
  
</body>  
</html>  

10.isErrorPage

The isErrorPage feature is made in use to announce that the current page is showing the error page

Note: Exception object can only be made in use in error page

Example of isErrorPage Attribute

//myerrorpage.jsp

<html>  
<body>   

<%@ page isErrorPage="true" %>  
  
 Sorry an exception occured!<br/>  
The exception is: <%= exception %>    

</body>  
</html>