The following example code demonstrates how to Display Form Data on Another Page Using Servlets.

Here is an example of a registration form and display the values entered by the user in another page using servlets in Java.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class RegistrationServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    RequestDispatcher dispatcher = request.getRequestDispatcher("/registration-form.jsp");
    dispatcher.forward(request, response);
  }

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String name = request.getParameter("name");
    String email = request.getParameter("email");
    String password = request.getParameter("password");
    request.setAttribute("name", name);
    request.setAttribute("email", email);
    request.setAttribute("password", password);
    RequestDispatcher dispatcher = request.getRequestDispatcher("/display-registration.jsp");
    dispatcher.forward(request, response);
  }
}

Here is an example of the JSP page, registration-form.jsp.

<!DOCTYPE html>
<html>
<head>
  <title>Registration Form</title>
</head>
<body>
  <h1>Registration Form</h1>
  <form action="/" method="post">
    <div>
      <label for="name">Name:</label>
      <input type="text" id="name" name="name">
    </div>
    <div>
      <label for="email">Email:</label>
      <input type="email" id="email" name="email">
    </div>
    <div>
      <label for="password">Password:</label>
      <input type="password" id="password" name="password">
    </div>
    <div>
      <button type="submit">Submit</button>
    </div>
  </form>
</body>
</html>

Here is an example of the JSP page, display-registration.jsp.

<!DOCTYPE html>
<html>
<head>
  <title>Display Registration</title>
</head>
<body>
  <h1>Display Registration</h1>
  <p>Name: ${name}</p>
  <p>Email: ${email}</p>
  <p>Password: ${password}</p>
</body>
</html>

In this example, the RegistrationServlet class handles both GET and POST requests. For a GET request, the servlet forwards the request to the registration-form.jsp page, which allows the user to enter their name, email, and password. For a POST request, the servlet retrieves the values entered by the user and sets them as attributes of the request. The servlet then forwards the request to the display-registration.jsp page, which displays the values entered by the user.


Further Reading

Understanding Enterprise Java Beans

Java Practice Exercise

programmingempire

Princites