Unit V - Java Server Pages
Unit V - Java Server Pages
• Directives can have a number of attributes which you can list down as
key-value pairs and separated by commas.
• The blanks between the @ symbol and the directive name, and between
the last attribute and the closing %>, are optional.
There are three types of directives:
• Page directive
• Include directive
• Taglib directive
1. Page Directives
• JSP page directive is used to define the properties applying the JSP
page such as the size of the allocated buffer, imported packages and
classes/interfaces, defining what type of page it is etc.
• The syntax of JSP page directive is as follows:
import
• This tells the container what packages/classes are needed to be
imported into the program.
• Syntax:
<%@page contentType="value"%>
• Example
<%@page contentType = "text/html"
%>
<%= "This is a test." %>
info
• This attribute simply sets the information of the JSP page which is
retrieved later by using getServletInfo() method of Servlet interface.
• Syntax:
• Example
<%@ page info=“Develop by Rohit” %>
<%= getServletInfo() %>
buffer
• Defines the size of the buffer that is allocated for handling the JSP
page.
• The size is defined in Kilo Bytes.
• Syntax:
• Example
<%@ page buffer="16kb" %>
language
• Defines the scripting language used in the page. By default, this
attribute contains the value ‘java’.
• Example
<%@ page isELIgnored="true" %>
//Now EL will be ignored
errorPage
• Defines which page to redirect to, in case the current page
encournters an exception.
• Syntax:
• Example
<%@ page errorPage = "error.jsp" %>
<%
int z = 1/0;
out.print("division of numbers is: " + z);
isErrorPage
• The isErrorPage attribute is used to declare that the current page is
the error page.
• Syntax:
• Example
<%@ page isErrorPage = "true" %>
<h1>Exception caught</h1>
The exception is: <% = exception %>
isThreadSafe
• If true, the servlet is assumed to be thread-safe.
• If false, implements SingleThreadModel is added to the servlet class
declaration so that the thread runs in the single thread model.
• The default is true.
• Syntax:
• Example
• Example
a.html
index.jsp
</body>
</html>
<html>
<head>
<title>JSP expression tag example2</title>
</head>
<body>
<%
int a=10;
int b=20;
int c=30;
%>
</body>
</html>
Using Declarations
• A declaration is code that is included in the servlet but outside of any
method.
• Declarations are used to create class variables or methods that can be
used in expressions, scriptlets, or other declarations.
• Syntax:
<%! statements... %>
• You can place declarations anywhere you want in a JSP.
<html>
<body>
<%!
int square(int n)
{
return n*n;
}
%>
<%= square(7) %>
</body>
</html>
<html>
<%@ page import=”java.text.*” %>
<%@ page import=”java.util.*” %>
<head>
<title>Counter JSP</title>
</head>
<body>
<h1>
This JSP has been displayed <%= count++ %> time.</h1>
</body>
</html>
<%!
private static int count = 1;
%>
<html>
<%@ page import="java.text.*" %>
<%@ page import="java.util.*" %>
<head>
<title>Date JSP</title>
</head>
<body>
<h1>
Today is <%= getDate() %></h1>
<h1>Have a nice day!</h1>
</body>
</html>
<%!
private String getDate()
{
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
Date today = new Date();
return df.format(today);
}
%>
JSP Session
• Session is an implicit object in JSP.
• It is used to gain access to all the user’s data till the user session is active.
• Instance of javax.servlet.http.HttpSession which provides a way to
identify a user across more than one page request.
• It behaves like same as session in servlet.