Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
50
Chapter 2 First Servlets
2.9 Debugging Servlets
Naturally, when you write servlets, you never make mistakes. However, some
of your colleagues might make an occasional error, and you can pass this
advice on to them. Seriously, though, debugging servlets can be tricky
because you don't execute them directly. Instead, you trigger their execution
by means of an HTTP request, and they are executed by the Web server. This
remote execution makes it difficult to insert break points or to read debug 
ging messages and stack traces. So, approaches to servlet debugging differ
somewhat from those used in general development. Here are seven general
strategies that can make your life easier.
1. Look at the HTML source.
If the result you see in the browser looks funny, choose  View 
Source  from the browser's menu. Sometimes a small HTML 
error like 
 instead of 
 can prevent much of 
the page from being viewed. Even better, use a formal HTML 
validator on the servlet's output. See Section 2.5 (Simple 
HTML Building Utilities) for a discussion of this approach.
2. Return error pages to the client. 
Sometimes certain classes of errors can be anticipated by the serv 
let. In these cases, the servlet should build descriptive information 
about the problem and return it to the client in a regular page or 
by means of the 
sendError
 method of 
HttpServletResponse
. 
See Chapter 6 (Generating the Server Response: HTTP Status 
Codes) for details on 
sendError
. For example, you should plan 
for cases when the client forgets some of the required form data 
and send an error page detailing what was missing. Error pages 
are not always possible, however. Sometimes something unex 
pected goes wrong with your servlet, and it simply crashes. The 
remaining approaches help you in those situations.
3. Start the server from the command line.
Most Web servers execute from a background process, and this 
process is often automatically started when the system is 
booted. If you are having trouble with your servlet, you should 
consider shutting down the server and restarting it from the 
command line. After this, 
System.out.println
 or 
Sys 
tem.err.println
 calls can be easily read from the window in 
which the server was started. When something goes wrong with 
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

jsp web hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Jsp Web Hosting Cheapest Web Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved