How to Use Eclipse with the Web Tools Platform
    Main Page
    Lab Hardware
    Lab Software
 

In the instructions below, substitute your UW Net ID wherever you see "uwnetid".

Last updated: 23 Jan 2007

The development environment for web applications has improved considerably since 2003, when you basically had to do everything yourself. This is true of both the Microsoft Integrated Development Environment (IDE -- Visual Studio) as well as the Java IDEs.

With the release of Visual Studio 2005, Microsoft led the way with the tight integration of local web and database servers with the IDE. Sun's Netbeans environment allows add-ons which provide a visual means of specifying web applications, services, and service-oriented applications, but may not tightly integrate web and database servers. This can be problematic, as a much more complex development environment involving one or two servers must be maintained. The open-source Eclipse project's Web Tools Platform (WTP) does not bundle but allows connections to local web and database servers, allows debugging and provides a means of deploying applications to and debugging them on remote servers as well.

The configuration and operation of Eclipse with WTP for a simple web application is described here.

An important point here is that there are several dynamic web "servers" (i.e., tomcat servers) that are possible:

  1. First, there is the tomcat runtime environment, which is started and stopped within Eclipse as a "Server". It uses port 8080.

  2. The second is the tomcat server, which resides in C:\Program Files\Apache Software Foundation\Tomcat 5.5. It also uses port 8080 and would conflict with the Eclipse runtime version if it was started. However, it does not start automatically -- one starts it by entering the following from a command shell (quotes are necessary):
      "C:\Program Files\Apache Software Foundation\Tomcat 5.5\bin\tomcat5.exe"
      

  3. The third dynamic web server you can use is the one that you can start on repos.insttech.washington.edu once you log into your repos account. It uses a port other than 8080 -- one that is unique to your repos account. It is considered to be a remote service, relative to the computer on which you are running Eclipse. Since it is local to repos and students were developing on repos in the past, it was called the Local Tomcat Development Service. But when you are developing your web applications and services on another computer, it is a remote service to that computer.

All of these dynamic web servers require that the web application be deployed to them. In a business setting, this deployment often involves setting up some server-specific settings for resolving names and properties (e.g., for databases), but in most academic work deployment means simply to transfer all code, web pages and XML files to the server.

Deployment happens automatically for the built-in tomcat server in Eclipse. For the rest, you will have to do a little setup (the "second" server mentioned above) or use systems already set up (e.g., the "third" server) to prepare it to receive a deployed archive, create the web archive, and upload it to the server.

Installing the Tools

For instructions on installing most of the tools, see Setup Eclipse with WTP.

Using Eclipse with WTP

Develop a Simple Web Application in Eclipse

The database service is not needed for this application.

  1. Start Eclipse
  2. Establish your Tomcat Server runtimes (one time only):

    1. Select Window/Preferences/Server/Installed Runtimes
    2. Click on the "Add..." button, and select "Apache/Tomcat 5.5 Runtime"
    3. Click Next
    4. Click Browse and browse to: C:\Program Files\Apache Software Foundation\Tomcat 5.5
    5. Select the correct JRE (in the labs, jdk) and click OK.

    6. Create a new Dynamic Web project

      While not everything matches, you can use the information in this tutorial to cobble together a simple web application. This tutorial is based on it, but gets the IP address of the browser client -- the computer you are working on. There are better ways of getting this, but it shows an interaction with the browser and server. You may find this particular example useful when testing a network, once this web application is deployed to a remote server.

      1. Click on menu items File/New/Project...
      2. Expand Web
      3. Click on Dynamic Web Project
      4. Click Next
      5. Provide a project name.

        For ease of use in later examples, call your project: ClientIP

      6. Click Finish
      7. Agree to any Sun license agreement.
      8. Click on Yes to the question of Open associated perspective?
      9. Optionally close the Welcome tab to provide more room to work.
      10. Expand ClientIP in the Project Explorer.

    7. Define your servlet class:

      1. Create a new package

        • right click on Java Resources: src
        • select New/Package

        The package name can be anything you want it to be, but should conform to standard package naming if you are going to use it elsewhere. This is the reverse domain name of your organization (e.g., "insttech.washington.edu" is "edu.washington.insttech") followed by a unique name within your organization (suggestion: "edu.washington.insttech.uwnetid"). For example: edu.washington.insttech.janedoe could uniquely identify this project as Jane Doe's.

      2. Create a new Java class file

        • right click on the package name (e.g., edu.washington.insttech.janedoe)
        • select New/Class

        We are going to start exploiting the "Content Assist" feature of Eclipse, where Eclipse helps reduce the amount of typing you need to do and details you need to remember. It can be activated either automatically by typing a period, or manually by pressing Ctrl-Space, which we will abbreviate to ~CS from now on.

        To continue with the new file creation...

        • enter the file's Name:
          GetClientIP
                  
        • delete the old contents of the Superclass: field
        • enter the Superclass:
          javax.servlet.http.~CS
                  
          Don't forget that ~CS is just shorthand for Ctrl-Space

        • select HttpServlet from the presented choices with cursor, TAB and Enter keys, or simply double-click on it

        • click on Finish

      3. enter the body of the code -- the definition of a doGet() method -- by using Content Assist:

        1. put cursor inside the public class code body
        2. press ~CS
        3. select doGet()
        4. set attribute for "client.ip" (arbitrary name) to the client browser's remote address, as follows:
          • Note that doGet's arg0 is the request, and arg1 is the response.

            We don't change the names to something more meaningful to speed up the creation of this web application. Normally, you would just to make things clear.

          • inside doGet() body, delete:
            // TODO Auto-generated method stub
            super.doGet(arg0, arg1);
                       
          • enter:
            arg0.setA~CS
                       
          • select setAttribute()
          • change highlighted setAttribute arg0 to the literal string:
            "client.ip"
                       
          • press TAB to move to the next argument

          • change highlighted setAttribute arg1 to:
            arg0.getRem~CS
                       
          • select getRemoteAddress()
          • append a semicolon to end of line

        5. specify .jsp file to run

          • on the next line, enter:
            arg0.getReq~CS
                       
          • select getRequestDispatcher()
          • change getRequestDispatcher arg0 to:
            "/show.jsp"
                       
          • enter a period at end of line (after right paren)
          • enter (if necessary):
            ~CS
                       
          • select forward()
          • accept both args (arg0 and arg1) as is -- press TAB twice
          • enter a semicolon at end of line
      4. You should end up with code that looks like this (with your own value for the package name):
        package edu.washington.insttech.uwnetid;
        
        import java.io.IOException;
        
        import javax.servlet.ServletException;
        import javax.servlet.http.HttpServlet;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        
        public class GetClientIP extends HttpServlet {
        @Override
          protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
          	arg0.setAttribute("client.ip", arg0.getRemoteAddr());
        	arg0.getRequestDispatcher("/show.jsp").forward(arg0, arg1);
          }
        }
               

      5. save Java code

    8. change WebContent/WEB-INF/web.xml file to associate the servlet code (the GetClientIP.class file) with a name and a URL

      1. right-click on the web.xml file
      2. select Open With
      3. select Text Editor
      4. delete all lines from <welcome-file-list> to </welcome-file-list>, inclusive
      5. before the </web-app> line, add the following:
        <servlet>
          <servlet-name>Get Client IP</servlet-name>
          <servlet-class>edu.washington.insttech.uwnetid.GetClientIP</servlet-class>
        </servlet>
        
        <servlet-mapping>
          <servlet-name>Get Client IP</servlet-name>
          <url-pattern>/</url-pattern>
        </servlet-mapping>
               
      6. You should see the following in the final web.xml file, with your unique package name instead of the generic one here:
        <?xml version="1.0" encoding="UTF-8"?>
        <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
        	<display-name>ClientIP</display-name>
        	<servlet>
        		<servlet-name>Get Client IP</servlet-name>
        		<servlet-class>edu.washington.insttech.uwnetid.GetClientIP</servlet-class>
        	</servlet>
        	<servlet-mapping>
        		<servlet-name>Get Client IP</servlet-name>
        		<url-pattern>/</url-pattern>
        	</servlet-mapping>
        </web-app>
               
      7. save the web.xml file

    9. create the show.jsp code

      1. right-click on WebContent
      2. select New/JSP
      3. enter Filename:
        show.jsp
               
      4. click on Finish

      5. click in body of <body>
      6. enter:
        My IP: <%= request.getA~CS
               
      7. select getAttribute()
      8. enter as its argument the literal string:
        "client.ip"
               
      9. You should see this as the complete file - -if there is less, that can be okay; all that is really required is the html and body start and end tags, and the "My IP:" line.
        <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
            pageEncoding="ISO-8859-1"%>
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>
        </head>
        <body>
        My IP: <%= request.getAttribute("client.ip") %>
        </body>
        </html>
               
      10. save the show.jsp file

    10. select the server for the project

      1. right click on ClientIP project name
      2. select Run As
      3. select Run on Server
      4. select Tomcat 5.5 Server
      5. click on Finish

    11. verify the results

      • If all goes well:

        The built-in tomcat server will start, your web application will be deployed to it automatically, and the built-in web browser will automatically open to http://localhost:8080/ClientIP/, which sends the request to start the web application to tomcat. The web application executes, generating HTML code via show.jsp, and tomcat returns the HTML code to the browser, which renders it as:

        My IP: 127.0.0.1
             
      • If all does NOT go well (e.g., you get an HTTP error or see "null" as the IP address):

        You probably have made a mistake typing something. Carefully double-check your previous work, especially the strings that you entered, including the ones that are unique to you (e.g., the package name). Once you have fixed your problem, you must remember that the old web application is still deployed on the server, which is running.

        1. Remove your project from the server:

          • right-click on the server
          • select Add and Remove Projects
          • click on ClientIP
          • click on Remove

        2. Stop the server
        3. Follow the instructions in the select the server for the project" item above.
        4. Re-verify the results.

    Debug a Simple Web Application in Eclipse

    1. Set a breakpoint in the servlet (e.g., on arg0.setAttribute[...]).

    2. Start the Server in Debug mode -- it should restart, and show "Debugging" as its status.

    3. Run the application by entering the URL in the browser -- the browser should hang.

    4. Look at your Eclipse session -- it is probably prompting you to "Confirm Perspective Switch", as it changes from a "Java" perspective to a "Debugging" perspective.

    5. Look at variable values, step through code, and/or resume execution, all via the Debug menu item choices.

    6. Eventually, the browser will either return with the expected results, or if you stopped the debugging session, it may continue to hang until you click on the "Stop" button.

    Deploy or Debug a Simple Web Application to a Remote Server

    Concisely:

    1. To deploy using repos:

      • Start up the remote Tomcat service on repos with the debug option

    2. To debug using repos:

      • Shutdown any existing remote Tomcat service on repos

      • Start up the remote Tomcat service on repos with the debug option

    3. On your computer using Eclipse:

      • Make sure your application is deployed

      • Setup a Remote Java Application using repos.insttech.washington.edu and your debug port number, apply it and click on Debug

      • Set a breakpoint in your code

      • Use a browser or web explorer with your URL to invoke your servlet.

      • Handle the debugging perspective switch and debug your code.

    For more detailed instructions, follow this link to find out how to start your own copy of a remote tomcat service on repos.insttech.washington.edu, as well as how to deploy to it and debug an application running on that remote service from your workstation.

    Develop a Simple Web Service in Eclipse

    This section is under construction and has not been well tested.

    This section is adapted from a tutorial written by student Phil Bonderud and Developing Web Services "Eclipse Web Tools Project" by Boris Minkin.

    The database service is not needed for this application.

    1. Start Eclipse
    2. Establish your Tomcat Server runtimes (one time only):

      • Select Window/Preferences/Server/Installed Runtimes
      • Click on the "Add..." button, and select "Apache/Tomcat 5.5 Runtime"
      • Click Next
      • Click Browse and browse to: C:\Program Files\Apache Software Foundation\Tomcat 5.5
      • Select the correct JRE (in the labs, jdk) and click OK.

    3. Create a new Dynamic Web project

      The example code, instructions and explanations in this web page by Boris Minkin are very helpful:

      Follow his instructions to create the StockWeb project. It is simplest to download and un-jar the appropriate source code after copying them to C:\temp:

          cd /d C:\temp
          jar xvf StockWeb.war
          

      You will need to create a services package under Java Resources: src, and when his instructions tell you to import files (New/Other/General/File System), select them from the C:\temp\WEB-INF\classes\services directory into your services folder.

      When creating a web service, it is NOT available from the .class file's menu, but rather from the .java file (Java Resources: src/services).

      His "Figure 2", representing the specification of the "Web Service", has changed its appearance with our more recent version of Eclipse, but his explanations of old options are illuminating. They indicate how to create and test a web service and a client interacting with the service. Better instructions for our version are found below, but a good explanation of what is going on is in his article.

      1. Move the top slider up to Test Service from Start Service to test the service.

      2. When he mentions "Generate a Proxy", this is the same field as the current Client Type: Java Proxy.

      3. Move the second slider from the top from No client to Test client.

        This will cause a web services client to be created.

      4. Click Next to set up the "Web Service Java Bean Identity". If a "Windows Security Alert" window pops up, click OK.

      5. Click Next to set up the .wsdl file and configure the service.

      6. Start the server.

      7. The click Finish to publish and test the service.

    4. Define your servlet class (e.g. SnoopServlet) and JSP code (showBrowser.jsp), and update the web.xml content.

    5. Create a new local Server (New.../Other.../Server/Server)

      Add your project to the server.

    6. Start the local server, which publishes or deploys it. It should show "Started" as its status.

    7. Test the application.

      For example, let's say you decided to name your project "DynamicWebProject", and an alias for your servlet (in web.xml) is "snoop". You could reference this in a local browser as follows:

          http://localhost:8080/DynamicWebProject/snoop
          

      which should display information about the "user-agent" if successful.

    Debug a Simple Web Service in Eclipse

    This section is under construction and has not been well tested.

    1. Set a breakpoint in the servlet (e.g., on if( userAgent != null )).

    2. Start the Server in Debug mode -- it should restart, and show "Debugging" as its status.

    3. Run the application by entering the URL in the browser -- the browser should hang.

    4. Look at your Eclipse session -- it is probably prompting you to "Confirm Perspective Switch", as it changes from a "Java" perspective to a "Debugging" perspective.

    5. Look at variable values, step through code, and/or resume execution, all via the Debug menu item choices.

    6. Eventually, the browser will either return with the expected results, or if you stopped the debugging session, it may continue to hang until you click on the "Stop" button.

    Deploy or Debug a Simple Web Service to a Remote Server

    Concisely:

    1. To deploy using repos:

      • Start up the remote Tomcat service on repos with the debug option

    2. To debug using repos:

      • Shutdown any existing remote Tomcat service on repos

      • Start up the remote Tomcat service on repos with the debug option

    3. On your computer using Eclipse:

      • Make sure your application is deployed

      • Setup a Remote Java Application using repos.insttech.washington.edu and your debug port number, apply it and click on Debug

      • Set a breakpoint in your code

      • Use a browser or web explorer with your URL to invoke your servlet.

      • Handle the debugging perspective switch and debug your code.

    For more detailed instructions, follow this link to find out how to start your own copy of a remote tomcat service on repos.insttech.washington.edu, as well as how to deploy to it and debug an application running on that remote service from your workstation.

    Change Log

    23 Jan 2007 Minor editorial correction to final version of web.xml file.
    18 Jan 2007 Made corrections and added confirmation of correct work based on the first ITW201 workshop.
    16 Jan 2007 Revised simple web application, to make it simpler to enter and understand; Moved MySQL section and Install sections to their own pages
    27 Dec 2006 Added simple web service example, in part
    22 Dec 2006 Added information about remote tomcat shutdown and "startup debug" to debug; Clarified and expanded section on mysql administration and database user creation
    14 Dec 2006 Original document


    Hours  |  Support Information  |  News  | 
    Policies  |  Emergencies