Demo: 1. Create Java Web Application

July 2025

Create a Java web application using JSP and Servlet, deploy it on Apache Tomcat, and display dynamic links generated by backend logic.

1. 📁 Project Structure Setup

Create a basic Maven or dynamic web project with the following structure:

MyWebApp/
├── src/
│   └── main/
│       └── java/
│           └── com/
│               └── example/
│                   ├── DemoServlet.java
│                   ├── DemoApp.java
│                   └── Demo2App.java
├── web/
│   └── index.jsp
├── WEB-INF/
│   ├── web.xml
├── pom.xml

2. 🧠 Backend Logic

Ensure you have the following classes:

  • DemoServlet.java

package com.example;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.IOException;

public class DemoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/html");
        String para = req.getParameter("p");
        if ("2".equals(para)){
            resp.getWriter().write("<h1>" + Demo2App.getMessage() + "</h1>");
        }else{
            resp.getWriter().write("<h1>" + DemoApp.getMessage() + "</h1>");
        }
        
        
    }
}
  • DemoApp.java and Demo2App.java with static getMessage() methods:

package com.example;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class DemoApp {
    public static String getMessage() {
        try {
            Document doc = Jsoup.connect("https://www.hku.hk").get();
            String title = doc.title();
            return "Website Title: " + title;
        } catch (Exception e) {
            return "Failed to fetch title: " + e.getMessage();
        }
    }
}
package com.example;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class Demo2App {
    static String urlname="https://www.hku.edu.hk";

    public static String getMessage() {
        try {
            Document doc = Jsoup.connect(urlname).get();
            String title = doc.title();
            return "Website Title: " + title;
        } catch (Exception e) {
            return "Failed to fetch title: " + e.getMessage();
        }
    }
}

3. 📝 JSP Page (index.jsp)

Create a JSP file that links to the servlet with different parameters:

<!DOCTYPE html>
<html>
<head><title>Welcome</title></head>
<body>
    <h2>Welcome to the WAR App</h2>
    <p><a href="demo">Click here to see the message</a></p>
     <p><a href="demo?p=2">Can you fix it to display the same message above</a></p>
</body>
</html>

4. 🧾 web.xml Configuration

Define the servlet and mapping in WEB-INF/web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >


<web-app xmlns="http://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://jakarta.ee/xml/ns/jakartaee
                             http://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
         version="5.0">

    <servlet>
        <servlet-name>DemoServlet</servlet-name>
        <servlet-class>com.example.DemoServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>DemoServlet</servlet-name>
        <url-pattern>/demo</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

5. 🧪 Using Maven Build and Package

Sample pom xml file for reference
mvn clean package

This will generate a WAR file in the target/ directory.


6. 🚀 Deploy to Apache Tomcat

  • Copy the generated .war file to the webapps/ directory of your Tomcat installation.

  • Access the app at: http://localhost:8080/MyWebApp/


  1. ✅ Test the Application

  • Visit the root URL to see the JSP with two links.

  • Click each link to see the message from either DemoApp or Demo2App.

The first link without problem, it can display the website title
It cannot display the website title, because the URL is incorrect

Last updated

Was this helpful?