Vous êtes sur la page 1sur 4

A

JSP Overview

JSP Definition
A JavaServer page (JSP) is a source code file that uses Java code to create an HTML document in response to a web browser request. All communication between the client-side browser and the server-side JSP is handled using HTTP requests and responses. The JSP source code is a mix of elements that include: HTML template code JSP directives, custom tags, and standard actions Java language statements and expressions JSPs are translated into equivalent Java servlets by a JSP container provided by a servlet engine, and then the resulting Java code is compiled and run on a Java virtual machine (JVM).

JSP Architecture
JSP and Java can be executed on the client side or on the server side. Clientside Java applets executing on the end-users web browser can provide important web-based functionality. However, server-side solutions using JSP pages and Java servlets provide faster high-volume access to data and services. A typical JSP-based web application architecture includes: Web browsers on the client side for initiating requests and for rendering responses HTTP request/response links for communication among components (usually using TCP/IP-based protocols) Web server (which sometimes doubles as a servlet engine)

23

24

A: JSP Overview

Servlet engine (which supplies the JSP container) Java servlets and JSP pages Java 2 Enterprise Edition (J2EE) services (such as XML) Database (and associated JDBC driver) Both the servlets and the JSP pages can service multiple requests simultaneously by using multiple threads from a single instance of code.

JSP Process
The JSP container is the part of the servlet engine software that monitors and handles JSP page requests. The JSP container manages the JSP process (using filenames and file timestamps) in the following sequence: 1. The JSP container accepts an HTTP request for a JSP page. 2. For efficiency, the JSP container first attempts to service the request using an existing instance of the JSP page. (The JSP container determines whether a .class file of the same name as the .jsp file exists, and whether the .class file timestamp is later than the file modification timestamp of the .jsp file. If both are true, the JSP container determines whether an instance of the .class file is running, and if so, sends the HTTP request to this instance.) 3. If an up-to-date .class file does not yet exist for the JSP page, the JSP container translates the JSP source code into Java servlet source code. 4. The JSP container compiles the Java servlet source code into a .class file. 5. The JSP container loads the class into the Java virtual machine (JVM) and creates an instance. 6. The JSP container sends the HTTP request to this instance for handling. In short, the JSP container translates the JSP source code into equivalent Java servlet source code, then compiles this generated code into a class file (byte code) that is loaded and executed by the Java virtual machine (JVM).

25

JSP Page Elements


A JSP page can contain various kinds of instructions to the JSP container about code to be generated and run: Template data client end. HTML code that is passed to the browser for rendering at the

Page directive A JSP element used to specify attributes for the entire JSP page such as packages to be included and output buffer size. The syntax is
<%@ page your_attribute=your_value %>

and allows multiple attribute-value pairs. Include directive A JSP element used to manage the contents of another file used as JSP source code. The syntax is
<%@ include file=your_filename%>

where your_filename can be an absolute or relative pathname. Taglib directive A JSP element used to specify custom actions from a tag library. The syntax is
<%@ taglib uri=tag_library_uri prefix=your_tag_prefix %>

where tag_library_uri is a uniform resource indicater (pathname) pointing to the tag library descriptor (.tld) file and your_tag_prefix is a unique identifier for the custom tags defined in the .tld file. Custom tags JSP tags based on the taglib directive which is specified within the JSP file. Custom tags must use both the tag prefix and the tag name, so that the syntax is
<your_tag_prefix:your_tag_name your_attribute=your_value> ... </your_tag_prefix:your_tag_name>

and allows multiple attribute-value pairs. JSP comment A hidden comment that is not displayable using the web browser source code view, but is visible within the JSP page on the server side. The syntax is
<%-- your_hidden_JSP_comment_here -->

and allows the comment string to span multiple lines. HTML comment An HTML comment that can be viewed as HTML source when displayed using the web browser source code view (although it is not displayed in the browser page view seen by the end-user). The syntax is
<!-- your_visible_HTML_comment_here -->

and allows the comment string to span multiple lines. Java expression Any valid Java expression whose value can be converted (outprinted) to a string that can be merged with the HTML in the JSP page.

26

A: JSP Overview

The syntax is <%=your_expression %> where your_expression can be any valid Java expression. Scriplet A set of one or more Java statements used in processing HTML requests. The syntax is
<% your_statement; %>

and allows multiple statements. Declaration A set of one or more Java statements used to create a generated source file. The syntax is
<%! your_statement %>

and allows multiple statements. Standard action A set of JSP elements pre-defined in the JSP standard that link to other JSP pages, Java beans, or the Java plug-in. The syntax is
<jsp:your_standard_action your_attribute=your_value>

and allows multiple attribute-value pairs. JSP pages can thus contain an eclectic mix of instructions: HTML code, JSP directives (page, include, and taglib), custom tags, comments (both hidden and viewable), Java statements (scriplets and declarations), Java expressions, and JSP standard action elements.

Further JSP Reading


Two recent books are recommended for understanding JSP: JSP: The Complete Reference by Phil Hanna, March 2001, Osborne, $50, 876 pages, ISBN 0072127686 Professional JSP, 2nd ed. by Simon Brown, et. al., April 2001, Wrox, $60, 1000 pages, ISBN 1861004958 See also the JSP website http://java.sun.com/products/jsp/.

Vous aimerez peut-être aussi