Cover | Table of Contents | Colophon
mod_
perl
from Apache, NSAPI from Netscape,
ISAPI
from Microsoft, and
Java Servlets from Sun Microsystems,
have been created over the years. While these solutions offer better
performance and scalability, all of these technologies suffer from a
common problem: they generate web pages by embedding
HTML directly in programming
language code. This pushes the creation of dynamic web pages
exclusively into the realm of programmers. JavaServer Pages, however,
changes all that.<html>
<body bgcolor="white">
<% java.util.Date clock = new java.util.Date( ); %>
<% if (clock.getHours( ) < 12) { %>
<h1>Good morning!</h1>
<% } else if (clock.getHours( ) < 18) { %>
<h1>Good day!</h1>
<% } else { %>
<h1>Good evening!</h1>
<% } %>
Welcome to our site, open 24 hours a day.
</body>
</html>
http://java.sun.com/products/jsp/ ). Appendix E, also contains a collection of references to
JSP-related products, web hosting services, and sites where you can
learn more about JSP and related technologies. You may want to
evaluate some of these products when you're ready to start
developing your application, but all you really need to work with the
examples in this book are a regular text editor, such as Notepad, vi,
or Emacs, and of course the Tomcat server.
/index.html /company/contact.html /products/list.jsp /images/banner.gif /WEB-INF/web.xml /WEB-INF/lib/bean.jar /WEB-INF/lib/actions.jar /WEB-INF/classes/com/mycorp/servlets/PurchaseServlet.class /WEB-INF/classes/com/mycorp/util/MyUtils.class /WEB-INF/...
public class OrderServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter( );
if (isOrderInfoValid(request)) {
saveOrderInfo(request);
out.println("<html>");
out.println(" <head>");
out.println(" <title>Order Confirmation</title>");
out.println(" </head>");
out.println(" <body>");
out.println(" <h1>Order Confirmation</h1>");
renderOrderInfo(request);
out.println(" </body>");
out.println("</html>");
}
...public class OrderServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter( );
if (isOrderInfoValid(request)) {
saveOrderInfo(request);
out.println("<html>");
out.println(" <head>");
out.println(" <title>Order Confirmation</title>");
out.println(" </head>");
out.println(" <body>");
out.println(" <h1>Order Confirmation</h1>");
renderOrderInfo(request);
out.println(" </body>");
out.println("</html>");
}
...
isOrderInfoValid( ) and saveOrderInfo(
)) and also generates the response HTML code, embedded
directly in the servlet code using println( )
calls. A more structured servlet application isolates different
pieces of the processing in various reusable utility classes, and may
also use a separate class library for generating the actual HTML
elements in the response. But even so, the pure servlet-based
approach still has a few problems:
|
Element
|
Description
|
|---|---|
<%@ page ... %> |
Defines page-dependent attributes, such as scripting language, error
page, and buffering requirements
|
<%@ include ... %> |
http://jakarta.apache.org.http://java.sun.com/j2se/.http://www10.software.ibm.com/developerworks/opensource/jikes/
). Tomcat can be configured to use Jikes
instead of the javac compiler from Sun; read the
Tomcat documentation if you would like to try this. To make things
simple, though, I suggest installing the Java 2 SDK from Sun. The
examples were developed and tested with Java 2 SDK, Standard Edition,
v1.2.2 and v1.3. I recommend that you use the latest version of the
SDK available for your platform.http://java.sun.com/j2se/.http://www10.software.ibm.com/developerworks/opensource/jikes/
). Tomcat can be configured to use Jikes
instead of the javac compiler from Sun; read the
Tomcat documentation if you would like to try this. To make things
simple, though, I suggest installing the Java 2 SDK from Sun. The
examples were developed and tested with Java 2 SDK, Standard Edition,
v1.2.2 and v1.3. I recommend that you use the latest version of the
SDK available for your platform.http://java.sun.com/cgi-bin/java-ports.cgi/
http://jakarta.apache.org/downloads/binindex.html
C:\Jakarta\jakarta-tomcat\bin> shutdown
http://www.oreilly.com/catalog/jserverpages/
http://www.TheJSPBook.com
C:\JSPBook> jar xvf jspbook.zip
C:\JSPBook> xcopy /s /i ora %TOMCAT_HOME%\webapps\ora
[hans@gefion /usr/local/jspbook] cp -R ora $TOMCAT_HOME/webapps
<%@ page language="java" contentType="text/html" %>
<html>
<body bgcolor="white">
<jsp:useBean id="clock" class="java.util.Date" />
The current time at the server is:
<ul>
<li>Date: <jsp:getProperty name="clock" property="date" />
<li>Month: <jsp:getProperty name="clock" property="month" />
<li>Year: <jsp:getProperty name="clock" property="year" />
<li>Hours: <jsp:getProperty name="clock" property="hours" />
<li>Minutes: <jsp:getProperty name="clock" property="minutes" />
</ul>
</body>
</html>
birthDate property is set to a valid
date./**
* This is just a simple example of a Java class
* with two instance variables, a constructor, and
* some methods.
*/
public class Rectangle {
// Data
private int width;
private int height;
// Constructor
public Rectangle(int w, int h) {
width = w;
height = h;
}
// Methods
public int getWidth( ) {
return width;
}
public void setWidth(int w) {
width = w;
}
public int getHeight( ) {
return height;
}
public void setHeight(int h) {
height = h;
}
public double getArea( ) {
double area;
area = width * height;
return area;
}
}/**
* This is just a simple example of a Java class
* with two instance variables, a constructor, and
* some methods.
*/
public class Rectangle {
// Data
private int width;
private int height;
// Constructor
public Rectangle(int w, int h) {
width = w;
height = h;
}
// Methods
public int getWidth( ) {
return width;
}
public void setWidth(int w) {
width = w;
}
public int getHeight( ) {
return height;
}
public void setHeight(int h) {
height = h;
}
public double getArea( ) {
double area;
area = width * height;
return area;
}
}
|
Variable Name
|
Java Type
|
|---|---|
request |
javax.servlet.http.HttpServletRequest |
response |
javax.servlet.http.HttpServletResponse |
pageContext |
javax.servlet.jsp.PageContext |
session |
javax.servlet.http.HttpSession |
application |
javax.servlet.ServletContext |
out |
javax.servlet.jsp.JspWriter |
config |
javax.servlet.ServletConfig |
page |
java.lang.Object |
UserAgent (containing information about the type
of client that is accessing the page).<jsp:getProperty> and the
<jsp:setProperty> actions to access a
bean's properties. However, a bean is just a Java class that
follows certain coding conventions, so you can also call its methods
directly.get
and set,
respectively, plus the name of the property. For instance, you can
retrieve the value of a property named month in a
bean with the method getMonth( ) and set it with
the method setMonth( ). Individually, the accessor
method for reading a property value is known as the
getter method, and the accessor method for
writing a property value is the setter<jsp:getProperty> action in some places, but
it is also useful to insert the value of any Java expression that can
be treated as a String.<%= and ends with
%>. Note that the only syntax difference
compared to a scriptlet is the equals sign (=) in
the start identifier. An example is:<%= userInfo.getUserName( ) %>
<jsp:getProperty> actions to fill out the
form fields with UserInfoBean values. To recap, it
looked like this:<tr>
<td>Name:</td>
<td><input type="text" name="userName"
value="<jsp:getProperty
name="userInfo"
property="userNameFormatted"
/>">
</td>
</tr>
<jsp:getProperty> syntax
is distracting, since it's used as the value of an HTML
element. You can use expressions to make the user input form page
easier to read. The following example shows the same part of the page
with the <jsp:getProperty> action replaced
with an expression:<tr>
<td>Name:</td>
<td><input type="text" name="userName"
value="<%= userInfo.getUserNameFormatted( ) %>" >
</td>
</tr>
UserInfoBean provides a set of properties with
values formatted for HTML output; that's what we used in Chapter 5 to avoid confusing the browser with special
characters in the bean property values. However, it's much
easier to reuse a bean if it doesn't need to format its
property values for a certain type of output. With expressions, we
can let the bean be ignorant of how its property values are used, and
use a utility class to do the formatting instead, as in Example 6.5.