Tutorials : The Power of Three - Eclipse, Tomcat, and Struts :

 

Debugging servlets

To show you how debugging a servlet works, we'll create a very simple servlet:

A single breakpoint is already established. Before debugging can start we'll have to define the servlet to Tomcat. This can be done by creating a web.xml file and placing it in the WEB-INF folder:

<!DOCTYPE web-app 
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
  "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

  <servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>HelloWorldServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>

</web-app>

The servlet can now be started with this URL:

http://localhost:8080/Tom1/hello

Another possibility is to define a generic rule for all servlets in Tomcat. This can be done in the web.xml file in Tomcat's conf directory. Remove the comments around the invoker servlet definitions:

<servlet>
  <servlet-name>invoker</servlet-name>
  <servlet-class>org.apache.catalina.servlets.InvokerServlet</servlet-class>
  <init-param>
    <param-name>debug</param-name>
    <param-value>0</param-value>
  </init-param>
  <load-on-startup>2</load-on-startup>
</servlet>

...and:

<servlet-mapping>
  <servlet-name>invoker</servlet-name>
  <url-pattern>/servlet/*</url-pattern>
</servlet-mapping>

You'll have to restart Tomcat if you do this modification. The "Hello World" application can now be started like this:

http://localhost:8080/Tom1/servlet/HelloWorldServlet

Having set the breakpoint above Eclipse will switch to the debug perspective and show the breakpoint:

In debug mode Eclipse offers many features, for example the possibility to inspect or even change the values of variables and objects. More information can be found in the Eclipse Help section: "Java Development User Guide".

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.