JSP Directive To Import ClassesWhen you are writing your Java Server Pages, there will be a time when you need to use classes that are available in your class path. For example, you wish to print the current date. How will you do this? Lets take a look at an example using purely JSP scriptlets.
<html> <body> <% /* * This is a multi line comment. * To print the date, we need an * instance of java.util.Date */ java.util.Date now = new java.util.Date(); out.println("Present Date = " + now); %> </body> </html>
Let me read your mind. Most likely, you are now asking the question "Why don't we use the import statement just like we did in Java programming?". You cannot simply insert a line of code that says 'import java.util.Date;' at the very first line between your <% and %>. This is where JSP Page Directive will come into play. Let us look at the revised version of the above code.
<%@ page import="java.util.Date" %> <html> <body> <% /* * This is a multi line comment. * To print the date, we need an * instance of java.util.Date */ Date now = new Date(); out.println("Present Date = " + now); %> </body> </html>
Using the JSP Page Directive as shown above, we instructed the JSP container to import the Date class under the java.util package.