Develop Web Applications Using Eclipse - Manual Build
by Dr. Wenjie He
Eclipse IDE (Integrated Development Environment) is a very popular development tool for Java projects.
In this class, we are going to use Eclipse as our main IDE for web application developement. We will use
two versions of Eclipse -
Standard and
Java EE versions. At the beginning, we only use the Eclipse Standard
version.
Getting Started with Eclipse
1. Eclipse Installation
- Go to the Eclipse download page
http://www.eclipse.org/downloads/. Select the version
Eclipse IDE for Java Developers (85 MB) to download the file
eclipse-java-ganymede-win32.zip.
- Unzip the file eclipse-java-ganymede-win32.zip and extract to the directory
C:\JavaEE\IDEs. Then our Eclipse installation directory is
C:\JavaEE\IDEs\eclipse.
- Create a shortcut on your desktop.
Go to the Eclipse installation directory C:\JavaEE\IDEs\eclipse.
Right-click the Eclipse icon
, and select Create Shortcut.
A shortcut is created. Drag the shortcut to your desktop, and change the name if you want.
Watch the video clip (play
) for the whole procedure.
- Create a folder called workspace inside your Eclipse installation directory to
hold your Eclipse projects. Here our workspace path is C:\JavaEE\IDEs\eclipse\workspace.
2. Starting Eclipse
- Double-click the Eclipse icon on your desktop. The first time you start the Eclipse,
the Workspace Launcher window opens for you to select your default workspace.
- Click the Browse button and navigate to the location of the workspace you choose.
Here we choose C:\JavaEE\IDEs\eclipse\workspace.
- When the Eclipse window opens for the first time, the Welcome window displays.
- Shutdown the Welcome window, then the default Java Perspective shows up.
If you want to bring the Welcome window back later, you can go to the Help menu,
and select Welcome.
3. Creating A Java Project
Note: This is a regular Java project. It is not a web application project, which means that
it does not contain any configuration information. We use this project to compile the servlets
for our
Duke2 (the servlet version of
Duke).
- Select File ==> New ==> Project... The New Java Project window opens.
-
Type Duke2 as the project name. Accept other settings, and click Next.
A window for more Java settings opens.
- Click Finish. A new Java project is created in Eclipse.
4. Preparing for Project
- Download the Duke2src.zip
file and unzip it in some directory, so that our source files are in a directory like ...\Duke2src.
- Create a new package called servlets in Eclipse that matches the package used in the
given source servlet files.
Right-click the project Duke2 node and select New ==> Package.
The New Java Package window opens. Type servlets for the Name,
and click Finish.
5. Adding Library JAR Files
Any Java servlet needs to extend HttpServlet class, which is not a Java SE class. It is a Java EE
class. So we need to import a JAR library file that contains all the classes for servlets.
We can find this class C:\JavaEE\Servers\Tomcat\apache-tomcat-5.5.26\common\lib\servlet-api.jar
in Tomcat.
- Right-click the project Duke2 node and select Properties at the bottom.
The Properties window opens. Select Java Build Path in the left pane.
- Click the Libraries tab, and click the Add External JARs... button.
Navigate to the directory C:\JavaEE\Servers\Tomcat\apache-tomcat-5.5.26\common\lib
and select servlet-api.jar file.
- Click OK. The library is added into the project.
6. Copying and Building Source Files
- Copy GreetingServlet.java and ResponseServlet.java from the
directory ...\Duke2src\servlets to the package servlets of the
project Duke2 directory. This step can be done by the drag-and-drop feature
of Eclipse.
See the video clip (play
) for the step.
- Since Build Automatically is set in Eclipse by default, our Java files are already compiled.
Look at the folder C:\JavaEE\IDEs\eclipse\workspace\Duke2\bin\servlets. Two Java
.class files are already there.
Note: The JavaBeans components are not web components.
7. Assembling Web Application
- Create a folder called Duke2 in a suitable location. I refer it as ...\Duke2.
- Copy the files duke.waving.gif and index.jsp from
...\Duke2Src to ...\Duke2.
- Create a subfolder WEB-INF under the ...\Duke2, and copy the
file web.xml from ...\Duke2src to ...\Duke2\WEB-INF.
- Create a subfolder classes under the ...\Duke2\WEB-INF, and copy the
folder C:\JavaEE\IDEs\eclipse\workspace\Duke2\bin\servlets that contains the compiled servlet
class files to ...\Duke2\WEB-INF\classes. See the folder structure below.
File structure of
Duke2
Duke2
Duke2\index.jsp
Duke2\duke.waving.gif
Duke2\WEB-INF
Duke2\WEB-INF\web.xml
Duke2\WEB-INF\classes
Duke2\WEB-INF\classes\servlets
Duke2\WEB-INF\classes\servlets\GreetingServlet.class
Duke2\WEB-INF\classes\servlets\ResponseServlet.class
8. Deploying and Running Duke2
- Copy the folder Duke2 into the folder C:\JavaEE\Servers\Tomcat\apache-tomcat-5.5.26\webapps.
- Start the Tomcat.
- Visit the URL http://localhost:8080/Duke2
You will see the
Duke2 example in the servlet version.
Exercise
Problem:
Create a web application that uses the source files in
SessionServlet.zip
to create a web component. Then you run it in Tomcat and see the result.
Requirements:
- You need to create your own configuration file
web.xml
.
- You need to configure the welcome file of your web application in the
web.xml
.
- You need to configure the servlet of your web application in the
web.xml
.
- You need to create a folder for your web application with a file structure that satisfies the
servlet specification.
==========The End==========