| By Boris Minkin | Article Rating: |
|
| January 7, 2006 01:15 PM EST | Reads: |
92,716 |
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
For our servlet to work, we'll create one more supporting class SampleProgram with a main method that can test all these supporting classes and will be eventually called by the servlet too.
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 was:<2>
at junit.framework.Assert.assertEquals(Assert.java:81)
at junit.framework.Assert.assertEquals(Assert.java:87)
Coding Web.xml Deployment Descriptor
Web applications require a Web.xml deployment descriptor (see Listing 10), which should be put under the WEB-INF directory.
In our deployment descriptor, we've included our SampleServlet and the index.html as a welcome page.
Deploying Servlet Using ANT
Eclipse 3.1 comes with built-in ANT support so you can execute XML-based scripts to deploy your application in a specific application server. In our case, we're deploying under Tomcat 5.0 server, which also has built-in support for Ant for the following tasks:
- Deploying WAR files
- Reloading WAR files
- Undeploying WAR files
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:
- Creating the WAR file
- Deploying the WAR file to Tomcat
- Reloading the application
- Undeploying the WAR file from Tomcat
- "build" - where to build a WAR file
- "path" - the context root of the Web application, in this case "myapp"
- "url" - the url of the Tomcat administrative application (which comes with Tomcat and provides an inter-face for deploying/undeploying applications)
- username - the user name for the Tomcat admin application (in our case it's "admin")
- password - the password for the Tomcat admin application (in our case it's "admin")
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.
Conclusion
In this article, I've shown you some of the new J2SE 5.0 elements and covered the creation of a Web application using simple tools available in Eclipse 3.1 and Tomcat 5.0, such as Java wizards, JUnit, and Ant. There are more advanced tools that can streamline and automate this development. One such extension to Eclipse is called Web Tools Project WTP (www.eclipse.org/Webtools), which I'll cover in a future article.
Published January 7, 2006 Reads 92,716
Copyright © 2006 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Boris Minkin
Boris Minkin is a Senior Technical Architect of a major financial corporation. He has more than 15 years of experience working in various areas of information technology and financial services. Boris is currently pursuing his Masters degree at Stevens Institute of Technology, New Jersey. His professional interests are in the Internet technology, service-oriented architecture, enterprise application architecture, multi-platform distributed applications, and relational database design. You can contact Boris at bm@panix.com.
![]() |
sumeet jaju 07/28/06 07:40:26 AM EDT | |||
Thanks for you |
||||
![]() |
SYS-CON Belgium News Desk 01/07/06 02:16:35 PM EST | |||
We'll build a servlet that will demo some new Java 5.0 features and do some basic tasks like creating a session to track user visits to multiple Web pages. This code can easily be extended to store a user ID in the session that will travel with her as the site is navigated. The value of the visit counter is stored in the session so multiple visits to the page will be counted (this includes the page refreshes after pressing the browser's 'Reload' button). If the counter goes over five, the counter gets cleared. |
||||
![]() |
SYS-CON Australia News Desk 01/06/06 12:59:13 PM EST | |||
We'll build a servlet that will demo some new Java 5.0 features and do some basic tasks like creating a session to track user visits to multiple Web pages. This code can easily be extended to store a user ID in the session that will travel with her as the site is navigated. The value of the visit counter is stored in the session so multiple visits to the page will be counted (this includes the page refreshes after pressing the browser's 'Reload' button). If the counter goes over five, the counter gets cleared. |
||||
![]() |
Lester D'Souza 01/06/06 12:24:47 PM EST | |||
A great article. I had to struggle to get it working due to some errors and typos. I wish I could upload my entire Eclipse project as part of the feedback. The nice thing about this set up was that I could modify my Servlet, then using the create-war, undeploy and deploy my changes on the tomcat server with the click of a button. Tomcat would detect my changes, and when I refreshed the browser, I could see my changes. This is an ideal setup for any web based development. Also I would like to mention that this is my first experience with Eclipse, Tomcat and Ant. I would like to rewrite this article and leave out the J2SE 5.0 stuff, since it does not fit in very well. Also I was able to use Junit to test the code. |
||||
![]() |
Eclipse News Desk 01/05/06 08:31:49 PM EST | |||
We'll build a servlet that will demo some new Java 5.0 features and do some basic tasks like creating a session to track user visits to multiple Web pages. This code can easily be extended to store a user ID in the session that will travel with her as the site is navigated. The value of the visit counter is stored in the session so multiple visits to the page will be counted (this includes the page refreshes after pressing the browser's 'Reload' button). If the counter goes over five, the counter gets cleared. |
||||
![]() |
SYS-CON Australia News Desk 01/05/06 07:38:14 PM EST | |||
We'll build a servlet that will demo some new Java 5.0 features and do some basic tasks like creating a session to track user visits to multiple Web pages. This code can easily be extended to store a user ID in the session that will travel with her as the site is navigated. The value of the visit counter is stored in the session so multiple visits to the page will be counted (this includes the page refreshes after pressing the browser's 'Reload' button). If the counter goes over five, the counter gets cleared. |
||||
![]() |
Carrie Latimer 01/05/06 07:01:44 PM EST | |||
I ran into the same issues Eric ran into. Help please. Thank you and take care. |
||||
![]() |
Carrie Latimer 01/05/06 07:01:34 PM EST | |||
I ran into the same issues Eric ran into. Help please. Thank you and take care. |
||||
![]() |
Eric Bresie 11/16/05 09:02:26 AM EST | |||
I was reading your article about Eclipse and Tomcat and was trying to follow on and ran into a snag or two. One problem that I did find was that the SampleProgram has a class name in the file of TestProgram. The other problem, and I'm not sure if this may not be an Eclipse project setup issue, environment issues, or something else, but when I try to compile the SampleProgram and reference the ForLoop's convertToString, it indicates that it cannot be resolved. I believe I have the classes included into the project correctly, so I am still a little confused. Since this is my first encounter with Java 5's new features such as the new forloops, I am not as familiar with this and was hoping you might have some idea. Do you have any ideas? Have you encountered a similar problem? Thanks ahead of time. Eric Bresie |
||||
![]() |
Servlet Newbie 11/10/05 12:22:30 PM EST | |||
Very good article! ...however: I cannot seem to get the servlet running. I can see that it is deployed, but if I type http://localhost:8080/myapp/SampleServlet/ Help please |
||||
![]() |
Eugenio Leal 11/01/05 07:47:57 PM EST | |||
Eclipse sucks. Websphere sucks. Netbeans Rules. |
||||
![]() |
Bernie's ramblings... 10/20/05 04:46:38 AM EDT | |||
Trackback Added: Eclipse, J2SE5 and Tomcat; I’ve always liked to read about Eclipse, and knowing more about it has often made me able to work better. There’s always just some gurus out there that seems to be able to make programming a bit more bearable. An article I came across today... |
||||
![]() |
Enterprise Open Source Magazine News Desk 10/17/05 03:31:03 PM EDT | |||
Enterprise Open Source Magazine - How to Bring Eclipse 3.1, J2SE 5.0, and Tomcat 5.0 Together |
||||
- Why SOA Needs Cloud Computing - Part 1
- Cloud Expo and The End of Tech Recession
- The Transition to Cloud Computing: What Does It Mean For You?
- A Rules Engine Built in PowerBuilder
- Sybase Named “Silver Sponsor” of iPhone Developer Summit
- How PowerBuilder Got Its Groove Back
- The Cloud Has Cross-Border Ambitions
- Ulitzer Names The World's 30 Most Influential Virtualization Bloggers
- Ulitzer Named "New Media" Partner of Greatly Anticipated iStrategy Event in Berlin
- Risks and Enterprise Mobility?
- Steps for Success in Enterprise Mobility?
- Are Mobile Luddites Resisting Mobility?
- The Difference Between Web Hosting and Cloud Computing
- Sybase CTO to Speak at 4th International Cloud Computing Expo
- Why SOA Needs Cloud Computing - Part 1
- Cloud Expo and The End of Tech Recession
- The Transition to Cloud Computing: What Does It Mean For You?
- Five Reasons to Choose a Private Cloud
- Seeding The Cloud: The Future of Data Management
- The Threat Behind the Firewall
- Economy Drives Adoption of Virtual Lab Technology
- Tips for Efficient PaaS Application Design
- A Rules Engine Built in PowerBuilder
- Sybase Named “Silver Sponsor” of iPhone Developer Summit
- Where Are RIA Technologies Headed in 2008?
- PowerBuilder History - How Did It Evolve?
- The Top 250 Players in the Cloud Computing Ecosystem
- Custom Common Dialogs Using SetWindowsHookEx
- DDDW Tips and Tricks
- OLE - Extending the Capabilities of PowerBuilder
- DataWindow.NET How To: Data Entry Form
- Book Excerpt: Sybase Adaptive Server Anywhere
- Sybase ASE 12.5 Performance and Tuning
- Working with SOA & Web Services in PowerBuilder
- Office 2003 Toolbar: A New Look For Your Old PowerBuilder App
- Dynamically Creating DataWindow Objects

































