Debugging

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    

JSTL: JSP Standard Tag Library Kick Start
By Jeff Heaton

Table of Contents
Chapter 12.  Debugging and Deploying Tag Libraries


Debugging is a critical process for any programming endeavor. JSTL, like any other programming language, is susceptible to programming errors. To effectively program JSTL and custom tag libraries, you must know how to combat these errors. In this section, we show you debugging techniques you can use to debug errors you're likely to encounter when using JSTL and your own custom tag libraries.

Various types of errors can be returned from a JSTL page. The error reports that JSTL displays are designed to provide information that will lead you to the cause of the problem.

Error reports returned from a JSP server may look like a complex jumble of text and numbers, but you must learn how to read them to determine the cause of the error. Several types of errors are common with JSTL pages; they range from general exceptions to JSTL-specific parsing errors. We begin by examining a general exception.

General Exceptions

An application displays a general exception whenever it executes a JSP page that causes a Java exception to be thrown. More than one type of exception can be thrown to a JSP page. Any exception that can be thrown by Java can cause a JSP page to terminate if the exception is not caught.

General exceptions have many causes, and we won't attempt to list them all. In this section, we examine the exception that is thrown when the JSTL processor encounters beginning and ending tags that do not match. This can often happen when you forget an ending tag. When such an error occurs, you'll see output similar to Figure 12.1.

Figure 12.1. A general JSTL exception.

graphics/12fig01.jpg

In Figure 12.1, we neglected to include an ending tag, as the exception indicates. The JSP page that causes the error is shown in Listing 12.1.

Listing 12.1 A Missing Ending Tag (general.jsp)
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <html>   <head>     <title>General JSTL</title>   </head>   <body>     <c:forEach var="i" begin="1" end="10" step="1">   </body> </html> 

A missing ending tag error occurs as a result of an exception being thrown by the parser. With this type of error, you are not provided with the line number within the JSP page where this error occurred. Instead, you are given a line number in the compiled servlet. While this number is not as helpful, the error text does yield some information:

org.apache.jasper.compiler.ParseException: End of content reached while more parsing required: tag nesting error? 

As you can see, Tomcat has reported that an ending tag is missing.

WARNING

Note that these errors are tied not only to which Web server you use but also to the version. These errors may look different on different Web servers and versions of those Web servers.


In the next section, we examine a JSTL-specific error that gives you the line number and column that caused the error.

A JSTL-Specific Error

A JSTL-specific error can occur when you forget an attribute to JSTL tag. There are other causes, but forgetting required attributes is one of the most common. When you receive a JSTL-specific exception, the output looks similar to that shown in Figure 12.2.

Figure 12.2. A JSTL-specific exception.

graphics/12fig02.jpg

A JSTL-specific error gives the line number and column that this error occurred on. This allows you to determine the exact part of the JSP page that caused this error. If you look closely at the first line of the exception thrown, you'll notice the following text:

/example/ch12/debug/jstl.jsp(8,6) According to the TLD attribute   value is mandatory for tag out 

This text tells you exactly which file caused the error. In this case, the error was caused by a file named jstl.jsp. The numbers following the filename tell you exactly which line and column in the JSP page caused this error. The number 8 indicates the eighth line, and the number 6 indicates the sixth column. Using this information you can quickly track down exactly which tag caused the error that JSTL is complaining about.

For this error, we have simply forgotten to include the value attribute for the <c:out> tag. To see this program, refer to Listing 12.2.

Listing 12.2 A Missing Attribute (jstl.jsp)
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <html>   <head>     <title>Count to 10 Example(using JSTL)</title>   </head>   <body>     <c:forEach var="i" begin="1" end="10" step="1">       <c:out  />       <br />     </c:forEach>   </body> </html> 

The errors that we've examined are the result of JSTL code. Let's now look at how to handle errors that occur inside your own tag libraries.

An Error in a Tag Library

In Chapter 11, we showed you how to create your own custom tag libraries. These custom tag libraries are Java programs, and like all Java programs, they can develop errors. If an error occurs in your custom tag, it is likely that a general exception will be thrown. You must recognize this form of general exception so that you can quickly determine which of your tags has caused the error. If you receive an error from one of your custom tag libraries, the output from your browser will look similar to that shown in Figure 12.3.

Figure 12.3. A tag library exception.

graphics/12fig03.jpg

As you can see in Figure 12.3, an error is being raised by the custom tag library. To understand the source of this error, we must examine the JSP code, shown in Listing 12.3, that produced the error.

Listing 12.3 A Custom Tag Error (custom.jsp)
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <%@ taglib uri="http://java.jeffheaton.com/taglib/jstl/forum"   prefix="forum" %> <html>   <head>     <title>Custom Tag Error</title>   </head>   <body>     <forum:newUser  password="admin" var="user"   scope="session"/>   </body> </html> 

The cause of our error is that we are attempting to create a user that has the same name as an existing user. We are attempting to create a user named admin, and the forum application should already have a user named admin. Therefore, this tag throws an error.

Most bugs are not as simple as the ones we've discussed. Often, your code will have logic errors and will not throw an exception, yet the code will not do what it is supposed to do, either. To handle this sort of bug, you must use a debugger. Debuggers are usually made available as part of an IDE. In the next section, we step you through the code of our custom tag library using a debugger.


    printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    
    Top

    [0672324504/ch12lev1sec1]

     
     


    JSTL. JSP Standard Tag Library Kick Start
    JSTL: JSP Standard Tag Library Kick Start
    ISBN: 0672324504
    EAN: 2147483647
    Year: 2001
    Pages: 93
    Authors: Jeff Heaton

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