Chapter 22 Creating JavaServer Pages


About JavaServer Pages

JavaServer Pages (JSP) technology provides a quick, easy way to create Web pages with both static and dynamic content. JSPs are text-based documents that contain static markup, usually in HTML or XML, as well as Java content in the form of scripts and/or calls to Java components. JSPs extend the Java Servlet API and have access to all Java APIs and components.

You can use JSPs in many ways in Web-based applications. As part of the J2EE application model, JSPs typically run on a Web server in the middle tier, responding to HTTP requests from clients, and invoking the business methods of Enterprise JavaBeans (EJB) components on a transaction server.

How JavaServer Pages work

JSPs are executed in a JSP engine (also called a JSP container) that is installed on a Web or application server. The JSP engine receives a request from a client and delivers it to the JSP. The JSP can create or use other objects to create a response. For example, it can forward the request to a servlet or an EJB component, which processes the request and returns a response to the JSP. The response is formatted according to the template in the JSP and returned to the client.

Translating into a servlet class

You can deploy JSPs to the server in either source or compiled form. If a JSP is in source form, the JSP engine typically translates the page into a class that implements the servlet interface and stores it in the server's memory. Depending on the implementation of the JSP engine, translation can occur at any time between initial deployment and the receipt of the first request. As long as the JSP remains unchanged, subsequent requests reuse the servlet class, reducing the time required for those requests.

Deploying the JSP as a compiled servlet class eliminates the time required to compile the JSP when the first request is received. It also eliminates the need to have the Java compiler on the server.

Requests and responses

Some JSP engines can handle requests and responses that use several different protocols, but all JSP engines can handle HTTP requests and responses. The JspPage and HttpJspPage classes in the javax.servlet.jsp package define the interface for the compiled JSP, which has three methods:

For more information about the EAServer implementation of the JSP engine, see "EAServer support of JSP".

What a JSP contains

A JSP contains static template text that is written to the output stream. It also contains dynamic content that can take several forms:

For more detailed information about using these content types, see "Application logic in JSPs".

A simple example

This sample JSP contains a directive, a scripting element (in this case an expression), and a standard tag. The dynamic content is shown in bold:

<HTML>
<HEAD><TITLE>Simple JSP</TITLE>
</HEAD>
<BODY>
<P>This page uses three kinds of dynamic content: </P>
<UL><LI>A page directive that imports the java util package.
<%@ page import = "java.util.*" %> 
<LI>An expression to get the current date using java.util.Date. Today's date is <%= new Date() %> .
<LI>An include tag to include data from another file without parsing the content. 
<jsp:include page="includedpage.txt" flush="true"/> 
</UL>
</BODY>
</HTML>

The page referenced is a text file that contains one sentence and is in the same directory as the JSP file. The included page might also be another resource, such as a JSP file, and its location can be specified using a URI path.

You can call the JSP from an HTML page with a hypertext reference:

<html><body>
<p><a href="simplepage.jsp">Click here to send a request to the simple JSP.</p>
</body></html>

This HTML is returned to the browser:

<HTML>
<HEAD><TITLE>Simple JSP</TITLE>
</HEAD>
<BODY>
<P>This page uses three kinds of dynamic content: </P>
<UL><LI>A page directive that imports the java util package.
<LI>An expression to get the current date using java.util.Date. Today's date is Mon Feb 14 17:03:51 EST 2000.
<LI>An include tag to include data from another file without parsing the content.
In this case the included file is a static file containing this sentence.
</UL>
</BODY>
</HTML>

 


Copyright © 2002 Sybase, Inc. All rights reserved.