JSP Form Example

Creating forms in JSP (JavaServer Pages) is a common task for collecting user inputs and processing them on the server side. In this example, we'll create a simple JSP form for user registration, including fields for username, password, and email. We'll also show how to handle the form submission and validate the input data using a servlet.


Before starting, ensure you have a basic understanding of JSP, servlets, and a Java web application development environment set up.

Project Structure

├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/
│   │   │       └── example/
│   │   │           └── servlet/
│   │   │               └── RegisterServlet.java
│   │   ├── resources/
│   │   └── webapp/
│   │       ├── index.jsp
│   │       └── WEB-INF/
│   │           └── web.xml
└── pom.xml


Add the following dependencies to your pom.xml file to use the latest versions of JSP and servlet APIs:


JSP Form

Create an index.jsp file with the following content:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
    <title>User Registration</title>
    <h2>User Registration Form</h2>
    <form action="register" method="post">
        <label for="username">Username:</label>
        <input type="text" id="username" name="username" required><br><br>
        <label for="password">Password:</label>
        <input type="password" id="password" name="password" required><br><br>
        <label for="email">Email:</label>
        <input type="email" id="email" name="email" required><br><br>
        <input type="submit" value="Register">

Servlet to Handle Form Submission

Create a servlet named RegisterServlet.java to handle the form submission:

package com.example.servlet;

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;

public class RegisterServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Retrieve form data
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");

        // Validate and process the form data
        // For simplicity, just display the data in the response
        PrintWriter out = response.getWriter();
        out.println("<h2>Registration Successful</h2>");
        out.println("<p>Username: " + username + "</p>");
        out.println("<p>Email: " + email + "</p>");


Configure the servlet in the web.xml file:

<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd" version="6.0">

Running the Application

  1. Build and deploy the project to your web server (e.g., Apache Tomcat).
  2. Access the form by navigating to http://localhost:8080/jsp-form-example/index.jsp.
  3. Fill out the form and submit it. You should see a confirmation page displaying the submitted data.


This tutorial demonstrated how to create a simple JSP form, handle form submission using a servlet, and validate the input data. By following these steps, you can build more complex forms and handle various types of user input in your web applications.