Cover | Table of Contents | Colophon
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body bgcolor="white">
<jsp:useBean id="clock" class="java.util.Date" />
<c:choose>
<c:when test="${clock.hours < 12}">
<h1>Good morning!</h1>
</c:when>
<c:when test="${clock.hours < 18}">
<h1>Good day!</h1>
</c:when>
<c:otherwise>
<h1>Good evening!</h1>
</c:otherwise>
</c:choose>
Welcome to our site, open 24 hours a day.
</body>
</html>
http://java.sun.com/products/jsp/). 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 is a regular text editor, such
as Notepad, vi, or Emacs, and of course the Tomcat server.
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.
Even so, the pure servlet-based approach
still has a few problems:
println(
) statements similar to the ones in the handcoded servlet
shown in Example 3-1, and all JSP elements are
converted to Java code that implements the corresponding dynamic
behavior. The container then compiles the servlet class.
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 available in the Java 2 SDK 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.4.2. I suggest that you use the latest version of the SDK
available for your platform.http://java.sun.com/cgi-bin/java-ports.cgi.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 available in the Java 2 SDK 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.4.2. I suggest that you use the latest version of the SDK
available for your platform.http://java.sun.com/cgi-bin/java-ports.cgi.C:\> echo %JAVA_HOME%
C:\jdk1.4.2
http://jakarta.apache.org/site/binindex.cgi.
http://www.oreilly.com/catalog/jserverpages3/.http://www.TheJSPBook.com/.C:\JSPBook> jar xvf jspbook3.zip
C:\JSPBook> xcopy /s /i ora %CATALINA_HOME%\webapps\ora
[hans@gefion jspbook] cp -R ora $CATALINA_HOME/webapps
http://www.TheJSPBook.com/.
I recommend that you do not use them initially,
though; it's easier to learn how JSP works if you
see the raw page elements before you use tools that hide them.
<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>http://www.TheJSPBook.com/.
I recommend that you do not use them initially,
though; it's easier to learn how JSP works if you
see the raw page elements before you use tools that hide them.
<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>JSP is Easy</title>
</head>
<body bgcolor="white">
<h1>JSP is as easy as ...</h1>
<%-- Calculate the sum of 1 + 2 + 3 dynamically --%>
1 + 2 + 3 = <c:out value="${1 + 2 + 3}" />
</body>
</html>
page, include, and
taglib. In this chapter, we're
using the page and the taglib
directives. The include directive is described in
Chapter 17.page
directive that specifies the content
type for the page:<%@ page contentType="text/html" %>
<%@
), followed by the directive name
(page in this case), directive attributes, and
ends with %>. A directive contains one or more
attribute name/value pairs (e.g.,
contentType="text/html"). Note that JSP element
and attribute names are case-sensitive, and in most cases, the same
is true for attribute values. All attribute values must also be
enclosed in single or double quotes.page
directive has many possible attributes. In Example 5-1, only the contentType
attribute is used. It specifies the MIME-type for the content the
page produces. The most common values are
text/html for HTML content and
text/plain for preformatted, plain text. But you
can also specify other types, such as text/xml for
browsers that support XML or text/vnd.wap.wml for
devices such as cell phones and PDAs that have built-in WML browsers.
The container sends the content type information to the browser as a
response header called Content-Type, so the
browser knows how to interpret and render the response body. If you
omit the contentType attribute, the container sets
the header to <%@ page contentType="text/html" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>JSP is Easy</title> </head> <body bgcolor="white"> <h1>JSP is as easy as ...</h1> <%-- Calculate the sum of 1 + 2 + 3 dynamically --%> 1 + 2 + 3 = <c:out value="${1 + 2 + 3}" /> </body> </html>