Debugging Servlets
(Page 1 of 4 )
Last week, you learned about working with servlets. This week, you will learn how to debug servlets. This article, the last of three parts, is excerpted from chapter five of the book
Murach's Java Servlets and JSP, written by Andrea Steelman and Joel Murach (Murach; ISBN: 1890774189).
How to debug servlets
When you develop servlets, you will encounter errors. That’s why this topic gives you some ideas on how to debug servlets when you’re using a text editor to develop your servlets. If you’re using an IDE, though, it may provide advanced debugging tools that let you step through code and monitor variables so you won’t the need to use the techniques that follow.
Common servlet problems
Figure 5-9 lists four common problems that can occur when you’re working with servlets. Then, it lists some possible solutions for each of these problems.
If your servlet won’t compile, the error message that’s displayed by the compiler should give you an idea of why the servlet won’t compile. If the compiler can’t find a class that’s in one of the Java APIs, for example, you may need to install the API. If the compiler can’t locate your custom classes, you may need to modify your classpath. And if the compiler has a problem locating a package, your package statement for the class might not correspond with the directory that contains the class.
If the servlet compiles but won’t run, it may be because the servlet engine isn’t running. To solve this problem, of course, you can start the servlet engine. However, if the servlet engine is already running, you should double-check the URL to make sure that it’s pointing to the correct host, path, and package for the servlet. A common mistake, for example, is to forget to include the package name when specifying a URL for a servlet.
If you make changes to a servlet and the changes aren’t apparent when you test it, it may be because the servlet engine hasn’t reloaded the modified class. Then, if you’re using Tomcat in a stand-alone development environment, you can either make sure that servlet reloading is turned on as explained in chapter 2. Or, you can shutdown Tomcat and restart it so the changed servlet will be reloaded the next time that it’s requested.
If the HTML response page doesn’t look right when it’s rendered by the browser, the servlet is probably sending bad HTML to the browser. To fix this problem, you can use the Source command (for Internet Explorer) or the Page Source command (for Netscape) to view the HTML that has been returned to the browser. Then, you can identify the problem and modify the servlet to fix it.
Common servlet problems
ProblemPrblem | Possible solutions |
The servlet won’t compile | Make sure the compiler has access to the JAR files for all necessary APIs. |
| Make sure the classpath is pointing to the directory that contains your user-defined packages. |
| Make sure the class is in the correct directory with the correct package statement. |
The servlet won’t run | Make sure the web server is running. |
| Make sure you’re using the correct URL. |
The changes aren’t showing up | Make sure servlet reloading is on, or shutdown and startup the server so it reloads the class that you modified. |
The HTML page doesn’t look right | Select the Source or Page Source command from your browser’s View menu to view the HTML code. Then, you can read through the HTML code to identify the problem, and you can fix the problem in the servlet. |
Note
- Appendix A shows how to install the Java APIs and how to set the classpath.
Figure 5-9 Common servlet problems
Next: How to print debugging data to the console >>
More Java Articles
More By Murach Publishing
|
This article is excerpted from chapter five of the book Murach's Java Servlets and JSP, written by Andrea Steelman and Joel Murach (Murach; ISBN: 1890774189). Check it out today at your favorite bookstore. Buy this book now.
|
|