Thursday, November 12, 2009

Hello World Java Web Application in Eclipse (Part 1)

Want to integrate charts into your webapp? Check out XChart.

Java Web Application Structure in Eclipse


Download .war and full source code of this and other sample Java Web Applications here.

This is the first of a three part series of tutorials that shows how to create a Hello World Java Web Application in Eclipse. In this part, I demonstrate how to create your Java Web Application project in Eclipse and set up the directory structure of the project. In Part 2, I take you through getting the Java jar needed to code Java servlets, creating a Java Servlet and a JSP file, configuring the web.xml and build.xml files. In Part 3, I show how to build the project with ANT using the build.xml file and how to deploy the project to a Tomcat server running on your development computer. Along the way, I point out a few tips and tricks for using the Eclipse EE IDE.



Step 1: Install the Eclipse IDE for Java EE developers. Once installed, open up Eclipse and go to the Eclipse Workbench. I also recommend tweaking the default Eclipse development settings for a much cleaner and efficient coding experience.

Step 2: Create a new Java Project. Right-click somewhere in the Package Explorer View and select New--->Project... .


On page 1 of the Project Wizard, choose "Java Project".

On page 2 of the Project Wizard, give the project the name: "HelloWebApp".

Finish the wizard.



Step 3: Set up the directory structure of the web app project. For this I followed the recommendations outlined in the Tomcat documentation that comes with the Tomcat installation, except I chose to include a lib folder directly in the project to hold Java jars needed for the project. Create the nested directory structure as follows:

HelloWebApp (this is the project)
--src
----com.hello (this is actually a package, not a folder)
--lib
--web
----WEB-INF

The src folder holds all your Java source files including any Servlet classes, which you place in the com.hello package. The lib folder holds any 3rd party jars your project depends on. The web folder holds any .jsp or .html pages, Javascript files, and CSS files. The web folder also contains a folder named WEB_INF, which hold the project's web.xml file.

Here's what your new HelloWebApp Java project should now look like in the Eclipse Package Explorer View.



Piece of Cake!!

Download .war and full source code of this and other sample Java Web Applications here.
Hello World Java Web Application in Eclipse (Part 2)
Hello World Java Web Application in Eclipse (Part 3)

2 comments:

Francois said...

Hi very good tutorila, thanks for that.

A small typo here:
HelloWebApp (this is the project)
--src
----com.hello (this is actually a package, not a folder)
--lib
--web
----WEB_INF

it's WEB-INF and not WEB_INF.

Tim Molter said...

@Francois Good eye! Thanks, I fixed it.