As it is evident from its name, JSP directives are a set of directions or instructions that allow you set how a JSP page is going to be processed and displayed.
For example, by using JSP directives you can-
Include the content of a webpage in another webpage.
You can indicate if you want output of your page to be in plain text, xml or html format.
You can create your own custom tags and used them in your JSP page.
Three types of directives in JSP are:
page directive.
include directive.
taglib directive.
The page directive
The page directive allows you to configure the entire JSP page by importing the required classes, setting the content type, setting the error page, etc.
Page directives has a few important attributes which you may use to configure your JSP web page such as-
import
contentType
session
errorPage
isErrorPage
info
Note: In this tutotial, we are going to discuss the import attribute of the page directive.
The import attribute
The import attribute is used to import a Java package that could be required by the java code embedded in a JSP web page. For example, we may import java.util
package in order to print the current day and time in a JSP web page. Let's see how it is done.
Importing a java package and its all classes.
import.jsp
<!–– Example of import attribute of page directive of JSP -->
<html>
<head>
<title>JSP import attribute</title>
</head>
<body>
<%@ page import="java.util.*" %>
The current date and time <%= new Date() %>
</body>
</html>
Executing this JSP wep page opens a web page with the current day, date and time.
Importing an individual class.
import2.jsp
<!–– Example of import attribute of page directive of JSP -->
<html>
<head>
<title>JSP import attribute</title>
</head>
<body>
<%@ page import="java.util.Date" %>
The current date and time <%= new Date() %>
</body>
</html>
We have used the import attribute of JSP's page directive to specifically import Java's Date class, which is present in the util package, and executing this JSP page also displays
a web page with current day, date and time, as shown below.
Advertisement
Importing multiple classes from different packages
import3.jsp
<!–– Example of import attribute of page directive of JSP -->
<html>
<head>
<title>JSP import attribute</title>
</head>
<body>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.DateFormat" %>
<% Date d = new Date(); %>
The current date and time : <%= d %>
<% DateFormat df = DateFormat.getInstance(); %>
<br/>
The formatted date and time : <%= df.format(d) %>
</body>
</html>
We have imported two individual classes, i.e. Date and DateFormat in a JSP web page.
We have displayed the current day, date and time in its simple form by using the Date class, and have also displayed
the current date and time in its formatted form by using the DateFormat class, as shown below.
Note :
Instead of importing each java class in a separate import attributes of single page directive,
we could also import Date and DateFormat classes by using a single import attribute within a page directive, such as-
You could place page directive inside/outside the <body> tag of html or even at the beginning of the jsp file and result will still be the same. Why? Because, the only thing that matters is to import
the java class or package with page directive before using it.
import4.jsp
<%@ page import="java.util.*" %>
<html>
<head>
<title>JSP import attribute</title>
</head>
<body>
The current date and time <%= new Date() %>
</body>
</html>
Executing the above mentioned import4.jsp file gives the same result as import.jsp.