18. JavaServer Pages

Java Servlet Programming, 2nd Edition > 18. JavaServer Pages

 
< BACKCONTINUE >

Chapter 18. JavaServer Pages

JavaServer Pages, commonly known as JSP, is a technology created by Sun Microsystems and closely tied to servlets. JSP was one of the first efforts to create a non-proprietary servlet-based content creation system. If you've been following servlets long, you may remember JSP was first announced in the spring of 1998 early enough in fact for the first edition of this book to include a short tutorial on JSP's prerelease 0.91 version. Of course, JSP has changed significantly since then. In this section, we cover JSP 1.1, built on top of Servlet API 2.2.

As with servlets, Sun releases a JSP specification (created by an expert group consisting of outside vendors and individuals), and then third-party vendors compete on their implementation of that standard. Unlike the other technologies discussed that build on top of pure servlets, JSP is a specification, not a product, and requires support from the server in order to work. Most servlet container vendors are providing this support, including Tomcat where the JSP engine is named Jasper and JSP is a core component of Java 2, Enterprise Edition ( J2EE).

One stated purpose of JSP (quoting from the specification) is to "enable the separation of dynamic and static content." Another purpose is to "enable the authoring of Web pages that create dynamic content easily but with maximum power and flexibility." JSP accomplishes both of these goals fairly well. However, because of the "maximum power" inherent in the JSP design, the JSP page author always has complete control of the system, and thus one could say JSP enables separation of content from presentation but doesn't enforce or encourage it as alternatives do. It's similar to how C++ allows good object-oriented design but doesn't structurally promote that good design as much as Java does.

JSP is also a very flexible technology, and there are many different ways to make use of JSP. One way is as a "shorthand" way to write a servlet: a servlet programmer, comfortable with Java, can put Java code directly in a JSP page instead of writing a full servlet and by that gains niceties like eliminating out.println( ) calls, being able to point at JSP files directly, and taking advantage of JSP's autocompile feature. By writing a JSP page instead of a servlet, however, the programmer loses full contact with the code and loses the ability to control the true execution environment to do things like extend com.oreilly.servlet.CacheHttpServlet or generate binary (image) output. For this reason real coding is best left to regular servlets, with JSPs used primarily for presentation logic.

Even when used just for presentation logic, JSP pages can be used many different ways. One way involves the use of JavaBeans embedded within the page. Another way involves creating custom tags that look like HTML but are actually hooks into supporting Java code. Yet another way involves sending all requests to a servlet that performs business logic and having that servlet forward the request for rendering to a JSP page using the RequestDispatcher mechanism. This technique is often called the Model 2 architecture, a name taken from the JSP 0.92 specification. Then there are other architectures, some given whimsical names like Model 1, Model 2, or Model 2 + 1. There's no shortage to ideas for the best way to use JSP.

In this chapter we'll look at the various ways to use JSPs, everything from "servlet shorthand" to custom tags. The coverage will be fast and furious but should provide enough of a base for you to compare JSP against the alternatives. We will not discuss all the various architecture options and will instead concentrate on the technical nuts and bolts. For architecture options and more information on Java-Server Pages, see the JSP home page at http://java.sun.com/products/jsp (a syntax "cheat sheet" for JSP 1.0 is available at http://java.sun.com/products/jsp/syntax.pdf) and the book JavaServerPages by Hans Bergsten (O'Reilly).


Last updated on 3/20/2003
Java Servlet Programming, 2nd Edition, © 2001 O'Reilly

< BACKCONTINUE >


Java servlet programming
Java Servlet Programming (Java Series)
ISBN: 0596000405
EAN: 2147483647
Year: 2000
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net