3.1. JSP Overview

The process of making JavaServer Pages accessible on the Web is much simpler than that for servlets. Assuming you have a Web server that supports JSP, you give your file a.jsp extension and simply place it in any of the designated JSP locations (which, on many servers, is any place you could put a normal Web page): no compiling, no packages, and no user CLASSPATH settings. However, although your personal environment doesn’t need any special settings, the server still has to be set up with access to the servlet and JSP class files and the Java compiler. For details, see Chapter 1 (Server Setup and Configuration).

Although what you write often looks more like a regular HTML file than like a servlet, behind the scenes the JSP page is automatically converted to a normal servlet, with the static HTML simply being printed to the output stream associated with the servlet. This translation is normally done the first time the page is requested. To ensure that the first real user doesn’t experience a momentary delay when the JSP page is translated into a servlet and compiled, developers can simply request the page themselves after first installing it. Alternatively, if you deliver your applications on the same server you develop them on, you can deliver the precompiled servlet class files in their server-specific directories (see example locations on page 128). You can even omit the JSP source code in such a case.

One warning about the automatic translation process is in order. If you make an error in the dynamic portion of your JSP page, the system may not be able to properly translate it into a servlet. If your page has such a fatal translation-time error, the server will present an HTML error page describing the problem to the client. Internet Explorer 5, however, typically replaces server-generated error messages with a canned page that it considers friendlier. You will need to turn off this “feature” when debugging JSP pages. To do so with Internet Explorer 5, go to the Tools menu, select Internet Options, choose the Advanced tab, and make sure the “Show friendly HTTP error messages” box is not checked.

Core Approach

When debugging JSP pages, be sure to turn off Internet Explorer ’s “friendly” HTTP error messages.


Aside from the regular HTML, there are three main types of JSP constructs that you embed in a page: scripting elements, directives, and actions. Scripting elements let you specify Java code that will become part of the resultant servlet, directives let you control the overall structure of the servlet, and actions let you specify existing components that should be used and otherwise control the behavior of the JSP engine. To simplify the scripting elements, you have access to a number of predefined variables, such as request in the code snippet just shown.

This book covers versions 1.1 and 1.2 of the JavaServer Pages specification. Basic JSP constructs are backward-compatible with JSP 1.0, but custom tags, Web applications, and use of the deployment descriptor (web.xml) are specific to JSP 1.1 and later. Furthermore, JSP 1.1 did not mandate the use of Java 2; JSP 1.2 does. Consequently, if you use constructs specific to Java 2 (e.g., collections), your JSP 1.2 code will not run on JSP 1.1-compatible servers that are running on top of JDK 1.1. Finally, note that all JSP 1.x versions are completely incompatible with the long-obsolete JSP 0.92. If JSP 0.92 was your only exposure to JSP, you have a pleasant surprise in store; JSP technology has been totally revamped (and improved) since then.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.180.43