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.xml2. π§ 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.javaandDemo2App.javawith staticgetMessage()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
mvn clean packageThis will generate a WAR file in the target/ directory.
6. π Deploy to Apache Tomcat
Copy the generated
.warfile to thewebapps/directory of your Tomcat installation.Access the app at:
http://localhost:8080/MyWebApp/

β Test the Application
Visit the root URL to see the JSP with two links.
Click each link to see the message from either
DemoApporDemo2App.


Last updated
Was this helpful?