OracleJSP Support for JavaServer Pages Developer's Guide and Reference

Part Number A90208-01
Go To Documentation Library
Go To Product List
Book List
Go To Table Of Contents
Go To Index

Master Index


Go to previous page Go to next page

General Overview

This chapter reviews standard features and functionality of JavaServer Pages technology. For further information, consult the Sun Microsystems JavaServer Pages Specification, Version 1.1.

(For an overview of Oracle-specific OracleJSP features, see Chapter 2, "Overview of Oracle's JSP Implementation". Also note that Appendix B, "Servlet and JSP Technical Background", provides related background on standard servlet and JSP technology.)

The following topics are covered here:

Introduction to JavaServer Pages

JavaServer Pages(TM) is a technology specified by Sun Microsystems as a convenient way of generating dynamic content in pages that are output by a Web application (an application running on a Web server).

This technology, which is closely coupled with Java servlet technology, allows you to include Java code snippets and calls to external Java components within the HTML code (or other markup code, such as XML) of your Web pages. JavaServer Pages (JSP) technology works nicely as a front-end for business logic and dynamic functionality in JavaBeans and Enterprise JavaBeans (EJBs).

JSP code is distinct from other Web scripting code, such as JavaScript, in a Web page. Anything that you can include in a normal HTML page can be included in a JSP page as well.

In a typical scenario for a database application, a JSP page will call a component such as a JavaBean or Enterprise JavaBean, and the bean will directly or indirectly access the database, generally through JDBC or perhaps SQLJ.

A JSP page is translated into a Java servlet before being executed (typically on demand, but sometimes in advance), and it processes HTTP requests and generates responses similarly to any other servlet. JSP technology offers a more convenient way to code the servlet.

Furthermore, JSP pages are fully interoperable with servlets--JSP pages can include output from a servlet or forward to a servlet, and servlets can include output from a JSP page or forward to a JSP page.

What a JSP Page Looks Like

Here is an example of a simple JSP page. (For an explanation of JSP syntax elements used here, see "Overview of JSP Syntax Elements".)

<HEAD><TITLE>The Welcome User JSP</TITLE></HEAD>
<% String user=request.getParameter("user"); %>
<H3>Welcome <%= (user==null) ? "" : user %>!</H3>
<P><B> Today is <%= new java.util.Date() %>. Have a nice day! :-)</B></P>
<B>Enter name:</B>
<INPUT TYPE="text" NAME="user" SIZE=15>
<INPUT TYPE="submit" VALUE="Submit name">

In a JSP page, Java elements are set off by tags such as <% and %>, as in the preceding example. In this example, Java snippets get the user name from an HTTP request object, print the user name, and get the current date.

This JSP page will produce the following output if the user inputs the name "Amy":

Text description of welcoamy.gif follows.
Text description of the illustration welcoamy.gif

Convenience of JSP Coding Versus Servlet Coding

Combining Java code and Java calls into an HTML page is more convenient than using straight Java code in a servlet. JSP syntax gives you a shortcut for coding dynamic Web pages, typically requiring much less code than Java servlet syntax. Following is an example contrasting servlet code and JSP code.

Servlet Code

import javax.servlet.*;
import javax.servlet.http.*;

public class Hello extends HttpServlet
   public void doGet(HttpServletRequest rq, HttpServletResponse rsp)
      try {
         PrintWriter out = rsp.getWriter();
         out.println("<P>Today is "+new java.util.Date()+".</P>");
      } catch (IOException ioe)
        // (error processing) 

(See "The Servlet Interface" for some background information about the standard HttpServlet abstract class, HttpServletRequest interface, and HttpServletResponse interface.)

JSP Code

<P>Today is <%= new java.util.Date() %>.</P>

Note how much simpler JSP syntax is. Among other things, it saves Java overhead such as package imports and try...catch blocks.

Additionally, the JSP translator automatically handles a significant amount of servlet coding overhead for you in the .java file that it outputs, such as directly or indirectly implementing the standard javax.servlet.jsp.HttpJspPage interface (see "Standard JSP Interfaces and Methods") and adding code to acquire an HTTP session.

Also note that because the HTML of a JSP page is not embedded within Java print statements as is the case in servlet code, you can use HTML authoring tools to create JSP pages.

Separation of Business Logic from Page Presentation--Calling JavaBeans

JSP technology allows separating the development efforts between the HTML code that determines static page presentation, and the Java code that processes business logic and presents dynamic content. It therefore becomes much easier to split maintenance responsibilities between presentation and layout specialists who may be proficient in HTML but not Java, and code specialists who may be proficient in Java but not HTML.

In a typical JSP page, most Java code and business logic will not be within snippets embedded in the JSP page--instead, it will be in JavaBeans or Enterprise JavaBeans that are invoked from the JSP page.

JSP technology offers the following syntax for defining and creating an instance of a JavaBeans class:

<jsp:useBean id="pageBean" class="mybeans.NameBean" scope="page" />

This example creates an instance, pageBean, of the mybeans.NameBean class (the scope parameter will be explained later in this chapter).

Later in the page, you can use this bean instance, as in the following example:

Hello <%= pageBean.getNewName() %> !

(This prints "Hello Julie !", for example, if the name "Julie" is in the newName attribute of pageBean, which might occur through user input.)

The separation of business logic from page presentation allows convenient division of responsibilities between the Java expert who is responsible for the business logic and dynamic content--this developer owns and maintains the code for the NameBean class--and the HTML expert who is responsible for the static presentation and layout of the Web page that the application user sees--this developer owns and maintains the code in the .jsp file for this JSP page.

Tags used with JavaBeans--useBean to declare the JavaBean instance and getProperty and setProperty to access bean properties--are further discussed in "JSP Actions and the <jsp: > Tag Set".

JSP Pages and Alternative Markup Languages

JavaServer Pages technology is typically used for dynamic HTML output, but the Sun Microsystems JavaServer Pages Specification, Version 1.1 also supports additional types of structured, text-based document output. A JSP translator does not process text outside of JSP elements, so any text that is appropriate for Web pages in general is typically appropriate for a JSP page as well.

A JSP page takes information from an HTTP request and accesses information from a data server (such as through a SQL database query). It combines and processes this information and incorporates it as appropriate into an HTTP response with dynamic content. The content can be formatted as HTML, DHTML, XHTML, or XML, for example.

For information about XML support, see "OracleJSP Support for XML and XSL".

JSP Execution

This section provides a top-level look at how a JSP is run, including on-demand translation (the first time a JSP page is run), the role of the JSP container and the servlet container, and error processing.


The term JSP container is used in the Sun Microsystems JavaServer Pages Specification, Version 1.1, replacing the term JSP engine that was used in earlier specifications. The two terms are synonymous. 

JSP Containers in a Nutshell

A JSP container is an entity that translates, executes, and processes JSP pages and delivers requests to them.

The exact make-up of a JSP container varies from implementation to implementation, but it will consist of a servlet or collection of servlets. The JSP container, therefore, is executed by a servlet container. (Servlet containers are summarized in "Servlet Containers".)

A JSP container may be incorporated into a Web server if the Web server is written in Java, or the container may be otherwise associated with and used by the Web server.

JSP Pages and On-Demand Translation

Presuming the typical on-demand translation scenario, a JSP page is usually executed through the following steps:

  1. The user requests the JSP page through a URL ending with a .jsp file name.

  2. Upon noting the .jsp file name extension in the URL, the servlet container of the Web server invokes the JSP container.

  3. The JSP container locates the JSP page and translates it if this is the first time it has been requested. Translation includes producing servlet code in a .java file and then compiling the .java file to produce a servlet .class file.

    The servlet class generated by the JSP translator subclasses a class (provided by the JSP container) that implements the javax.servlet.jsp.HttpJspPage interface (described in "Standard JSP Interfaces and Methods"). The servlet class is referred to as the page implementation class. This document will refer to instances of page implementation classes as JSP page instances.

    Translating a JSP page into a servlet automatically incorporates standard servlet programming overhead into the generated servlet code, such as implementing the HttpJspPage interface and generating code for its service method.

  4. The JSP container triggers instantiation and execution of the page implementation class.

The servlet (JSP page instance) will then process the HTTP request, generate an HTTP response, and pass the response back to the client.


The preceding steps are loosely described for purposes of this discussion. As mentioned earlier, each vendor decides how to implement its JSP container, but it will consist of a servlet or collection of servlets. For example, there may be a front-end servlet that locates the JSP page, a translation servlet that handles translation and compilation, and a wrapper servlet class that is subclassed by each page implementation class (because a translated page is not a pure servlet and cannot be run directly by the servlet container). A servlet container is required to run each of these components.  

Requesting a JSP Page

A JSP page can be requested either directly--through a URL--or indirectly--through another Web page or servlet.

Directly Request a JSP Page

As with a servlet or HTML page, the end-user can request a JSP page directly by URL. For example, assume you have a HelloWorld JSP page that is located under the myapp application root directory in the Web server, as follows:


If it uses port 8080 of the Web server, you can request it with the following URL:


(The application root directory is specified in the servlet context of the application. "Servlet Contexts" summarizes servlet contexts.)

The first time the end-user requests HelloWorld.jsp, the JSP container triggers both translation and execution of the page. With subsequent requests, the JSP container triggers page execution only; the translation step is no longer necessary.

Indirectly Requesting a JSP Page

JSP pages, like servlets, can also be executed indirectly--linked from a regular HTML page or referenced from another JSP page or from a servlet.

When invoking one JSP page from a JSP statement in another JSP page, the path can be either relative to the application root--known as context-relative or application-relative--or relative to the invoking page--known as page-relative. An application-relative path starts with "/"; a page-relative path does not.

Be aware that, typically, neither of these paths is the same path as used in a URL or HTML link. Continuing the example in the preceding section, the path in an HTML link is the same as in the direct URL request, as follows:

<a href="/myapp/dir1/HelloWorld.jsp" /a>

The application-relative path in a JSP statement is:

<jsp:include page="/dir1/HelloWorld.jsp" flush="true" />

The page-relative path to invoke HelloWorld.jsp from a JSP page in the same directory is:

<jsp:forward page="HelloWorld.jsp" />

("JSP Actions and the <jsp: > Tag Set" discusses the jsp:include and jsp:forward statements.)

Overview of JSP Syntax Elements

You have seen a simple example of JSP syntax in "What a JSP Page Looks Like"; now here is a top-level list of syntax categories and topics:

This section introduces each category, including basic syntax and a few examples. For more information, see the Sun Microsystems JavaServer Pages Specification, Version 1.1.


There are XML-compatible alternatives to the syntax for JSP directives, declarations, expressions, and scriptlets. See "XML-Alternative Syntax"


Directives provide instruction to the JSP container regarding the entire JSP page. This information is used in translating or executing the page. The basic syntax is as follows:

<%@ directive attribute1="value1" attribute2="value2"... %>

The JSP 1.1 specification supports the following directives:

Scripting Elements

JSP scripting elements include the following categories of snippets of Java code that can appear in a JSP page:

JSP Objects and Scopes

In this document, the term JSP object refers to a Java class instance declared within or accessible to a JSP page. JSP objects can be either:


Scopes are discussed below, in "Object Scopes".

Explicit Objects

Explicit objects are typically JavaBean instances declared and created in jsp:useBean action statements. The jsp:useBean statement and other action statements are described in "JSP Actions and the <jsp: > Tag Set", but an example is also shown here:

<jsp:useBean id="pageBean" class="mybeans.NameBean" scope="page" />

This statement defines an instance, pageBean, of the NameBean class that is in the mybeans package. The scope parameter is discussed in "Object Scopes" below.

You can also create objects within Java scriptlets or declarations, just as you would create Java class instances in any Java program.

Object Scopes

Objects in a JSP page, whether explicit or implicit, are accessible within a particular scope. In the case of explicit objects, such as a JavaBean instance created in a jsp:useBean action statement, you can explicitly set the scope with the following syntax (as in the example in the preceding section, "Explicit Objects"):


There are four possible scopes:

Implicit Objects

JSP technology makes available to any JSP page a set of implicit objects. These are Java class instances that are created automatically by the JSP mechanism and that allow interaction with the underlying servlet environment.

The following implicit objects are available. For information about methods available with these objects, refer to the Sun Microsystems Javadoc for the noted classes and interfaces at the following location:

Using an Implicit Object

Any of the implicit objects discussed in the preceding section may be useful. The following example uses the request object to retrieve and display the value of the username parameter from the HTTP request:

<H3> Welcome <%= request.getParameter("username") %> ! <H3>

JSP Actions and the <jsp: > Tag Set

JSP action elements result in some sort of action occurring while the JSP page is being executed, such as instantiating a Java object and making it available to the page. Such actions may include the following:

Action elements use a set of standard JSP tags that begin with <jsp: syntax. Although the tags described earlier in this chapter that begin with <% syntax are sufficient to code a JSP page, the <jsp: tags provide additional functionality and convenience.

Action elements also use syntax similar to that of XML statements, with similar "begin" and "end" tags such as in the following example:

<jsp:sampletag attr1="value1" attr2="value2" ... attrN="valueN">

Or, where there is no body, the action statement is terminated with an empty tag:

<jsp:sampletag attr1="value1", ..., attrN="valueN" />

The JSP specification includes the following standard action tags, which are introduced and briefly discussed here:

Tag Libraries

In addition to the standard JSP tags discussed previously in this section, the JSP specification lets vendors define their own tag libraries and also lets vendors implement a framework allowing customers to define their own tag libraries.

A tag library defines a collection of custom tags and can be thought of as a JSP sub-language. Developers can use tag libraries directly, in manually coding a JSP page, but they might also be used automatically by Java development tools. A tag library must be portable between different JSP container implementations.

Import a tag library into a JSP page using the taglib directive, introduced in "Directives".

Key concepts of standard JavaServer Pages support for JSP tag libraries include the following topics:

For information about these topics, see "Standard Tag Library Framework".

For information about the sample tag library provided with OracleJSP, see "Overview of the JSP Markup Language (JML) Sample Tag Library"

For further information, see the Sun Microsystems JavaServer Pages Specification, Version 1.1.

Go to previous page Go to next page
Copyright © 1996-2001, Oracle Corporation.

All Rights Reserved.
Go To Documentation Library
Go To Product List
Book List
Go To Table Of Contents
Go To Index

Master Index