Chapter 24 JavaServer Pages
JavaServer Pages (JSP) technology provides a quick, easy way to create Web pages with both static and dynamic content. JSPs are text-based documents that contain static markup, usually in HTML or XML, as well as Java content in the form of scripts and/or calls to Java components. JSPs extend the Java Servlet API and have access to all Java APIs and components.
You can use JSPs in many ways in Web-based applications. As part of the J2EE application model, JSPs typically run on a Web server in the middle tier, responding to HTTP requests from clients, and invoking the business methods of Enterprise JavaBeans (EJB) components on a transaction server.
JSPs are executed in a JSP engine (also called a JSP container) that is installed on a Web or application server. The JSP engine receives a request from a client and delivers it to the JSP page. The JSP page can create or use other objects to create a response. For example, it can forward the request to a servlet, Bean, or EJB, which processes the request and returns a response to the JSP page. The response is formatted according to the template in the JSP page and returned to the client.
You can deploy JSPs to the server in either source or compiled form. If a JSP page is in source form, the JSP engine typically translates the page into a class that implements the servlet interface and stores it in the server's memory. Depending on the implementation of the JSP engine, translation can occur at any time between initial deployment and the receipt of the first request. As long as the JSP page remains unchanged, subsequent requests reuse the servlet class, reducing the time required for those requests.
Deploying the JSP page as a compiled servlet class eliminates the time required to compile the JSP page when the first request is received. It also eliminates the need to have the Java compiler on the server.
Some JSP engines can handle requests and responses that use several different protocols, but all JSP engines can handle HTTP requests and responses. The JspPage and HttpJspPage classes in the javax.servlet.jsp package define the interface for the compiled JSP page, which has three methods: jspInit(), jspDestroy(), and _jspService(HttpServletRequest request, HttpServletResponse response).
For more information about the EAServer implementation of the JSP engine, see "Jaguar support of JSP".
A JSP page contains static template text that is written to the output stream. It also contains dynamic content that can take several forms:
For more detailed information about using these content types, see "Application logic in JSPs".
This sample JSP page contains a directive, a scripting element (in this case an expression), and a standard tag. The dynamic content is shown in bold:
<HTML>
<HEAD><TITLE>Simple JSP page</TITLE>
</HEAD>
<BODY>
<P>This page uses three kinds of dynamic content: </P>
<UL><LI>A page directive that imports the java util package.
<%@ page import = "java.util.*" %>
<LI>An expression to get the current date using java.util.Date. Today's date is <%= new Date() %> .
<LI>An include tag to include data from another file without parsing the content.
<jsp:include page="includedpage.txt" flush="true"/>
</UL>
</BODY>
</HTML>
The page referenced is a text file that contains one sentence and is in the same directory as the JSP file. The included page might also be another resource, such as a JSP file, and its location can be specified using a URI path.
You can call the JSP page from an HTML page with a hypertext reference:
<html><body>
<p><a href="simplepage.jsp">Click here to send a request to the simple JSP page.</p>
</body></html>
This HTML is returned to the browser:
<HTML>
<HEAD><TITLE>Simple JSP page</TITLE>
</HEAD>
<BODY>
<P>This page uses three kinds of dynamic content: </P>
<UL><LI>A page directive that imports the java util package.
<LI>An expression to get the current date using java.util.Date. Today's date is Mon Feb 14 17:03:51 EST 2000.
<LI>An include tag to include data from another file without parsing the content.
In this case the included file is a static file containing this sentence.
</UL>
</BODY>
</HTML>
Copyright © 2000 Sybase, Inc. All rights reserved. |