Web Projects in Eclipse
— print (last updated: Nov 19, 2009) print

Select font size:

Eclipse and Web Tools

On Linux-based systems, eclipse is usually available as a package, but I generally favor getting the latest release version directly from the Eclipse home page:
http://www.eclipse.org/
I tend to favor going "small," but Eclipse's idea of small is still pretty big. The distribution:
Eclipse IDE for Java Developers
is the right idea in terms of our needs. Recent version of this package (of size 92MB) are available from the Computer Science server:
eclipse-java-galileo-SR1-win32.zip Windows
eclipse-java-galileo-SR1-linux-gtk.tar.gz Linux (32-bit)
eclipse-java-galileo-SR1-macosx-cocoa-x86_64.tar.gz Mac (64-bit)
Eclipse likes to name its major releases instead of numbering them — e.g., "europa" = 3.3, "ganymede" = 3.4, "galileo" = 3.5; get it (I don't) ? — and this is version is 3.5.1.

Web Tools

In order to create Web applications, we need additional support in Eclipse. The premier Eclipse software tool which provides this functionality is Web Tools Platform (WTP) which has a life of its own at
http://www.eclipse.org/webtools/
The WTP requires minimally 4 packages which get installed along with Eclipse. The version of WTP which corresponds to Eclipse 3.5.1 is WTP 3.1.1. These are the relevant packages (available on the Computer Science server):
emf-runtime-2.5.0.zip
xsd-runtime-2.5.0.zip
GEF-SDK-3.5.1.zip
wtp-R-3.1.1-20090917225226.zip
If you have a different version of Eclipse, presumably you should try to track down the WTP version which corresponds to your Eclipse version.

Installation

    Windows   Linux   Mac    

Windows

Decide on an installation folder, say:
My Documents\
Keep in mind that all the packages will add the initial eclipse directory at the top. First extract the basis into My Documents
eclipse-java-galileo-SR1-win32.zip
Then, one by one, extract the WTP zip folders, also into My Documents (NOT into My Documents\eclipse !!). Say YES to ALL for any file overrides — these overrides are innocuous.
emf-runtime-2.5.0.zip
xsd-runtime-2.5.0.zip
GEF-SDK-3.5.1.zip
wtp-R-3.1.1-20090917225226.zip
Finally, the executable you want is:
My Documents\eclipse\eclipse.exe
Simply make a shortcut to it in some convenient location.

Sample Web Application

We need the Apache Tomcat web server. Refer to the discussion in the Tomcat/Servlets/JSP with NetBeans about installing Tomcat. Unlike NetBeans, tomcat uses its own private configuration files for running tomcat, and needs no configuration of tomcat, per se.
  1. Open Eclipse. Decide on the default "workspace directory" and go ahead. It opens in the so-called Java perspective.
  2. Open the J2EE perspective by going through Window Open Perspective Other
  3. Right-click in the Project Explorer window. Select New Other. Open the Server category, select the Server entry within and click Next.
  4. In the Define a New Server window, select Apache Tomcat v6.0 Server, click Next.
  5. In the Tomcat Server window, click the Browse button to navigate to your Tomcat installation directory. Click Finish.
Back in Eclipse, look for the Servers tab at the bottom. you should see the Tomcat 6.0 server at localhost entry listed in there.

Dealing with port conflicts

If necessary, change the Tomcat port number to avoid conflicts with other services as we did in. Tomcat/Servlets/JSP with NetBeans. Double-click on the Tomcat entry in the Servers tab to reveal the configuration listing. Shift to the right to find these settings:
Tomcat admin port  8005
HTTP/1.1           8080
AJP/1.3            8090
For example, make the number these:
Tomcat admin port  8105
HTTP/1.1           8180
AJP/1.3            8190
Save the configuration and close this configurator.

Create and run the sample application

  1. Select New Project Web Dynamic Web Project; click Next. Set HelloWorld as the Project Name; click Finish.
  2. Right-click on the HelloWorld project and select New JSP. Give it the name index.jsp (the .jsp extension is optional, and typing it will not confuse the file name like in NetBeans). Click Finish.
  3. Type "Hello Eclipse" in the document's body and save it.
  4. Run this web application (with only one file) by clicking the green run button, or by right-clicking on the project and selecting Run As Run on Server.
  5. A Run On Server popup queries you for the server to use. Check the Always use this server when running this project checkbox and click Finish. The tomcat server should start and you should see the JSP page displayed in Eclipse's internal browser. If the server output fails, go to the Servers window, right-click on the Tomcat server entry and select Restart.
  6. Check the output in an external browser (like Firefox): Enter the URL by hand (assuming the default port 8080):
  7. Right-click on the HelloWorld project and select New Servlet. Give it the info and then click Finish:
    Java package:  servlet
    Class name:    Hello
    
  8. Add the following code to the Servlet's doGet method:
    java.io.PrintWriter out = response.getWriter();
    try {
      out.println("Hello Servlet");
    } finally {
      out.close();
    }
    
  9. Right-click on the Hello servlet from the Project window and select Run Run on Server.

Comparison with NetBeans

Eclipse structures its web applications in a manner similar to NetBeans, but there are some important differences.
  1. Web files: Eclipse's web files directory is WebContent and it uses this same name in the Project window. NetBeans' directory is web and it refers to this folder as Web Pages.
  2. Java source files: Eclipse's source file directory is src which it refers to as "Java Resources: src" in the project window. NetBeans' directory is src/java which it refers to as "Source Packages".
  3. Libraries: NetBeans has a stock of Library JAR files which are readily useable. In Eclipse you must either create Libraries or else put the necessary JAR files in the WEB-INF/lib folder.
  4. Servlets: NetBeans adds more structure to its the servlet skeleton. In Eclipse there is no
    processRequest
    
    function which both doGet and doPost are set up to call; you have to create one yourself. Also NetBeans automatically defines the PrintWriter out object wherease Eclipse does not.
  5. Content refresh: NetBeans automatically refreshes the content of its Project folder by virtue of any files which were added externally (i.e., not through the application). With Eclipse, you must explicitly Refresh the project to pick up externally added files.


© Robert M. Kline