Thursday, August 22, 2013

Basic lesson 6:- Directives in JSP

We have been fully qualifying the java.util.Date in the examples in the previous sections.  Perhaps you wondered why we don't just import java.util.*;
It is possible to use "import" statements in JSPs, but the syntax is a little different from normal Java.  Try the following example:
<%@ page import="java.util.*" %>
<HTML>
<BODY>
<%
    System.out.println( "Evaluating date now" );
    Date date = new Date();
%>
Hello!  The time is now <%= date %>
</BODY>
</HTML>
The first line in the above example is called a "directive".  A JSP "directive" starts with <%@ characters.
This one is a "page directive".  The page directive can contain the list of all imported packages.  To import more than one item, separate the package names by commas, e.g.
<%@ page import="java.util.*,java.text.*" %>
There are a number of JSP directives, besides the page directive.  Besides the page directives, the other most useful directives are include and taglib.  We will be covering taglib separately.
The include directive is used to physically include the contents of another file.  The included file can be HTML or JSP or anything else -- the result is as if the original JSP file actually contained the included text.  To see this directive in action, create a new JSP
<HTML>
<BODY>
Going to include hello.jsp...<BR>
<%@ include file="hello.jsp" %>
</BODY>
</HTML>
View this JSP in your browser, and you will see your original hello.jsp get included in the new JSP.
Exercise:  Modify all your earlier exercises to import the java.util packages.

No comments:

Post a Comment