|Oracle JavaServer Pages Developer's Guide and Reference
Part Number A83726-01
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.
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.
Presuming the typical on-demand translation scenario, a JSP page is usually executed through the following steps:
.jspfile name extension in the URL, the servlet container of the Web server invokes the JSP container.
.javafile and then compiling the
.javafile to produce a servlet
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.
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.
A JSP page can be requested either directly--through a URL--or indirectly--through another Web page or servlet.
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.
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:
The application-relative path in a JSP statement is:
The page-relative path to invoke
HelloWorld.jsp from a JSP page in the same directory is:
("JSP Actions and the <jsp: > Tag Set" discusses the