reset password

Web Development with Eclipse and Tomcat

This guide is for setting up Eclipse and Tomcat for Java web application development under Windows XP/Vista/7/10. A screen capture video illustrating this process is available on YouTube or can be downloaded as an mp4 file.

Java

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

Tomcat

Download the latest Tomcat 8.5 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-8.5.x.

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

Eclipse

Download the latest Eclipse IDE for Java EE Developers from Eclipse.org. There is now an Eclipse Installer, but personally I prefer the zip package as it does not require internet access or admin privilege during installation. 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 8.5, and the installation directory is your Tomcat directory. During project creation, check the option Generate web.xml deployment descriptor.

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 v8.5 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.

Deployment on CS3

To deploy your application onto the CS3 server, you simply need to transfer the following files from your Eclipse project to their corresponding folders under your CS3 account:

  • All the files and subfolders under the project build/classes folder should be transferred to the www/WEB-INF/classes folder under your CS3 account.
  • All the files and subfolders under the project WebContent folder, excluding the META-INF subfolder, should be transfered to the www folder under your CS3 account.
  • Transfer the web.xml file last - this basically tells the Tomcat server to load (or reload) your project.

Note that directory structure must be preserved during file transfer, e.g. build/classes/cs320/sample/HelloServlet.class should be transfered to www/WEB-INF/classes/cs320/sample/HelloServlet.class under your account on CS3.

After your application is deployed on the CS3 server, you may access it using the following URL:

http://cs3.calstatela.edu:8080/<username>/<servlet_or_jsp_name>

where <username> is your account name on the server.

This page has been viewed 87229 times.