Finding the Java Sources

I l @ ve RuBoard

As soon as you start developing JSP, you'll run into your first Java error (either a syntax error or an exception). Because JSP is executing Java code instead of the raw JSP source, it can be tricky to figure out exactly where the error occurred.

Let's look at both types of errors and see how to track them down. First, let's create a JSP file with a simple syntax error in Listing 1.6.

Listing 1.6 fiveloop_syntax.jsp
 <HTML>  <HEAD><TITLE>Hello World!</TITLE></HEAD>  <BODY>  <%    int i = 0;    while (i++ < 5) {  %>   Hello World! Loop #   <% out.print(j); %>   <br>  <% }  %> </BODY> </HTML> 

Luckily, syntax errors generate fairly easy-to-read error messages and even print out the offending line so that you can locate it in your sources as shown in Figure 1.4.

Figure 1.4. A syntax error.

graphics/01fig04.gif

Less easy to deal with are runtime exceptions. Let's generate a divide-by-zero error in Listing 1.7.

Listing 1.7 fiveloop_runtime.jsp
 <HTML>  <HEAD><TITLE>Hello World!</TITLE></HEAD>  <BODY>  <%    int i = 0;    while (i++ < 5) {  %>   Hello World! Loop #  <%      out.print(i/0); %>   <br>  <% }  %> </BODY> </HTML> 

In this case, the clues are more obscured, but it is still fairly simple to find the guilty party. You simply need to find the Java file that was generated from the JSP. Looking at the backtrace, as shown in Figure 1.5, you can see that the fiveloop_runtime.jsp source got turned into fiveloop_0005fruntime_jsp.java and that the error occurred on line 70 of that file. So now, all you need to do is to find out where Tomcat stashes the Java files and look at the source.

Figure 1.5. A runtime error.

graphics/01fig05.gif

Underneath the base Tomcat directory is a directory called work. If you look there, you'll find another directory called localhost and, underneath there, a directory for every context that has been defined for Tomcat. If you look one level further, inside the cartapp directory, you'll find a jsp directory; inside that directory is the .java file. If you open it with an editor and look at line 70, you'll see this:

 out.print(i/0); 

Debugging a JSP program is basically the same process as debugging a normal Java program, except that you have to find the .java file first.

You can also look at the log to see an error, something that can be very useful if you are trying to look at a runtime error at a later date or if a third party tells you that there has been a problem on the site. The log filename is configurable in the context definition inside server.xml; the files are located in TOMCAT_HOME/logs (in our case, C:\TOMCAT\logs\localhost_cartapp_log.%date%.txt, where %date% is the current date).

I l @ ve RuBoard


MySQL and JSP Web Applications. Data-Driven Programming Using Tomcat and MySQL
MySQL and JSP Web Applications: Data-Driven Programming Using Tomcat and MySQL
ISBN: 0672323095
EAN: 2147483647
Year: 2002
Pages: 203
Authors: James Turner

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