JSP Form Processing Using Post Method

Below is the step-by-step implementation of POST method.

Step-by-Step implementation of POST Method

Step 1: Create the dynamic web project using eclipse and its project named as jsp-POST-demo.

Step 2: Create a JSP form named index.jsp and this form includes input fields to store user data and form attributes that can be stored on a JSP page to configure the form to be submitted and set the method attribute to “POST “.

HTML
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Form Processing</title>
    <link rel="stylesheet" type="text/css" href="WEB-INF/css/style.css">
    <style>
        /* Additional CSS for better visibility */
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
        }

        .container {
            width: 50%;
            margin: 50px auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        h2 {
            color: #333;
            text-align: center;
        }

        form {
            margin-top: 20px;
        }

        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        input[type="text"],
        input[type="email"],
        input[type="checkbox"],
        input[type="radio"],
        input[type="submit"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-sizing: border-box; /* Ensure padding and border are included in width */
        }

        input[type="submit"] {
            background-color: #007bff;
            color: #fff;
            cursor: pointer;
        }

        input[type="submit"]:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>Submit Form (POST Method)</h2>
        <form action="processPost.jsp" method="POST">
            <label for="name">Name:</label>
            <input type="text" id="name" name="name" required><br>
            <label for="email">Email:</label>
            <input type="email" id="email" name="email" required><br>
            <label for="subscribe">Subscribe:</label>
            <input type="checkbox" id="subscribe" name="subscribe"><br>
            <label for="gender">Gender:</label>
            <input type="radio" id="male" name="gender" value="male"> Male
            <input type="radio" id="female" name="gender" value="female"> Female<br>
            <input type="submit" value="Submit">
        </form>
    </div>
</body>
</html>


Step 3: Create the new JSP page and it named as processPost.jsp to handles the form submission and in this page can retrieve the form data from the request object using getParameter() method. This method can take the name of the form field as its the parameter and returns the value entered by user.

HTML
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>POST Form Processing</title>
    <link rel="stylesheet" type="text/css" href="WEB-INF/css/style.css">
    <style>
        /* Additional CSS for better visibility */
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
        }

        .container {
            width: 50%;
            margin: 50px auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        h2, h3 {
            color: #333;
            text-align: center;
        }

        p {
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>POST Form Processing</h2>
        <%-- Retrieving parameters from the request --%>
        <% String name = request.getParameter("name"); %>
        <% String email = request.getParameter("email"); %>
        <% String subscribe = request.getParameter("subscribe"); %>
        <% String gender = request.getParameter("gender"); %>

        <h3>Hello, <%= name %></h3>
        <p>Your email is: <%= email %></p>
        <p>Subscribed: <%= (subscribe != null) ? "Yes" : "No" %></p>
        <p>Gender: <%= gender %></p>
    </div>
</body>
</html>


Step 4: Once we complete the project, it runs the tomcat server then output shows the form. Refer the below output image for better understanding.

Output:

1. Display the FORM:

2. Display Processed Data:

If we follow the above steps, then we can successfully build the JSP form processing demo project.



JSP – Form Processing

In JavaServer Pages, Form processing is the fundamental aspect of web development, and it can allow users to interact with websites by submitting data. It can handle form submissions involving extracting the data from the HTTP requests and generating the appropriate responses.

Similar Reads

Form Processing in JSP

Form processing in the JavaServer Pages involves receiving data submitted through the HTML forms extracting this data from the request object and generating the response based on the received data....

JSP Form Processing Using GET Method

The GET method is one of the HTTP methods used to send form data to a web server. When this happens, the form is submitted using the GET method and the form data is inserted into the query parameters of the URL....

JSP Form Processing Using Post Method

Below is the step-by-step implementation of POST method....