- Java EE Tutorials
- JSP Tutorials
- Recommended Books for Java Server Pages (JSP)
Directives specifies translation time instruction to JSP engine. There are three types of directives:
- page directive
- include directive
- taglib directive
JSP Page Directive
This directive can be used to specify any of a number of page-dependent attributes such as scripting language to use, class to extend, page to be imported and more. Syntax of JSP page directive is as follows:
<%@ page attribute="value"%>
Where attribute can be any of the following:
- import– It is used to import classes, interface of a package similar to import statement in Java.
- contentType – It defines MIME(Multipurpose Internet Mail Extension) type of response. You can specify any of these values:text/xml, text/html, application/msword, or text/html:charset=ISO-8859-1. Default value is ” “text/html;charset=ISO-8859-1″.”.
- extends – It defines parent class of generated servlet class.
- info – Defines information about jsp page which can later be retrieved by using getServletInfo() method of Servlet interface.
- buffer – Defines size of the buffer used to handle output stream.Default buffer size is 8kb or greater.
- language – It defines programming langauge used in jsp page.default value is “java”.
- isELIgnored – It specifies whether EL(Expression Langauge) elements are ignored on the page.The value is either true or false.The value is false by default.
- isThreadSafe – Defines the threading model for the generated servlet. The default value is true.If make it false,it will wait until the JSP finishes responding to a request before passing another request to it.
- errorPage – It is used to define the error page. If an error occurs on current page then it will be redirected to the error page defined by this attribute.
- isErrorPage – It declares that current page is the error page.
- autoFlush – It defines whether output should be flushed automatically when buffer is filled.The default is true.
- pageEncoding – It defines character encoding of output stream.The default is ISO-8859-1.
- session – It defines whether JSP page is participating in HTTP session.
Example – import attribute
Listing 1 – example.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> <%@ page import="java.util.Date" %> Today is: <%= new java.util.Date() %> </body> </html>
Output
Execute the above example.jsp in Eclipse by selecting Run As > Run On Server, output as below would be seen:
Example – contentType attribute
Listing 2 – example.jsp
<html> <body> <%@ page contentType="text/xml" %> Today is: <%= new java.util.Date() %> </body> </html>
Output
Execute the above example.jsp in Eclipse by selecting Run As > Run On Server, output as below would be seen:
Example – info
Listing 3 – example.jsp
<html> <body> <%@ page info="This page is by Javabeat..." %> </body> </html>
The web container creates a method as below:
public String getServletInfo() { return "This page is by Javabeat..."; }
Example – buffer
Listing 4 – example.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> <%@ page buffer="10kb" %> Today is: <%= new java.util.Date() %> </body> </html>
Example – errorPage and isErrorPage attribute
Listing 5 – example.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> <%@ page errorPage="errorpage.jsp"%> <%=10/0 %> </body> </html>
Listing 6 -errorpage.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> <%@ page isErrorPage="true" %> Exception occured..The exception is: <%= exception %> </body> </html>
Output
Execute the above example.jsp in Eclipse by selecting Run As > Run On Server, output as below would be seen:
JSP Include Directive
It is used to include any contents of files such as jsp file, html file or text file. Syntax of JSP include directive:
<%@ include file="file name" %>
Let us write an example.jsp as below. This will include a file called welcome.jsp:
Listing 7 -errorpage.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> <%@ include file="welcome.jsp" %> Today is: <%= new java.util.Date() %> </body> </html>
Listing 8 -welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Insert title here</title> </head> <body> <h2>Welcome to Javabeat</h2> </body> </html>
Output
Execute the above example.jsp in Eclipse by selecting Run As > Run On Server, output as below would be seen:
JSP Taglib Directive
It is used to define tag library that defines custom tags. This directive includes URI and custom tag prefix. Syntax of JSP include directive:
<%@ taglib uri="uri" prefix="prefix_to_the_Tag" >
where
- uri is the resolved to the location the container understands
- prefix attribute informs a container what bits of markup are custom actions.
Let us see an example below which demonstrates use of taglib directive:
Listing 9 -example.jsp
<%@ page contentType="text/html" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>JSP is Easy</title> </head> <body bgcolor="white"> <h1>JSP is as easy as ...</h1> <%-- Calculate the sum of 1 + 2 + 3 dynamically --%> 1 + 2 + 3 = <c:out value="${1 + 2 + 3}" /> </body> </html>
Previous Tutorial : JSP Actions || Next Tutorial : JSP Implicit Objects