<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>User Input Form</title> </head> <body> <h1>User Input Form</h1> <form action="processForm" method="post"> <label for="name">Name:</label> <input type="text" id="name" name="name" required><br><br> <label for="email">Email:</label> <input type="email" id="email" name="email" required><br><br> <input type="submit" value="Submit"> </form> </body> </html> import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/processForm") public class FormProcessorServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Retrieve request parameters String name = request.getParameter("name"); String email = request.getParameter("email"); // Set response type response.setContentType("text/html"); // Generate response response.getWriter().println("<html><body>"); response.getWriter().println("<h1>Form Submission Successful</h1>"); response.getWriter().println("<p>Name: " + name + "</p>"); response.getWriter().println("<p>Email: " + email + "</p>"); response.getWriter().println("</body></html>"); } } <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_3_1.xsd" version="3.1"> <servlet> <servlet-name>FormProcessorServlet</servlet-name> <servlet-class>FormProcessorServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FormProcessorServlet</servlet-name> <url-pattern>/processForm</url-pattern> </servlet-mapping> </web-app> How to Run the Example Create a Dynamic Web Project: Use an IDE like Eclipse or IntelliJ to create a new Dynamic Web Project. Add the HTML Form: Create an HTML file (e.g., form.html) and paste the form code into it. Place this file in the WebContent or root folder. Add the Servlet: Create a new servlet class (FormProcessorServlet) and paste the servlet code into it. Configure web.xml: If you are not using annotations, add the servlet configuration in the web.xml file located in the WEB-INF directory. Run the Server: Deploy the project on a servlet container (like Apache Tomcat). Access the Form: Open a web browser and navigate to http://localhost:8080/YourProjectName/form.html to access the form. Submit the Form: Fill in the form and submit it. The servlet will process the input and display the name and email back to the user.
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter