Develop Servlets with Tomcat on XAMPP

Aditya Padhi
4 min readFeb 4, 2024

--

Embarking on the journey of servlet development with Tomcat on a Windows environment using XAMPP opens up a world of possibilities for web application enthusiasts. In this blog, we delve into the intricacies of crafting dynamic, server-side applications, leveraging the powerful combination of Apache Tomcat and XAMPP.

I. Tomcat Setup

1. Start the Tomcat Service

The server has been initiated and is actively receiving requests, prepared to deliver responses on port 8080.

2. You should now be able to see the below page on your browser on localhost:8000 / 127.0.0.1:8080 :

3. Manager Setup: Access the tomcat-users.xml file found in the C:\xampp\tomcat\conf\tomcat-users.xml file, remove the commenting from the specified role and user, and update the password to a memorable one.

4. Access the manager application from the browser using localhost:8080/manager or from the front end.

The Tomcat Web Application Manager will showcase all applications residing in the webappsfolder. (C:\xampp\tomcat\webapps)

II. Developing and Deploying the Servlet

  1. Open the webapps folder in VSCODE.

2. Install the Extension Pack for Java plugin.

3. Create Java Project.

4. Choose No Build Tools followed by webapps folder as Project Location.

Project Name: auth [Can be any name without spaces and hyphens]

Note: webapps folder must be open when creating the project so that the project named auth can be created inside the folder.

VSCode will automatically open the auth folder in a separate window. Now go ahead and close the webappsfolder window.

5. Modify the .vscode/settings.json file to incorporate the necessary adjustments for compiling and placing the class files in a suitable location within the WEB-INF, enabling Tomcat to recognize them.

{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "WEB-INF/classes",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

6. Now copy servlet-api.jar from the Tomcat library path xampp\tomcat\lib and place the same in the lib folder inside the project path.

VVIMP Note: In an application development lifecycle, libraries are required in two places one during the compilation and one during the running of the instance. During compilation VSCode picks the libaries from the folder path we provide in settings.json i.e the project `auth\lib` path of the application but while the application is running in the server Tomcat expects the library to be present in the `tomcat\lib` folder. So the jar must be kept in both the locations or VSCode must be provided with the appropriate path `tomcat\lib` directory for the successful compilation of a java servlet.

7. Create a folder structure in the source src folder for the application.

src\com\fire\App.java

package com.fire; 

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.IOException;
import javax.servlet.ServletException;

public class App extends HttpServlet {

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet App</title>");
out.println("</head>");
out.println("<body>");
out.println("Welcome to the Servlet App");
out.println("</body>");
out.println("</html>");
}
}

8. Upon saving the App.java file, a directory named WEB-INF\classes is automatically generated, and the App.class file is also located within the same folder.

Not required: Compiling the old way with all libraries from lib:

javac -cp C:\xampp\tomcat\lib\* -d C:\xampp\tomcat\webapps\auth\WEB-INF\classes App.java

9. Add web.xml with the following content to the WEB-INF folder.

WEB-INF\web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">
<display-name>Auth</display-name>
<description>
Welcome to Auth
</description>
<servlet>
<servlet-name>App</servlet-name>
<servlet-class>com.fire.App</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>App</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

10. Refresh the manager page & Reload the auth application if required and click on the /auth link.

Result:

Task: Now add JSON library to the lib directory in both tomcat/lib and auth/lib and test the working of the library.

Conclusion:

In wrapping up our exploration of servlet development with Tomcat and XAMPP on Windows, we’ve navigated through the fundamentals of setting up your environment and crafting dynamic web applications.

Thanks.

--

--