diff options
| author | Kamal Wickramanayake <kamal@inbox.lk> | 2026-03-28 21:35:10 +0530 |
|---|---|---|
| committer | Kamal Wickramanayake <kamal@inbox.lk> | 2026-03-28 21:35:10 +0530 |
| commit | 5b6b5b62a0dfd4525db9a97a52634d1f8bb8e19c (patch) | |
| tree | 3017b927bd13690c3316ed5b6a0032eb3391f1a4 /maven/03-hello2-form-submission-webapp | |
| parent | dfc57517c8fd60f862029d95c25aa01410578c69 (diff) | |
Added a simple application showing form submission
Diffstat (limited to 'maven/03-hello2-form-submission-webapp')
7 files changed, 137 insertions, 0 deletions
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 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>com.example.servletapp</groupId> + <artifactId>hello2</artifactId> + <packaging>war</packaging> + <version>1.0-SNAPSHOT</version> + <name>hello2 Maven Webapp</name> + <url>http://maven.apache.org</url> + + <properties> + <maven.compiler.source>25</maven.compiler.source> + <maven.compiler.target>25</maven.compiler.target> + </properties> + + <dependencies> + <!-- Jakarta EE 11 API --> + <dependency> + <groupId>jakarta.platform</groupId> + <artifactId>jakarta.jakartaee-api</artifactId> + <version>11.0.0</version> <!-- Use latest 11 release --> + <scope>provided</scope> + </dependency> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>3.8.1</version> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <finalName>hello2</finalName> + + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.13.0</version> + <configuration> + <source>${maven.compiler.source}</source> + <target>${maven.compiler.target}</target> + </configuration> + </plugin> + </plugins> + </build> +</project>
\ 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("<!DOCTYPE html>"); + out.println("<html>"); + out.println("<head>"); + out.println("<title>Servlet Post Example Response</title>"); + out.println("</head>"); + out.println("<body>"); + out.println("<h1>Thank you for your submission, " + (userName != null && !userName.isEmpty() ? userName : "Guest") + "!</h1>"); + out.println("<p>Method used: " + request.getMethod() + "</p>"); + out.println("</body>"); + out.println("</html>"); + } + } +}
\ 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 @@ +<!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> + <display-name>Archetype Created Web Application</display-name> +</web-app> 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 @@ +<!DOCTYPE html> +<html> +<head> + <title>Jakarta EE 11 Servlet Post Example</title> +</head> +<body> + <h1>Enter your name</h1> + <form action="submitForm" method="POST"> + <label for="name">Name:</label> + <input type="text" id="name" name="name"> + <input type="submit" value="Submit"> + </form> +</body> +</html>
\ No newline at end of file |
