reset password

Web Development with Eclipse and Tomcat

This guide is for setting up Eclipse and Tomcat for Java web application development under Windows 2K/XP/Vista.

Java

Download and install the latest Java SE JDK from Sun/Oracle - we only need the JDK, not JavaFX, NetBeans or Java EE. Set a JAVA_HOME environment variable to the directory where JDK is installed.

Tomcat

Download the latest Tomcat 6 [1] binary release from the Apache Project. Note that there are several packages available for download. You should download the ZIP file under the "Core" distribution. After downloading the file, unzip it to a local directory, e.g. c:\apache-tomcat-6.0.x.

Add the JAR files of the following libraries to the lib folder under the Tomcat directory:

Eclipse

Download Eclipse IDE for Java EE Developers from Eclipse.org. Unzip it to a local directory, e.g. c:\eclipse, then start up Eclipse by double-click on eclipse.exe under the Eclipse folder. If you are using Eclipse for the first time, you will be asked to choose a folder to be Eclipse's workspace. Eclipse will store all your projects in that folder.

We can now create a sample web application using Eclipse. The application consists of a servlet called HelloServlet and a JSP page HelloJSTL.jsp.

First, create a Dynamic Web Project (File -> New -> Project ... -> Dynamic Web Project). You need to specify the project name and a target runtime. We'll use webtest as the project name. The type of the target runtime is Apache Tomcat 6.0, and the installation directory is your Tomcat directory.

Create a new servlet (Right click on the project name -> New -> Servlet). Enter webtest.servlet as the package name and HelloServlet as the class name then click Finish. Edit HelloServlet.java so the doGet() method looks like the following:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.getWriter().print("Hello Servlet");
}

Create a new JSP page (Right click on the project name -> New -> JSP). Enter HelloJSTL.jsp as the file name then click Finish. Edit HelloJSTL.jsp so it looks like the following:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>
<c:out value="Hello JSTL" />
</body>
</html>

Run the application (Right click on the project name -> Run As -> Run on Server). Select Tomcat v6.0 Server as the server type then click Finish. Enter the URL http://localhost:8080/webtest/HelloServlet in a browser and you should see "Hello Servlet" output by the servlet, and enter the URL http://localhost:8080/webtest/HelloJSTL.jsp and you should see "Hello JSTL" displayed by the JSP page.

A screen capture video illustrating this process is available here.

Notes

1. Although Tomcat 7 is available, our servers are still running Tomcat 6, so we recommend that you use Tomcat 6 in your development environment if you need to deploy your web application on servers like CS3.

This page has been viewed 85346 times.