Sun Java Solaris Communities My SDN Account
 
Tutorials & Code Camps

NO TITLE

 
[Top] [Prev] [Next] [Bottom]

A First JSP Application

FIGURE 1-1 shows what is perhaps the simplest JSP application one could write. It continues the illustrious computer science Hello, World tradition. CODE EXAMPLE 1-1 and CODE EXAMPLE 1-2 show how the example is put together.

FIGURE 1-1    Duke Says Hello

CODE EXAMPLE 1-1 The Duke Banner (dukebanner.html)
<table border="0" width="400" cellspacing="0" cellpadding="0">
<tr>
<td height="150" width="150"> &nbsp; </td>
<td width="250"> &nbsp; </td>
</tr> <tr>
<td width="150"> &nbsp; </td>
<td align="right" width="250">
<img src="duke.waving.gif">
</td>
</tr>

</table>
<br>

CODE EXAMPLE 1-2 The JSP Page (helloworld.jsp)
<%@ page info="a hello world example" %>

<html>
<head><title>Hello, World</title></head>

<body bgcolor="#ffffff" background="background.gif">
<%@ include file="dukebanner.html" %> <table>
<tr>
<td width=150> &nbsp; </td>
<td width=250 align=right> <h1>Hello, World!</h1> </td>
</tr>
</table>

</body>
</html>

The Page Directive

The page directive is a JSP tag that you will use in almost every JSP source file you write. In helloworld.jsp, it's the line that looks like this:

<%@ page info="a hello world example" %>

The page directive gives instructions to the JSP container that apply to the entire JSP source file. In this example, page specifies an informative comment that will become part of the compiled JSP file. In other cases, page might specify the scripting language used in the JSP source file, packages the source file would import, or the error page called if an error or exception occurs.

You can use the page directive anywhere in the JSP file, but it's good coding style to place it at the top of the file. Because it's a JSP tag, you can even place it before the opening <html> tag.

The Include Directive

The include directive inserts the contents of another file in the main JSP file, where the directive is located. It's useful for including copyright information, scripting language files, or anything you might want to reuse in other applications. In this example, the included file is an HTML table that creates a graphic banner.

You can see the content of the included file by viewing the page source of the main JSP file while you are running Hello, World. The included file does not contain <html> or <body> tags, because these tags would conflict with the same tags in the calling JSP file.

A Note About the JSP Tags

As you use the examples in this chapter, remember that the JSP tags are case sensitive. If, for example, you type <%@ Page %>, instead of <%@ page %>, your tag will not be recognized, and the JSP implementation will throw an exception. Some of the attributes on the tags take class names, package names, pathnames or other case-sensitive values as well.

If you have any doubts about the correct spelling or syntax of any JSP tag, see the JavaServer Pages Syntax Card.

How To Run the Example

Install the example as described in Installing and Running the Example Applications. Then, open a Web browser and go to:

http://localhost:8080/helloworld/helloworld.jsp

[Top] [Prev] [Next] [Bottom]

Copyright © 2000, Sun Microsystems, Inc. All rights reserved.

Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.