Java
  Home arrow Java arrow Page 4 - 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 - Perspective


    (Page 4 of 4 )

    The goal of this chapter has been to teach you the basics of coding, saving, and testing servlets. So at this point, you should be able to develop simple, but practical, servlets of your own. In addition, you should have a basic understanding of how servlets are executed.

    Note, however, that you usually don’t use servlets to send the HTML code back to the browser as shown in this chapter. Instead, you structure your web applications so servlets do the processing that’s required and JSPs send the HTML code back to the browser. In that way, you combine the best features of servlets with the best features of JSPs, and that’s what you’ll learn how to do in the next chapter.

    Summary

    --------------------------------------

    • A servlet is a Java class that runs on a server, and a servlet for a web application extends the HttpServlet class.
    • When you write servlets, you override the doGet and doPost methods to provide the processing that’s required. These methods receive the request object and the response object that are passed to them by the server.
    • After you use the setContentType method of the response object to set the content type of the response that’s returned to the browser, you use the getWriter method to create a PrintWriter object. Then, you can use the println and print methods of that object to send HTML back to the browser.
    • The class files for servlets must be stored in the WEB-INF\classes directory of an application or in a subdirectory that corresponds to a package name.
    • To request a servlet from a URL, you include the word “servlet” after the document root directory in the path. This is followed by the package name, a dot, and the servlet name.
    • You can override the init method of a servlet to initialize its instance variables. These variables are then available to all of the threads that are spawned for the one instance of the servlet.
    • When you code a thread-safe servlet, you prevent two or more users from accessing the same block of code at the same time.
    • To print debugging data to the server console, you can use the println method of the System.out or System.err object. An alternative is to use the log methods of the HttpServlet class to write debugging data to a log file.

    Terms

    --------------------------------------
      servlet
      request object
      response object
      content type
      instance variable
      thread-safe
      log file
      stack trace

    Objectives

    --------------------------------------

    • Code and test servlets that require any of the features presented in this chapter.
    • Provide debugging data for a servlet by writing messages to either the console or a log file.
    • Describe the directory structure that must be used for servlet classes.
    • Describe the difference between the URL for a JSP and the URL for a servlet.
    • Describe the use of the init, doGet, and doPost methods in a servlet.
    • Describe the execution of a servlet, and explain its effect on local and instance variables.
    • Explain what is meant by a thread-safe servlet and describe what you have to do to develop one.

    Exercise 5-1 Modify the EmailServlet class

    1. Enter a URL in your browser that requests the EmailServlet class that’s located in the murach/WEB-INF/classes/email5 directory and sends two parameters to it.
    2. Run the HTML document named join_email_list.html in the email5 directory so it accesses and runs the EmailServlet class.
    3. Add the accessCount instance variable to the EmailServlet class as described in figure 5-7. Then, run the servlet from the HTML document. This will test whether servlet reloading has been turned on as described in chapter 2. If it isn’t on, you will have to stop and restart Tomcat before the changes to the servlet show up. (You may have to refresh your browser for the changes to take effect.)
    4. Modify the HTML document so it uses the Post method instead of the Get method, and modify the servlet so it works properly.
    5. Print a debugging message to the console that shows the value of the accessCount variable. Then, run the servlet two or more times to see how this message appears in the console.
    6. Repeat step 5, but use a log file this time.

    Exercise 5-2 Create a new servlet

    In this exercise, you’ll modify the HTML document for the Email List application, and you’ll create a new servlet that responds to the HTML document. This is comparable to what you did for exercise 4-2, but the details are repeated here.

    1. Modify the HTML document named join_email_list.html that’s in the email5 directory so it has this line of text after the Email address box: “I’m interested in these types of music.” Then, follow this line with a list box that has options for Rock, Country, Bluegrass, and Folk music. This list box should be followed by the Submit button, and the Submit button should link to a new servlet named MusicChoicesServlet.
    2. Create a new servlet named MusicChoicesServlet that responds to the changed HTML document. This servlet should respond with an H1 line that looks like this:

      Thanks for joining our email list, John Smith.

      And this line should be followed by text that looks like this:

      We’ll use email to notify you whenever we have new releases for
      these types of music:

      Country
      Bluegrass

      In other words, you list the types of music that correspond to the items that are selected in the list box. And the entire web page consists of just the heading and text lines that I’ve just described. 
    3. Test the HTML document and the new servlet by running them. Note the parameter list that is passed to the servlet by the HTML document. Then, test the new servlet by using a URL that includes a parameter list.

    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek