< Prev
Next >



JSP Directives





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 - using JSP directives, you can



Three types of directives in JSP -






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 -




The import attribute


The import attribute is used to import a Java package that maybe required by the java code embedded in JSP web page. For example, we may import java.util package in order to print the current day and time in JSP web page. Let's see how it is done.



import3.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 using Date class and have also displayed the current date and time in its formatted form using DateFormat class, as shown below.






Note :




<%@ 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 have the same result as import.jsp.



Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< JSP Scripting Elements
JSP contentType Attribute >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2