Java Servlet Application on WebSphere Liberty Profile | Generated by AI

Home PDF

Here’s a complete guide to running a very simple Java Servlet application on WebSphere Liberty Profile (WLP). This example creates a basic Servlet that returns “Hello World!” and deploys it to WLP using the dropins directory. We’ll assume you’ve already set up WLP with the servlet-4.0 feature enabled, as this provides the necessary Servlet support. This guide avoids using Maven and provides all required code and deployment steps.


Step-by-Step Guide

1. Create the Servlet Application Structure

Create a directory structure for your Servlet application manually. You can use any folder name, but for this example, we’ll call it SimpleServletApp.

2. Write the Servlet Code

Create a file named HelloServlet.java in SimpleServletApp/src/com/example/ with the following content:

package com.example;

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

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/plain");
        resp.getWriter().write("Hello World!");
    }
}

3. Create the web.xml Deployment Descriptor

Create a file named web.xml in SimpleServletApp/webapp/WEB-INF/ with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>com.example.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

4. Compile the Servlet

Compile the HelloServlet.java file into a .class file using javac. You’ll need the javax.servlet-api library in your classpath, which is provided by WLP but must be available during compilation.

5. Package the Application into a WAR File

Organize the compiled files and create a WAR file manually.

6. Deploy the WAR File on WLP

Deploy the WAR file to WLP using the dropins directory for automatic deployment.

7. Access the Application

Test the deployed Servlet in a browser.


Notes


Summary

This guide shows how to:

  1. Create a simple Servlet (HelloServlet.java) and web.xml.
  2. Compile the Servlet without Maven using javac.
  3. Package it into a WAR file (myapp.war) manually.
  4. Deploy it to WLP’s dropins directory.
  5. Access “Hello World!” at http://localhost:9080/myapp/hello.

This provides a straightforward, minimal Servlet application running on WebSphere Liberty Profile without build tools like Maven.


Back 2025.02.26 Donate