Java
  Home arrow Java arrow Page 3 - Debugging Servlets
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Debugging Servlets
By: Murach Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2006-09-28

    Table of Contents:
  • Debugging Servlets
  • How to print debugging data to the console
  • How to write debugging data to a log file
  • Perspective

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Debugging Servlets - How to write debugging data to a log file


    (Page 3 of 4 )

    If you can’t print debugging data to the console, you can print debugging data to a log file as shown in figure 5-11. Although each servlet engine uses log files a little differently, you should be able to use these log methods with any servlet engine. However, you may need to check the documentation for your servlet engine to see how it works with log files.

    To write data to a log file, you can use the two log methods of the HttpServlet class. If you just want to write a message to a log file, you can use the first log method. But if you want to write a message to the log file along with the stack trace for an exception, you can use the second log method. A stack trace is a series of messages that presents the chain of method calls that precede the current method.

    The code in this figure uses the first log method to display the value for the emailAddress variable of the EmailServlet class. Then, it uses the second log method to print a message and a stack trace for an IOException. The data that’s printed by these two log methods is shown in the TextPad window.

    Tomcat 4.0 stores all log files in its logs directory. Within this directory, Tomcat stores several types of log files with one file of each type for each date. In the TextPad window, you can see the contents of the localhost_log.2002-10-15.txt file. This log file contains the value of the emailAddress variable as well as the stack trace for an IOException.

    Often, the servlet engine automatically writes other information in the same log file that the log methods of the HttpServlet class use. In that case, you can write your debugging information to a separate text file to make it easier to view your debugging messages. To do that, you can create your own class that writes error messages to a file.

    To illustrate, the CD that comes with this book includes a LogUtil class in the util package that contains log methods that work like the log methods shown in this figure. However, you can easily modify this class to specify a name and location for the log file. Then, you can use the log methods in this class to write your debugging information to the file.

    Two methods of the HttpServlet class used to log errors

    MethodMethod

    Description

    log(String message)

    Writes the specified message to the server’s error log.

    log(String message, Throwable t)

    Writes the specified message and stack trace for the exception to the server’s error log.

    Servlet code that prints data to a server specific log file

      String emailAddress = request.getParameter("emailAddress");
      log("EmailServlet emailAddress: " + emailAddress);
      User user = new User(firstName, lastName, emailAddress);
      try{
         
    UserIO.addRecord(user, file);
      }
      catch(IOException ioe){
          log("EmailServlet IOException in UserIO", ioe);
      }

    The location of log files in Tomcat 4.0

      \tomcat\logs

    A typical log file


    Figure 5-11.  How to write debugging data to a log file

    Description

    1. You can use the log methods of the HttpServlet class to write debugging information to a log file. Tomcat 4.0 stores its log files in a directory named logs.
    2. The name and location of the log files for a servlet engine may vary depending on the servlet engine. To find the name and location of your log files, check the documentation for your servlet engine.
    3. A stack trace is the chain of method calls for any statement that calls a method.

    More Java Articles
    More By Murach Publishing


       · This article is an excerpt from the book "Murach's Java Servlets and JSP," published...
     

    Buy this book now. 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.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek