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.
Prerequisites
Before starting, ensure you have a basic understanding of JSP, servlets, and a Java web application development environment set up.
Project Structure
jsp-form-example/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── example/
│ │ │ └── servlet/
│ │ │ └── RegisterServlet.java
│ │ ├── resources/
│ │ └── webapp/
│ │ ├── index.jsp
│ │ └── WEB-INF/
│ │ └── web.xml
└── pom.xml
Dependencies
Add the following dependencies to your pom.xml
file to use the latest versions of JSP and servlet APIs:
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>3.0.0</version>
</dependency>
JSP Form
Create an index.jsp
file with the following content:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<title>User Registration</title>
</head>
<body>
<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">
</form>
</body>
</html>
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;
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
@Override
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
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h2>Registration Successful</h2>");
out.println("<p>Username: " + username + "</p>");
out.println("<p>Email: " + email + "</p>");
out.println("</body></html>");
}
}
Configuration
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">
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.example.servlet.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
</web-app>
Running the Application
- Build and deploy the project to your web server (e.g., Apache Tomcat).
- Access the form by navigating to
http://localhost:8080/jsp-form-example/index.jsp
. - Fill out the form and submit it. You should see a confirmation page displaying the submitted data.
Summary
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.
Comments
Post a Comment
Leave Comment