reset password

Web Development with Eclipse and Tomcat

This guide is for setting up Eclipse and Tomcat for Java web application development on Windows 7/8/10. The process on Mac OS X and Linux should be quite similar.

Java Development Kit (JDK)

JDK 8 or above is required for this class. I recommend JDK 8 or 11 as these two are the long-term support (LTS) versions. If you don't have JDK 8+ or prefer a newer version, please download and install OpenJDK. Note that if you already have a JDK installed, you should uninstall it before installing a new version.

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 Enterprise Java 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.

Please read this article on how to fix Eclipse slow startup on Windows 10.

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:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>HelloJSTL</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

The JDK on CS3 is version 11. If your JDK version is higher than 11, you must configure Eclipse to generate JDK 11 class files as follows:

  • Right click on the project and select Properties -> Project Facets
  • For the Java project facet, select 11 from the Version drop-down list
  • Click Apply and Close

And 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 transferred to the www folder under your CS3 account.
  • Transfer the web.xml file. This should be done after all other files are transferred, and later whenever you update your class files on CS3, you must re-upload the web.xml file even if its content is not changed. This is because the Tomcat server monitors the web.xml file of each project, and uploading/re-uploading web.xml basically tells the Tomcat server to load/reload your project.

Note that directory structure must be preserved during file transfer, e.g. build/classes/cs3220/sample/HelloServlet.class should be transfered to www/WEB-INF/classes/cs3220/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 87117 times.