How do I debug my web project in Tomcat from Eclipse?
Author: Deron Eriksson
Description: This Java tutorial describes how to debug a web project in Eclipse without using plug-ins.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1 || Tomcat 5.5.20


Page: < 1 2 3 4 >

(Continued from page 2)

Now let's create some files for our project.

'tomcat-demo' project

Add the following directories and files:

Adding files and directories to 'tomcat-demo' project

TestServlet.java

package test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 public class TestServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

	private static final long serialVersionUID = 1L;

	public TestServlet() {
		super();
	}   	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		performTask(request, response);
	}  	

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		performTask(request, response);
	}   
	
	private void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("TestServlet says hi");
	}
	
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="tomcat-demo" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<servlet>
		<servlet-name>TestServlet</servlet-name>
		<servlet-class>test.TestServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>TestServlet</servlet-name>
		<url-pattern>/test</url-pattern>
	</servlet-mapping>
</web-app>

test.html

this is test.html

So far, our project knows about TomcatSW (via the CATALINA_HOME jarW files we added to the classpath). Also, we have our TestServlet class set up, and we have it mapped in in our web.xmlW file, which is the 'deployment descriptor' file that describes our project.

Next we need to tell Tomcat about our 'tomcat-demo' project by updating Tomcat's server.xml with 'context' data about our project. The server.xml file is located on my machine at C:\apache-tomcat-5.5.20\conf\server.xml. However, in another tutorial, I created a shortcut link to the tomcatSW server.xml file so that it can be opened and modified via Eclipe's XMLW Editor. As a result, I can go into the _shortcuts project and double-click the 'tomcat 5.5.20 server.xml' file link, which opens up the Tomcat server.xml file within EclipseSW.

Nested within the <Host> tags, you need to create a <Context> entry in server.xml. For this demo, I use the following:

      <Context docBase="C:\projects\workspace\tomcat-demo\web" 
      	path="/tomcat-demo" reloadable="true"/>

The docBase attribute specifies the document base of our web application. This tells Tomcat where our web application is sitting in the file system. The path attribute indicates the path in a client's browser that needs to be used to get to the web application.

Adding Context entry to Tomcat's server.xml

(Continued on page 4)

Page: < 1 2 3 4 >