From 5b6b5b62a0dfd4525db9a97a52634d1f8bb8e19c Mon Sep 17 00:00:00 2001 From: Kamal Wickramanayake Date: Sat, 28 Mar 2026 21:35:10 +0530 Subject: Added a simple application showing form submission --- maven/03-hello2-form-submission-webapp/.gitignore | 1 + .../.vscode/settings.json | 3 ++ maven/03-hello2-form-submission-webapp/README | 28 +++++++++++++ maven/03-hello2-form-submission-webapp/pom.xml | 49 ++++++++++++++++++++++ .../lk/ac/pdn/ceit/hello/PostExampleServlet.java | 35 ++++++++++++++++ .../src/main/webapp/WEB-INF/web.xml | 7 ++++ .../src/main/webapp/index.jsp | 14 +++++++ 7 files changed, 137 insertions(+) create mode 100644 maven/03-hello2-form-submission-webapp/.gitignore create mode 100644 maven/03-hello2-form-submission-webapp/.vscode/settings.json create mode 100644 maven/03-hello2-form-submission-webapp/README create mode 100644 maven/03-hello2-form-submission-webapp/pom.xml create mode 100644 maven/03-hello2-form-submission-webapp/src/main/java/lk/ac/pdn/ceit/hello/PostExampleServlet.java create mode 100644 maven/03-hello2-form-submission-webapp/src/main/webapp/WEB-INF/web.xml create mode 100644 maven/03-hello2-form-submission-webapp/src/main/webapp/index.jsp diff --git a/maven/03-hello2-form-submission-webapp/.gitignore b/maven/03-hello2-form-submission-webapp/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/maven/03-hello2-form-submission-webapp/.gitignore @@ -0,0 +1 @@ +target diff --git a/maven/03-hello2-form-submission-webapp/.vscode/settings.json b/maven/03-hello2-form-submission-webapp/.vscode/settings.json new file mode 100644 index 0000000..c5f3f6b --- /dev/null +++ b/maven/03-hello2-form-submission-webapp/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.configuration.updateBuildConfiguration": "interactive" +} \ No newline at end of file diff --git a/maven/03-hello2-form-submission-webapp/README b/maven/03-hello2-form-submission-webapp/README new file mode 100644 index 0000000..015d62f --- /dev/null +++ b/maven/03-hello2-form-submission-webapp/README @@ -0,0 +1,28 @@ +This webapp shows how a servlet is used to process a form submission. + +Command to generate this webapp from Maven archetype: + +mvn archetype:generate -DgroupId=com.example.servletapp \ + -DartifactId=hello2 \ + -DarchetypeArtifactId=maven-archetype-webapp \ + -DinteractiveMode=false + +Open the project inside the IDE (like Visual Studio Code). + +Update the pom.xml file to the one provided here. + +Create directory src/main/java . + +Create a new package (say lk.ac.pdn.ceit.hello). + +Create a new Java class (name it PostExampleServlet). + +Complete the Java class (look at the given PostExampleServlet.java file). + +Replace the content of src/main/webapp/index.jsp with the one provided. + +Build the project: + + mvn clean package + +Deploy the generated hello2.war to Tomcat and access it via http://localhost:8080/hello2/. diff --git a/maven/03-hello2-form-submission-webapp/pom.xml b/maven/03-hello2-form-submission-webapp/pom.xml new file mode 100644 index 0000000..0091cbf --- /dev/null +++ b/maven/03-hello2-form-submission-webapp/pom.xml @@ -0,0 +1,49 @@ + + 4.0.0 + com.example.servletapp + hello2 + war + 1.0-SNAPSHOT + hello2 Maven Webapp + http://maven.apache.org + + + 25 + 25 + + + + + + jakarta.platform + jakarta.jakartaee-api + 11.0.0 + provided + + + + junit + junit + 3.8.1 + test + + + + + hello2 + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + \ No newline at end of file diff --git a/maven/03-hello2-form-submission-webapp/src/main/java/lk/ac/pdn/ceit/hello/PostExampleServlet.java b/maven/03-hello2-form-submission-webapp/src/main/java/lk/ac/pdn/ceit/hello/PostExampleServlet.java new file mode 100644 index 0000000..f6ba0c6 --- /dev/null +++ b/maven/03-hello2-form-submission-webapp/src/main/java/lk/ac/pdn/ceit/hello/PostExampleServlet.java @@ -0,0 +1,35 @@ + +package lk.ac.pdn.ceit.hello; + +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; + +@WebServlet("/submitForm") +public class PostExampleServlet extends HttpServlet { + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + response.setContentType("text/html;charset=UTF-8"); + String userName = request.getParameter("name"); + + try (PrintWriter out = response.getWriter()) { + out.println(""); + out.println(""); + out.println(""); + out.println("Servlet Post Example Response"); + out.println(""); + out.println(""); + out.println("

Thank you for your submission, " + (userName != null && !userName.isEmpty() ? userName : "Guest") + "!

"); + out.println("

Method used: " + request.getMethod() + "

"); + out.println(""); + out.println(""); + } + } +} \ No newline at end of file diff --git a/maven/03-hello2-form-submission-webapp/src/main/webapp/WEB-INF/web.xml b/maven/03-hello2-form-submission-webapp/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..9f88c1f --- /dev/null +++ b/maven/03-hello2-form-submission-webapp/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/maven/03-hello2-form-submission-webapp/src/main/webapp/index.jsp b/maven/03-hello2-form-submission-webapp/src/main/webapp/index.jsp new file mode 100644 index 0000000..77dd3d6 --- /dev/null +++ b/maven/03-hello2-form-submission-webapp/src/main/webapp/index.jsp @@ -0,0 +1,14 @@ + + + + Jakarta EE 11 Servlet Post Example + + +

Enter your name

+
+ + + +
+ + \ No newline at end of file -- cgit v1.2.3