|
(1)
(2) (3)
(4)
A Simple JavaServer Page
It's time we saw a JSP file:
|
<html>
<head>
<title>
Demo of
a JSP page
</title>
</head>
<body>
<!--
Set global information for the page -->
<%@
page language="java" %>
<!--
Declare the character variable -->
<%!
char c = 0; %>
<!--
Scriptlet - Java code -->
<%
for (int
i = 0; i < 26; i++)
{
for (int
j = 0; j < 26; j++)
{
// Output
capital letters of the alphabet, and change
starting letter
c = (char)(0x41
+ (26 - i + j)%26);
%>
<!--
Output the value of c.toString() to the HTML
page -->
<%=
c %>
<%
}
%>
<br>
<%
}
%>
</body>
</html>
|
|
This
page just outputs the alphabet 26 times and changes the
starting letter. The HTML is self-explanatory and written
the way it should be, rather than cluttering up methods
of a servlet.
Elements
of a JavaServer Page
The
Java code in the page includes:
Ø
Directives - these provide global
information to the page, for example, import statements,
the page for error handling or whether the page is part
of a session. In the above example we set the script language
to Java.
Ø
Declaratives - these are for page-wide
variable and method declarations.
Ø
Scriptlets - the Java code embedded
in the page.
Ø
Expressions - formats the expression
as a string for inclusion in the output of the page.
We
will meet the last JSP element type, actions, soon. These
elements follow an XML-like syntax, and perform a function
behind the scenes. They provide the means to totally separate
presentation from logic. A good example is <jsp:useBean
.../> which finds or creates an instance
of a bean with the given scope and name. With the tag extension
mechanismto be introduced in JSP 1.1, you'll be able
to define similar action tags and put their functionality
in a tag library.
Now
let's examine the basic elements of a JSP in a more complete
fashion, before we code some more.
Something
I've found very useful to have around while coding is the
syntax crib sheet available at http://java.sun.com/products/jsp/syntax.html
in PDF format. This has a concise summary of what we'll
see here.
©1999
Wrox Press Limited, US and UK.
|