How to Set Up Eclipse for Web-application Development

Russell Bateman
Spring 2010
last update:

Table of Contents

Introduction
Step by step
Eclipse preferences
Code format settings
Set up the JDK
Set up the server container
Set up JSF and JSTL libraries
Appendix A: web.xml
Appendix B: faces-config.xml

Introduction

This short tutorial should help you set up a new Eclipse workspace for web application development including a Tomcat 7 server and the Apach MyFaces libraries for JavaServer Faces development. You may ignore the JSF set-up in favor of JBoss RichFaces, Seam or any other framework you choose to follow.

Step by step

  1. Create a new workspace (unless you want to use the relevant parts of this step-by-step to convert an existing one). Launch Eclipse and choose or switch to this new workspace.
  2.  
  3. If you have workspace preferences saved (exported) from a previous workspace that you'd like to see respected in this new one, do that now before forgetting. (Or, you can do it later.)

    For example, you may like to set the width of your Java code to 120 and show a line in the editor window so that you don't go past it. This is done by

    1. Going to Window -> Preferences -> General -> Editors -> Text Editors.
    2. Clicking Show print margin and typing "120" into Print margin column.
    3. Tab width in spaces is also set there as is whether to show line numbers. Etc. There are many tailorings you may wish to pass from workspace to workspace.

    To export these, ...

    1. Go to File -> Export... -> General -> Preferences.
    2. Click Next, then Export all (or you can get clever, if you like).
    3. Name a preference file path, such as C:\Users\russ\dev\russ-eclipse.epf.
    4. Click Finish and ensure your preferences were put into the file you named.
  4.  
  5. If you have workspace preferences saved that govern how newly set-up code is automatically formatted by Eclipse, go to Window -> Preferences -> Java -> Code Style -> Formatter, click Import and then navigate to find the template you saved away after modifying the format. I obsess over my code format and save my settings to java-templat-formats.xml.
  6.  
  7. Establish the workspace JDK by choosing Window -> Preferences -> Java -> Installed JREs. What will be there already is the JRE installed on your computer host for executing Java programs. In my humble opinion, you don't want this; see Setting up the Sun Java Developer's Kit.

    To change it, click on the Location path, then click the Edit button. Navigate to where you've created a copy of Sun's Java Development Kit. For example, mine is at C:\Users\russ\dev\downloads\jdk1.6.0_20. While you're in the JRE Definition dialog, be sure to update the JRE name: so it's obvious that you're using your special one. The name can be anything you choose; I chose jdk1.6.0_20. You should see something like this:

  8.  
  9. Now establish your server container environment. This is what you will use to run and test your web applications. I'm choosing Tomcat 7 because I get a lot more "JEE" cooperation, specifically for JavaServer Faces work, out of that version (although Tomcat 6 works very well too); see Setting Up Apache Tomcat.

    Assuming you have this downloaded, unzippped and available, do this:

    1. Go to Window -> Preferences -> Server > Runtime Environments.
    2. Click Add, then select the name that most closely corresponds to your choice of environment. I'm choosing Apache Tomcat v7.0.
    3. Click Create a new local server to check. This will set up the server in your Project Explorer view that you will be using to run and test.
    4. Click Next.
    5. For the Tomcat installation directory:, click Browse and go find the server you downloaded earlier. Or, you can click the Download and Install... button (but I've not always been pleased with that option which installs it for your whole computer).
    6. Click Finish.
  10.  
  11. Finally, let's set up the JavaServer Faces (JSF) and JavaServer Pages Standard Tag Library (JSTL) for JSP work. If you're doing something different, you may not need or even want to set these up. Moreover, I make a choice here you may not want in that I'm choosing to use Apache MyFaces. Besides this option, there are many other implementations of Faces like Mojarra and RichFaces.

    The easiest way to set up these libraries is to create your first project. This can be a bogus project if you like. We'll delete it afterward.

    1. Right-click in the Project Explorer view and choose New. Find the Dynamic Web Project wizard; you may have to look under Other... -> Web.
    2. Name the project bogus.
    3. Set the Configuration to JavaServer Faces v1.2 Project.
    4. Click Next thrice (until you see JSF Capabilities.
    5. Click the Manage libraries... icon ().
    6. Use the User Libraries dialog to set up the two sets of JARs. This dialog is also available from the Build Path operations you will become acquainted with when using Eclipse.

    First, fetch the downloads we need.

    Follow these steps to set up the JSF and JSTL libraries:

    1. In the User Libraries dialog, click New... and type JSF 1.2 (Apache MyFaces) as the name. Click OK.
    2. While the new library is selected, fill it with JARs by clicking Add JARs..., then navigating to where you've downloaded Apache MyFaces.
    3. Select all the JAR files on the path myfaces-core-1.2.8-bin/lib and click Open. This will add them to the JSF library we created.
    4. Click New... again and create JSTL 1.2. Add the two JARs it will contain similarly to how you added those for the JSF library.

    You should now see something similar to the following:

    Dismiss the User Libraries dialog, then click to check both JSF and JSTL boxes to cause them to be added to the JSF capabilities for this workspace. Each time you set up project facets for a new Dynamic Web Project in Eclipse, you should automatically get these libraries.

Finished!

At this point, you're ready to create your first Dynamic Web Project in Eclipse. You can delete the bogus project from your workspace.

Appendix A: web.xml

The file web/WEB-INF/web.xml for most projects should appear thus. I have removed the <description> elements for brevity and enhanced the <welcome-file-list> over what Eclipse sometimes generates.


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns="http://java.sun.com/xml/ns/javaee"
		xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
		xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
		version="2.5">
  
    Faces Servlet
    javax.faces.webapp.FacesServlet
    1
  
  
    Faces Servlet
    *.faces
    /faces/*
  
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
    javax.servlet.jsp.jstl.fmt.localizationContext
    resources.application
  
  
    javax.faces.STATE_SAVING_METHOD
    client
  
  
    org.apache.myfaces.ALLOW_JAVASCRIPT
    true
  
  
    org.apache.myfaces.PRETTY_HTML
    true
  
  
    org.apache.myfaces.DETECT_JAVASCRIPT
    false
  
  
    org.apache.myfaces.AUTO_SCROLL
    true
  
  
    faces
    org.apache.myfaces.webapp.MyFacesServlet
    1
  
  
    faces
    *.jsf
  
  
    faces
    *.faces
  
  
    org.apache.myfaces.webapp.StartupServletContextListener
  
</web-app>

Appendix B: faces-config.xml

The file web/WEB-INF/faces-config.xml for most projects should appear' thus, usually with a different managed-bean name, a different to-view-id name, etc. This file is extremely variable and this is only a simple example of what to expect.


<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
	version="1.2">

   
      /index.jsp
      
         login
         /welcome.jsp
      
   

   
      user
      com.corejsf.UserBean
      session
   
</faces-config>