Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
2.9 Debugging Servlets
51
your servlet, your first task is to discover exactly how far the
servlet got before it failed and to gather some information about
the key data structures during the time period just before it
failed. Simple
println
statements are surprisingly effective for
this purpose. If you are running your servlets on a server that
you cannot easily halt and restart, then do your debugging with
the JSWDK, Tomcat, or the Java Web Server on your personal
machine, and save deployment to the real server for later.
4. Use the log file.
The
HttpServlet
class has a method called
log
that lets you
write information into a logging file on the server. Reading debug
ging messages from the log file is a bit less convenient than watch
ing them directly from a window as with the previous approach,
but using the log file does not require stopping and restarting the
server. There are two variations of this method: one that takes a
String
, and the other that takes a
String
and a
Throwable
(an
ancestor class of
Exception
). The exact location of the log file is
server specific, but it is generally clearly documented or can be
found in subdirectories of the server installation directory.
5. Look at the request data separately.
Servlets read data from the HTTP request, construct a response,
and send it back to the client. If something in the process goes
wrong, you want to discover if it is because the client is sending
the wrong data or because the servlet is processing it incorrectly.
The
EchoServer
class, shown in Section 16.12 (A Debugging
Web Server), lets you submit HTML forms and get a result that
shows you exactly how the data arrived at the server.
6. Look at the response data separately.
Once you look at the request data separately, you'll want to do
the same for the response data. The
WebClient
class, presented
next in Section 2.10 (WebClient: Talking to Web Servers Inter
actively), permits you to connect to the server interactively,
send custom HTTP request data, and see everything that comes
back, HTTP response headers and all.
7. Stop and restart the server.
Most full blown Web servers that support servlets have a desig
nated location for servlets that are under development. Servlets in
this location (e.g., the
servlets
directory for the Java Web
Server) are supposed to be automatically reloaded when their
associated class file changes. At times, however, some servers can
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
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