How to Bring Eclipse 3.1, J2SE 5.0, and Tomcat 5.0 Together
Eclipse is the most popular Open Source IDE on the Java market and the latest 3.1 release supports all the new language elements of J2SE 5.0.
In this article I'll show you see how to create a Web project that has Java classes located in different packages and how to use ANT to build this project and JUnit to test it. I assume that you have J2SE 5.0 installed and are familiar with Ant and JUnit.
Building Java 5.0 Applications with Eclipse
Creating a New Project in Eclipse
Since the standard J2SE 5.0 SDK doesn't support servlets, let's add the servlet.jar that comes with Tomcat. It's in its directory common\lib\servlet-api.jar. To add this external jar to an Eclipse project, right-click on MyJavaProject, select properties, Java Build Path, and under the Libraries tab add this external jar. Now our servlets will compile.
Eclipse by itself doesn't support debugging with Tomcat unless you add some custom Web development plug-ins to it, such as Web Tools Project (WTP). It's possible to debug Tomcat applications using Eclipse remote debugging capabilities. For more information see the Java Developer's Journal at http://java.sys-con.com/read/44918.htm.
Some of the New J2SE 5.0 Features
Just press Ctrl-S and the class is saved and compiled. As you can see, it has three methods for boxing, unboxing, and simplified adding to a collection.
Eclipse can handle both generic and non-generic types:
Enhanced For Loop
Java printf Function
Methods with Variable Number of Arguments (varargs)
Don't forget to create this class in the package support.
Creating a Servlet
Then, we can just create a SampleServlet class by selecting HttpServlet as a superclass in the Eclipse class creation window.
Servlets can handle all the HTTP protocol invocation types. GET and POST are the most common ones, through. Servlet code is in Listing 7.
As one can see from the code, the servlet first tries to create an Http session by calling request.getSession(true), which means that a new session will be created if one doesn't exist already, then we'll get the "sessiontest.counter" attribute from the session, which will be assigned 0 if it's null, increment it, and set it back to the session. The session will be invalidated (all attributes removed from it) when counter goes above five. We'll get an HTTP request header called "Cookie" and a reference to the HTTP response writer and pass them to the SampleProgram class (see Listing 8) that will perform the logic described below.
Creating a Test Class and JUnit Test Case
As you can see, the program in Listing 8 uses all our classes. It can also be tested from the command line (without having to deploy a servlet in Tomcat) by creating a JUnit test case.
To create a JUnit test case in Eclipse, right-click on the class SampleProgram and select the menus New, Other, Junit, and Junit Test Case. It'll add the junit.jar to the classpath, if needed, and a window pops up asking how we want to create our test case.
Select setUp() and tearDown() methods to set up the test environment and tear it down when finished. Also select create main method and allow Swing UI so we can run our test visually. On the next screen select methods to test: main and testClasses.
Once finished, a SampleProgramTest class is generated and we can test it by selecting Run As - JUnit test from the context menu. JUnit will test to see if any exceptions occur and display them as errors. It will also display failures if any of our assertion tests fail. Below is the source code of our sample JUnit test (see Listing 9).
In both test methods, we've added method calls with arguments to make sure there are no exceptions. We also do an assertion that will definitely fail to demonstrate the JUnit failure detection capability:
junit.framework.ComparisonFailure: expected:<1> but
Coding Web.xml Deployment Descriptor
In our deployment descriptor, we've included our SampleServlet and the index.html as a welcome page.
Deploying Servlet Using ANT
To take advantage of this support, we have to add Eclipse to the external catalina-ant.jar (which is located under the $TOMCAT\server\lib directory and contains support for Tomcat Ant tasks) to the runtime class-path before running Ant tasks. Select the menus Run, External Tools, and External Tools and double-click on the Ant-build.
Running Ant tasks is simple in Eclipse. Just right-click on your build file (you have to create a build.xml file containing your Ant tasks as in Listing 11) and select Run As - Ant Build, then select tasks to create-a war file, and deploy the application.
As you can see from Listing 11, there are four major tasks in the build:
Using the Eclipse interface (right-click on build.xml file and select Run As - Ant Build...), these tasks can be executed one at a time or all together. Note that "Deploying the WAR file to the Tomcat" task is dependent on "Creating WAR file" task, so execute the "create-war" task first, then "deploy" task.
It's time for us to start the Tomcat server and deploy our application. In Windows, simply go to Control Panel - Services and start the Apache Tomcat service. In Unix, you can execute the $TOMCAT\bin\startup.sh script.
Once it's deployed (the ANT deployment task has been completed), just point your browser at http://localhost:8080/myapp/SampleServlet/ and you should see the output as in Figure 1.
© 2005 SYS-CON Media Inc.
Questions or problems regarding this web site should be directed to email@example.com.
Copyright © 2008 Art Beckman. All rights reserved.
Last Modified: March 9, 2008