JavaServer

Preview Pack: JavaServer Pages


Java Web Server Documentation

A word about Preview Packs

This document talks about JavaServerTM Pages, a new feature currently under development for the JavaTM  Web ServerTM. Enjoy but don't deploy. The designation "Preview Pack" indicates the feature is still under development and subject to change.

The Java Web Server Team

JavaServer Pages provide a clean separation of content from presentation. In the past, to create dynamic pages you had to wear both a web page designer and a programmer hat. The result was often pages that were compromised rather than optimized.

JavaServer Pages give you incredible flexibility. Combine your choice of layout (minimal, moderate, or advanced HTML) with your choice of content generators (simple Java Code, 3rd-party JavaBeans components, or beans you write yourself) for the solution that's right for you. Separating the tasks means not only that you can concentrate on one task at a time, but also that you can assign those tasks to different team members. JavaServer Pages let you pick the combination that suits your needs, your skills, and your time.

The rest of this document gives you the bigger picture:

Introduction

JavaServer Pages let you embed a scripting language into web pages (HTML documents). The scripted HTML file has a .jsp extension to identify it as a JavaServer Pages file to the server. Before the page is served, the JavaServer Pages syntax is parsed and processed into a servlet on the server side. The servlet so generated will output real dynamic content back to the client.

JavaServer Pages use the JavaTM programming language as the default scripting language. This means that scripting on the server side can take advantage of the full set of capabilities that the Java programming language offers. (Support for other scripting languages might be added in the future.) In addition, JavaServer Pages provide a way to easily access reusable JavaBean components.

Traditionally, dynamic content for web pages has been generated in one of the following ways:

Both these approaches have the same shortcomings: they mix content generation with presentation.

JavaServer Pages solve this problem easily and cleanly. JavaServer Pages provide:

Common Questions

What are "JavaServer Pages"?

Why would I want to use it?

How do I implement it?

Where can I run it?

Samples using JavaServer Pages

To invoke the JavaServer Pages samples, with the Java Web Server running, enter a URL of the following form into the locator field of your browser:
http://<server_host>:<port>/<java_server_page_file>
For example:
http://rtc:8080/colors.jsp

Reference Information

JavaServer Pages Access Model

The server uses JavaServer Pages in two ways:
  1. A request comes into a JavaServer Page. The page accesses reusable JavaBean components that perform particular well-defined computations (like access a database) and store result sets as bean properties. The page uses such beans to generate dynamic content and present it back to the client. The contract that the JavaServer Pages developer cares about is the bean interface.
  2. A request comes in to a servlet that generates the dynamic content that the response would contain. The servlet invokes a JavaServer Page that will present the content generated from the servlet.

There are two APIs to support this model of request processing using JavaServer Pages. One API facilitates passing context between the invoking servlet and the JavaServer Page. The other API lets the invoking servlet specify which JavaServer Page to use.

(In both of the above cases, the page could also contain any valid Java code. We only encourage separation of content from presentation not mandate it.)

A request comes into a JavaServer Page
In the JavaServer Pages access model, dynamic content is generated by bean components which are accessed from the page. The presentation of the content is done using a JavaServer Page. This access model is shown below:

In the JavaServer Pages request model shown above, the client makes a request that is handled by a JavaServer Page. The JavaServer Page refers to a BEAN component which is passed the request parameters automatically via JavaBeans introspection. The JavaServer Page can then query the bean to obtain the results of the computation. Also if the bean happens to implement the Servlet interface, the service method of the servlet is called for each request.Each time a property is queried, the bean can do some dynamic computation to return the result.

TM or a JavaTM BlendTM component. The results are obtained by the JavaServer Page using standard bean property readers. The results so obtained are displayed using HTML. Typically, the bean developer is a Java programmer and the JavaServer Page designer is a web page designer or an HTML programmer. JavaServer Pages allows them each to do their job by cleanly separating content generation from presentation logic.

A request comes in to a servlet
Another way to achieve the same effect is shown below:

In the JavaServer Pages request model shown here, the client makes a request that is handled by a Java servlet. The servlet then generates the content that will be displayed in the HTML page. In this particular example, the servlet uses JDBC to communicate with a database in order to obtain the content. The resulting content is then wrapped into a bean which is passed to a JavaServer Page. The JavaServer Page uses the content generated by the servlet and presents it in HTML.

In this case, content generation is handled by the servlet and content presentation is handled by the JavaServer Page.

The web page designer and the servlet (or business logic) developer are different people possessing different skill sets. JavaServer Pages technology offers solutions that helps both web page developers and business logic developers do their own jobs without having to worry about the other.

Syntax

The syntax of JavaServer Pages can be divided into five main areas.

In the following sections, each of these syntax items is described in detail.

JavaServer Pages Directives

The general syntax of the JavaServer Pages directive is
<%@ {variable = " < value >"}+ %>
There is a predefined set of variables which are described below.

Following are some examples of JavaServer Pages directives:

<%@ import = "java.io.*,java.util.Hashtable" %>
<%@ extends = "javax.servlet.http.HttpServlet" %>

JavaServer Pages Declarations

All declarations that define class-wide variables for the servlet class and all methods that are classwide definitions can be defined in a JavaServer Pages file within a SCRIPT tag. The SCRIPT tag begins with a:
< SCRIPT runat=server >
and ends with a:
< /SCRIPT >
In the body of the SCRIPT tag, Java language variables that will be used in the servlet class can be declared. Class wide method definitions can also go into the body of the SCRIPT tag. The runat=server tag is required to indicate that this tag is for server-side processing rather than a hint for the browser. An example usage could be:
< SCRIPT runat=server >
int i = 0;
String foo = "Hello";
private void foo() {
// some code;
}
< /SCRIPT >

JavaServer Pages Scriptlets

The body of the JavaServer Pages scriptlet is the heart of the body of the service method of the generated servlet unless a specific method directive is specified. Any valid Java code can be used. The script specified here can rely upon a set of predefined variables. These variables are:

The code itself is embedded between <% and %> tags. For example:

<% response.getPrintWriter().print("Hello"); %>
Another example:
<%
foo = request.getParameter("Name");
out.println(foo);
%>

JavaServer Pages Expressions

These are tags that contain Java language expressions in that will be replaced with the values of those expressions. These are specified between <%= and %> tags. Expressions specifed within these tags will first be evaluated, the result will then be converted into a string and then displayed. Conversions to string representation for all primitive types like int , float etc... are provided automatically. For example:
<%= foobar %>
will substitute the value of "foobar" in place of the tag.

JavaServer Pages Beans

One of the most powerful features of JavaServer Pages is that JavaBeans can be accessed from within a JavaServer Pages file. Any of the following actions can be performed on the bean. The bean can be:

  1. created from a serialized file or a class file
  2. referred to from an HTTP session
  3. passed to the page from a servlet

So, a JavaServer Page file can access reusable server-side components by just declaring them in the file. Bean properties can then be accessed inside the JavaServer Page file. The bean does not necessarily have to come from a class file or a serialized file. For example, a servlet can generate dynamic content and store it in a bean. This bean can then be passed to a JavaServer Pages file for use within the web page defined by the file. The APIs to do this are described in the API section below.

The syntax to refer to a bean inside a JavaServer Page is defined by the BEAN tag. Once a bean is declared this way, all request parameters, be it form based or query parameter based, are set in the bean using set properties. Also if the bean happens to implement the Servlet interface, the service method of the servlet is called for each request and the init method is called once per creation. Once created a bean's lifetime is either for the lifetime of the current request or the lifetime of the HTTP session depending on what the scope in the BEAN tag is. The BEAN tag opens with:

< BEAN name=" < value > " varname=" < value >" class=" < name > " introspect="{yes|no}" serializedfile=" < value >" create="{yes|no}" scope="{request|session}">
and closes with an optional < /BEAN > tag. The attributes name, varname, class, create, scope, and serializedfile are described below.

An example BEAN tag might be:

< BEAN name="foobar" class="FooClass" scope="request" >

Once a bean is declared it can be accessed anywhere in the file. For example, the bean declared in the example above can be accessed inside a JavaServer Pages file as follows:

The name of the row is <%= foobar.getRowName() %> .

JavaServer Pages APIs

Two interfaces are used to support JavaServer Pages. These APIs provide a way to separate content generation from the presentation of this content. The idea is for a servlet to be able to generate content and the store this content (typically in the form of a bean) in the request context. Then the servlet that generated the context generates a response by passing the request context to a JavaServer Pages file that mostly contains presentation logic. To access the dynamic content, the JavaServer Pages file uses the BEAN tag described above. The two JavaServer Pages APIs are defined below:


Top
java-server-feedback@java.sun.com
Copyright © 1998 Sun Microsystems, Inc.
All Rights Reserved.