Sun Java System Web Server 7.0 Developer's Guide to Java Web Applications

Creating JSPs

You can create JSPs basically the same way you create HTML files. You can use an HTML editor to create pages and edit the page layout. You make a page a JSP by inserting tags that are specific to JSP into the raw source code where needed, and by giving the file a .jsp extension. In JSP 2.0 web application resources with an extension of .jspx are interpreted as JSP documents.

JSPs specification distinguishes between two types: classic syntax, which is defined by the JSP specification itself, and XML syntax, also referred to as JSP documents. For a summary of the JSP tags you can use, see JSP Tag Libraries and Standard Portable Tags.

JSPs are compiled into servlets, so servlet design considerations also apply to JSPs. JSPs and servlets can perform the same tasks, but each excels at one task at the expense of the other. Servlets are strong in processing and adaptability. However, performing HTML output from servlet requires you to write several cumbersome println statements. Conversely, although JSPs excel at layout tasks because they can be created with HTML editors, performing complex computational or processing tasks with them is awkward. For information about servlets, see Chapter 4, Developing Servlets.

Additional JSP design tips are described in the following sections:

Designing for Ease of Maintenance

Each JSP can call or include any other JSP. For example, you can create a generic corporate banner, a standard navigation bar, and a left-side column table of contents, where each element is in a separate JSP and is included for each page built. The page can be constructed with a JSP functioning as a frameset, dynamically determining the pages to load into each subframe. A JSP can also be included when it is compiled into a servlet or when a request arrives.

Designing for Portability

JSPs are completely portable between different applications and different servers. This portability can be disadvantage because they have no particular application data knowledge.

Generic JSPs can be used for portable page elements, such as navigation bars or corporate headers and footers, which are meant to be included in other JSPs. You can create a library of reusable generic page elements to use throughout an application, or even among several applications.

For example, the minimal generic JSP is a static HTML page with no JSP-specific tag. A slightly less minimal JSP contains some Java code that operates on generic data, such as printing the date and time, or that makes a change to the page's structure based on a standard value set in the request object.

Handling Exceptions

If an uncaught exception occurs in a JSP file, Sun Java System Web Server 7.0 generates an exception, usually a 404 or 500 error. To avoid this problem, set the errorPage attribute of the page directive, for example, <%@ page other_page_directive_attr_list errorPage=/oops.jsp%> .