Eclipse For Java Web Development

How to Set Up Eclipse for Web-application Development

Russell Bateman
Spring 2010
last update:

Eclipse For Java Web Development

You need to start by getting all of the pieces - Java SE Development Kit, WebLogic Server, and Eclipse IDE. Go to to download Java SE Development kit (neither WLS nor Eclipse come with it). The Eclipse WTP project provides tools for developing standard Java web applications. Typical web artifacts in a Java environment are HTML, CSS and XML files, webservices and servlets. Eclipse WTP simplifies the creation these web artifacts. It provides runtime environments in which these artifacts can be deployed, started and debugged.

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 webapplication development including a Tomcat 7 server and the Apach MyFaceslibraries for JavaServer Faces development. You may ignore the JSF set-up infavor of JBoss RichFaces, Seam or any other framework you choose to follow.

  • My personal preference is Netbeans. Extremely easy to create and work with JEE projects 2. Code generation (creating entity classes from database, Session facades for entity classes) is superb 3.
  • The Eclipse IDE is famous for our Java Integrated Development Environment (IDE), but we have a number of pretty cool IDEs, including our C/C IDE, JavaScript/TypeScript IDE, PHP IDE, and more.

Step by step

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

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

    1. Going to Window -> Preferences -> General -> Editors-> Text Editors.
    2. Clicking Show print margin and typing '120' intoPrint margin column.
    3. Tab width in spaces is also set there as is whether to show linenumbers. Etc. There are many tailorings you may wish to pass fromworkspace 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 asC:Usersrussdevruss-eclipse.epf.
    4. Click Finish and ensure your preferences were put into the fileyou named.
  3. If you have workspace preferences saved that govern how newly set-up codeis automatically formatted by Eclipse, go to Window -> Preferences-> Java -> Code Style -> Formatter, click Import andthen navigate to find the template you saved away after modifying the format.I obsess over my code format and save my settings tojava-templat-formats.xml.
  4. Establish the workspace JDK by choosing Window -> Preferences-> Java -> Installed JREs. What will be there already is the JREinstalled on your computer host for executing Java programs. In my humbleopinion, you don't want this; seeSetting up the Sun Java Developer's Kit.

    To change it, click on the Location path, then click the Editbutton. Navigate to where you've created a copy of Sun's Java Development Kit.For example, mine is at C:Usersrussdevdownloadsjdk1.6.0_20. Whileyou're in the JRE Definition dialog, be sure to update the JREname: so it's obvious that you're using your special one. The name can beanything you choose; I chose jdk1.6.0_20. You should see something likethis:

  5. Now establish your server container environment. This is what you will useto run and test your web applications. I'm choosing Tomcat 7 because I get alot more 'JEE' cooperation, specifically for JavaServer Faces work, out of thatversion (although Tomcat 6 works very well too); seeSetting Up Apache Tomcat.

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

    1. Go to Window -> Preferences -> Server > RuntimeEnvironments.
    2. Click Add, then select the name that most closely corresponds toyour choice of environment. I'm choosing Apache Tomcat v7.0.
    3. Click Create a new local server to check. This will set up theserver in your Project Explorer view that you will be using torun and test.
    4. Click Next.
    5. For the Tomcat installation directory:, click Browseand go find the server you downloaded earlier. Or, you can click theDownload and Install.. button (but I've not always been pleasedwith that option which installs it for your whole computer).
    6. Click Finish.
  6. Finally, let's set up the JavaServer Faces (JSF) and JavaServer PagesStandard 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 hereyou may not want in that I'm choosing to use Apache MyFaces. Besides thisoption, there are many other implementations of Faces like Mojarra andRichFaces.

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

    1. Right-click in the Project Explorer view and chooseNew. Find the Dynamic Web Project wizard; you may haveto 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 theBuild Path operations you will becomeacquainted with when using Eclipse.

    First, fetch the downloads we need.

    • Get Apache MyFacesfrom here. Choose the relevant download link such asmyfaces-core.1.2.8-bin.zip for Windows ormyfaces-core.1.2.8-bin.tar.gz for Linux and Macintosh. Idownloaded it to the path C:Usersrussdevdownloads and unzippedit, reduced the subdirectory depth, etc. so that I could reference it inthe next step asC:Usersrussdevdownloadsmyfaces-core-1.2.8-binlib.
    • The JSTL download is harder to locate because of how unintuitive Sun hasmade it by mixing it in with GlassFish. You can look around by Googling'jstl download', but I found ithere. I clicked on DownloadJSTL, then on each of JSTL API and JSTL Implementationas we need both of these JARs. I put them into a subdirectory namedjstl-1.2 on the path C:Usersrussdevdownloads.

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

    1. In the User Libraries dialog, click New.. and typeJSF 1.2 (Apache MyFaces) as the name. Click OK.
    2. While the new library is selected, fill it with JARs by clickingAdd JARs.., then navigating to where you've downloaded ApacheMyFaces.
    3. Select all the JAR files on the path myfaces-core-1.2.8-bin/liband click Open. This will add them to the JSF library wecreated.
    4. Click New.. again and create JSTL 1.2. Add the two JARsit 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 andJSTL boxes to cause them to be added to the JSF capabilities for thisworkspace. Each time you set up project facets for a new Dynamic Web Project inEclipse, you should automatically get these libraries.

Eclipse

Finished!

At this point, you're ready to create your first Dynamic Web Project inEclipse. 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. Ihave removed the <description> elements for brevity andenhanced the <welcome-file-list> over what Eclipsesometimes generates.

Appendix B: faces-config.xml

Eclipse Web Development Ide

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 whatto expect.