Eclipse + Tomcat + WTP Web Development Project

Source: Internet
Author: User
Tags jboss apache tomcat
Document directory
  • Java Development Kit
  • Server Runtime
  • Eclipse and other eclipse Projects
  • Web tools Platform
  • Preferences
  • Choose a Server Runtime Environment
  • Create a J2EE WEB Project with a web module
  • The Design
  • Create a Servlet
  • Edit web module deployment descriptor
  • Add JSP
  • Processing ing our work:
  • Project has server Properties
  • Create a server
  • Start the server
  • Running the application
  • Can it be easier?
  • Conclusion

This document is the first in a series of tutorials where we will demonstrate how you can use the rich set of tools and APIs that comes with the web tools platform project. this first tutorial will give you a step by step tour of using some of the tools for web development. for this purpose we will build and run a J2EE Web application using WTP tools and Apache Tomcat.

By Naci Dai, eteration a.s.
December 7, 2004


This document is the first in a series of tutorials where we will demonstrate how you can use the rich set of tools and APIs that comes with the web tools platform project. this first tutorial will give you a step by step tour of using some of the tools for web development. for this purpose we will build and run a J2EE Web application using WTP tools and Apache Tomcat.

Getting started


Java Development Kit

You will need a Java Development Kit (JDK) version 1.4.2 and above as Eclipse 3.1 requires it. java Runtime Environment (JRE) is sufficient for eclipse but it will not be suitable for Web application development. you can get latest JDK from sun.


Most server runtimes require tools, such as the Java compiler (found in tools. Jar), that are only found in the JDK but not the JRE.


Server Runtime

We are going to be build a web application that runs on a server. to do this tutorial you will need a Server Runtime that is supported by WTP. apache Tomcat, objectweb Jonas, and JBoss are some of the OSS Licensed Server runtimes supported by WTP. you will need to download and install the Server Runtime before you can start working with WTP. it does not come with one. later you will learn how to tell WTP to use your preferred Server Runtime.


Eclipse and other eclipse Projects

Obviusly, you will need an eclipse installation. the eclipse driver used in this tutorial is you can find a suitable driver for your platform at here. WTP uses components from other eclipse projects: the eclipse modeling framework, EMF, Java EMF model, Jem, are used to define models for artifacts defined in development Java, W3C, XML and J2EE standards; the graphical editing framework, GEF, is used to provide graphical editing capabilities for XML, XSD and WSDL editors, etc. you can obtain these components from their projects.

  • EMF
  • GEF
  • JEM
Web tools Platform

You will need to download the full WTP package that is used in this tutorial from WTP downloads. You will need a WTP runtime.

Once you have everything downloaded, install JDK and your favorite Server Runtime, and unzip eclipse, EMF, GEF, JEM and wtp zip files to a folder. You are ready to go!




After you launch eclipse for the first time, but before we can build our first web application, we need to do a little eclipse house keeping: if it is not already defined, you will need to set the Java Preferences to point to your JDK installation (see figure ). you shoshould have obtained the latest JDK from sun.

Choose a Server Runtime Environment

This step is involves changing or creating the settings for installed server runtimes. during web application development, we will need a Server Runtime Environment to test, debug and run our project. we begin by telling WTP what our Server Runtime is, and where it is located. A "Server Runtime Environment" is not a server. it provides the environment, libraries and infrastructure that a "server" needs. A server is an instance of the Server Runtime that can host our web applications and other server-side components. to define a Server Runtime, We need to visit the appropriate preferences page:

Here you will find a list of server runtimes that have been defined previusly. You can choose a Server Runtime and change its properties. To add a new one click "Add ...". A wizard will popup and display a list of server runtimes that are supported by WTP. choose your Server Runtime from the list. jonas and JBoss are currently supported under the "generic Server Runtime ". basic "J2EE Runtime Library" is not a true Server Runtime, but can be used for coding support only. it does not provide a server instance that can be used to run Web artifacts.

After you click "Next", you will have to choose the JDK and the folder where you have installed your server runtime. of course, based on your choice of the Server Runtime, you will be asked to provide different properties. click Finish. now we can do the fun part.

We chose Apache Tomcat. you can also choose another server such as objectweb Jonas. if you wowould like to use them as your runtime, select the "generic server", and follow the Wizard to define the properties for Jonas, JBoss or weblogic. there are differences between for server runtimes; Apache Tomcat provides dynamic development support; I. e: the ability to run the web application from the Eclipse project without the need to publish the artifacts to a server. generic server does not support dynamic development, but it will automatically publish the artifacts (your web application) to the server when you run.

Developing the application

In this section we will create a J2EE Web application that contains typical components such as servlets and JSPs. We will:
• Create a project with a web module
• Create a Servlet
• Edit deployment descriptor
• Create and edit a JSP


Create a J2EE WEB Project with a web module

Go to New-> project. choose web category from the list of available wizards to create a new eclipse resource. you will find two project types available under this category. simple Web project is a basic eclipse resource project that can be associated with a server. we are interested in creating a J2EE Web application, so we will choose the J2EE WEB Project here:

A J2EE WEB Project is a type of Java project that has been initialized to host a server-side application. It needs to be associated with a target server.
A target server provides the container which will be used to execute our web application. specifically, it will provide a set of libraries (jars), that are added to the project classpath, which are needed to compile our custom classes. e. g.: to create a new servlet, we need javax. servlet. * packages. these are not a part of the JDK libraries. server Runtime provides them to the project. if Yiu have Skipped the previos step where we defined a Server Runtime, you will get a chance to define one here by clicking on the new ....
Make sure that "Add module to an ear Project" is not selected. We will create a standalone web application that can be deployed as a standard web module.
Finally, we will change the context root to "tutorial". This is used when you access the Web application with a URL, I. e. http: // localhost: 8080/tutorial

When the wizard completes its tasks, you will find that it has create a new Java project and has initialized it so that you can start with your development right away: it has created a Java source folder. you will add your custom Java packages and classes here. it has defined the classpath (JRE and tomcat) so that you will have J2EE libraries in your project classpath. it has created a resource folder named "web content", which mimics the standard war structure from the J2EE specification. it will contain the web resources that will be packaged with your J2EE Web module. all artifacts inside this folder are accessible from the Web application context.


The Design

This is a simple hello WTP application. We will call a simple "snoop Servlet" that will display information about the client browser in a JSP file.


Create a Servlet

Go to New-> class. Create a New Java class that is a subclass of the standard javax. servlet. http. httpservlet:

Add the Code provided in Listing 1 to the servlet:

Listing 1. snoopservlet. Java

package org.eclipse.wtp.tutorial;import;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class SnoopServlet extends HttpServlet {public void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {String userAgent =  req.getHeader("user-agent");String clientBrowser =  "Not known!";if( userAgent != null)clientBrowser = userAgent;req.setAttribute("client.browser",clientBrowser );req.getRequestDispatcher("/showBrowser.jsp").forward(req,resp);}}


Edit web module deployment descriptor

Web modules in J2EE has a deployment descriptor where you configure the web application and its components. this deployment descriptors is called the web. XML. according to the J2EE specification, it must be located in the WEB-INF folder. web. XML must have definitions for the servlet and the servlet URI mapping. enter the following lines into web. XML:


Listing 2. deployment descriptor web. xml

<servlet-name>Snoop Servlet</servlet-name>
<servlet-name>Snoop Servlet</servlet-name>

You can obtain code assist from the wtp xml editor during this process by hitting [CTRL] + [space] as usual.


Go to New-> file. Select the webcontent folder and enter showbrowser. jsp as the file name.

Our JSP file will be opened with a "JSP Source page editor ". hit [CTRL] + [space] and choose HTML template. you can get HTML, JavaScript, JSP and Java code assist in a JSP editor.

JSP Source page editor also provides Java code assist. Try the following inside a JSP expression:

At the end your JSP shoshould have the following code

Use the table template

Listing 3. showbrowser. jsp

<table border="1">
<tr> <th>Header</th><th>Value</th>
<td><%= request.getAttribute("client.browser")%></td>
Processing ing our work:

Go to the menu window> show view and select the project explorer view from the J2EE category.

Project Explorer is a smart navigator that knows about the web and J2EE artifacts. you will be able to browse your web module, deployment descriptors and the contents of these. for example, we can navigate to the deployment descriptor and observer our Servlet and its mapping.


Running the application

In this section we will run our recently developed web application on a server. In order to do this, we will:
• Create a server
• Add our web module to a server
• Run the server
• Run the web application.

Project has server Properties

In the Java package explorer or the J2EE project explorer, highlight your project name WTP-tutorial-I and using the right-click menu (mouse menu) choose Properties. alternatively, you can hit [alt] + [enter]. this will display Project Properties window. here we can set the target runtime and the default server for the project. remember that we have chosen the target runtime when we created the project. this runtime has provided the classpath for the project. you can choose a different runtime here. this will change the libraries that are used to compile your application. here we can also choose a default server. the default server is the server on which our application will be deployed and run. if you have not previusly defined a server as set it as default, you will see a message in the properties view. to create a server you will have to cancel this window and come back to it after you have created the server.


Create a server

Servers can be created using the Server View. To open a Server View go to window-> show view-> other ..., And choose the servers under the server category.

The server view allows you to create servers for the currently installed Server Runtime environments. use the right click menu to create a new server. in the following wizard, choose the server address where you will deploy (publish) Your web module. you will also have to choose the server type and, if you have defined more than one installed environment for this server (see preferences), choose th E Server Runtime. When you click Finish, you have a server!

Now you can go back to the Project Properties and set your default server.

Start the server

In the Server View, do a menu-click on your server and choose start, or you can simply click the start button. you will see the "status" of the server from stopped to "starting", and when the server is ready it will change its status to "started ".

Running the application

Open your favorite browser and type http: // localhost: 8080/tutorial/snoop.


Can it be easier?

Well, now that you have been through all this trouble to run your application, did you know that You coshould have done all this with almost just one click? Try to select you webcontents on try "run on server ...". The Web application will popup inside the WTP Web browser in almost an instant. You can then try to call your Servlet as usual.


In this tutorial, we looked at what is necessary to create a simple web application. The next part will show how we can debug our web application and monitor HTTP traffic.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.