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. A screen capture video illustrating this process is available on YouTube or can be downloaded as an mp4 file.

Java Development Kit (JDK)

If you already have JDK 8 or above on your computer, you do not need to install a new JDK. If you don't have JDK or prefer a newer version, I recommend installing OpenJDK.

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.

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

Because the JDK on CS3 is still version 10 instead of 11, if you use JDK 11 on your computer, you must configure Eclipse to generate JDK 10 class files as follows:

  • Right click on the project and select Properties -> Project Facets
  • For the Java project facet, select 10 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 transfered 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 though it's 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 87275 times.