Name List - JSP



Name List - JSP


This example uses an HTML form to create a POST request containing the user’s name, and then uses JSP to show a list of all of the entered names.

index.html contains an HTML form:

<!DOCTYPE html>
<html>
  <head>
    <title>Name Form</title>
  </head>
  <body>
    <h1>What's your name?</h1>

    <form action="/post-name-list-jsp/names" method="POST">
      <input type="text" name="name" value="Ada">
      <br><br>
      <input type="submit" value="Submit">
    </form>

    <hr>

    <p>Click <a href="/post-name-list-jsp/names">here</a> to see everybody's name.</p>
  </body>
</html>

NamesServlet.java handles the POST request by outputting the user’s name to the response. The servlet also handles GET requests to the /names URL, which it handles by adding the list of names to the request and forwarding the request to name-list.jsp.

package io.happycoding.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.ServletException;

@WebServlet("/names")
public class NamesServlet extends HttpServlet {

  List<String> names = new ArrayList<>();

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {

    request.setAttribute("names", names);
    request.getRequestDispatcher("/WEB-INF/name-list.jsp").forward(request,response);
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws IOException {

    String name = request.getParameter("name");
    names.add(name);
    response.sendRedirect("/post-name-list-jsp/names");
  }
}

name-list.jsp renders an HTML page by iterating over every name in the list.

<%@ page import="java.util.List" %>
<!DOCTYPE html>
<html>
  <head>
    <title>Name List</title>
  </head>
  <body>
    <h1>Name List</h1>

    <ul>
      <% List<String> names = (List<String>) request.getAttribute("names"); %>
      <% for (String name : names) { %>
        <li><%= name %></li>
      <% } %>
    </ul>

    <p>Click <a href="/post-name-list-jsp/index.html">here</a> to enter another name.</p>
  </body>
</html>

name form

hello Ada

name list



Post Requests Examples

Comments

Happy Coding is a community of folks just like you learning about coding.
Do you have a comment or question? Post it here!

Comments are powered by the Happy Coding forum. This page has a corresponding forum post, and replies to that post show up as comments here. Click the button above to go to the forum to post a comment!