Oracle JavaServer Pages Developer's Guide and Reference Release 8.1.7 Part Number A83726-01 |
Library | Service | Contents | Index

## 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.

Note: 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 `javax.servlet.jsp.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.

Note: 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:

    myapp/dir1/HelloWorld.jsp

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

    http://hostname:8080/myapp/dir1/HelloWorld.jsp

(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.)

| Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
Library | Service | Contents | Index