How do I create a utility project that multiple projects can share?
Author: Deron Eriksson
Description: This Java tutorial describes how to debug a utility project from another project in Eclipse.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1 || Tomcat 5.5.20


Page: < 1 2

(Continued from page 1)

Now I go to the Debug perspective and fire up my tomcat-demo debug configuration. (In my Tomcat's server.xml file I have a Context entry for tomcat-demo, as described in the "How do i debug my web project in TomcatSW from EclipseSW without plug-ins?" tutorial.)

'tomcat-demo' Debug Configuration started

In 'tomcat-demo', I now modify the TestServlet performTask method to instantiate a UtilityDemo class and call getMessage() on that class. The TestServlet class is able to find the UtilityDemo class because the utility-demo project is in the classpathW of the tomcat-demo project.

Added UtilityDemo code to TestServlet

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;

import demo.UtilityDemo;

 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");
		out.println("<br/>");
		UtilityDemo utilityDemo = new UtilityDemo();
		out.println(utilityDemo.getMessage());
	}
	
}

Now I can hit the TestServlet in a browser window. TestServlet is mapped to /test. As you can see, we are able to call classes in our utility project from our web application project.

Hitting TestServlet in browser

So what's the big point of this, you may ask? The nice thing about this is that you can now debug your utility classes from within your web application project on the fly. As an example, let's change the message that UtilityDemo's getMessage() method returns and save the file.

Changing UtilityDemo message

Now let's refresh the browser window. Our changes automatically appear! Our debug configuration didn't need to restart, or anything else like that. No redeployment to Tomcat or anything like that is necessary.

Hitting TestServlet in browser again

When it's time to deploy your web application project (ie, turn it into a warW file or earW file), make a jarW file out of your utility project and include it in your web app's /lib directory.

In more that one technical class that I've been to, the instructors have taught the class by firing up a servletW container, had us change code, and then build a war file and deploy it to the servlet container in order to test! As you can imagine, this is really a terrible way to code, since you basically need to do a deployment every time you change your code to see if it actually works, and you can't really debug unless you do remote debugging. Firing up a servlet container via Eclipse where you can debug your project's code (set breakpoints, step through, step in, step out, etc) is a much more practical, fast, useful way of coding. In this lesson, we've also seen how we can debug classes in a utility project from our web application project.

Page: < 1 2