将项目添加到ArrayList中,但不要保存它们

时间:2019-04-16 18:12:53

标签: java jsp

以index.html的形式,我输入用户名和密码,并将它们添加到jsp页面“ supporto.jsp”的两个不同的ArrayList中,即“用户”和“ passw”。然后从这里在“ registration.jsp”中进行重定向,到目前为止一切正常。当我重复将新事物添加到数组并打印它们的操作时,它显示出该数组仅包含一个元素(而不是两个),并且仅显示了最后一个插入的元素。为什么?

index.html

<html>

  <head>
    <title>
      Registrazione
    </title>
  </head>

  <body>
    <h3> Scegli un username ed una password per registrarti: </h3>
    <form action="appoggio.jsp" method="post">
      <p> <input type="text" name="user"> Username </p>
      <p> <input type="password" name="pwd"> Password </p>
      <p> <input type="submit" value="Invia"> </p>
    </form>
  </body>

</html>

appoggio.jsp

<head>
    <title>
      Benvenuto/a
    </title>
  </head>

  <body>
    <%@page import="java.util.ArrayList"%>
    <%
      String usern = request.getParameter("user");
      String pass = request.getParameter("pwd");
      String nextPage;

      ArrayList<String> utenti = new ArrayList<String>(); //qui dentro salvo tutti gli usern
      ArrayList<String> passw = new ArrayList<String>(); //qui dentro salvo tutte le pass
    %>
    <%
      utenti.add(usern);
      passw.add(pass);
      request.setAttribute("usern", utenti);
      request.setAttribute("pass", passw);
      nextPage = "/registrazione.jsp";
      RequestDispatcher rd=request.getRequestDispatcher(nextPage);
      rd.forward(request, response);
    %>
  </body>
</html>

registrazione.jsp

<html>

  <head>
    <title>
      Registrazione
    </title>
  </head>

  <body>
    <%@ page import="java.util.ArrayList" %>
    <%
      ArrayList utenti = (ArrayList)request.getAttribute("usern");
      //utenti.add(request.getParameter("usern"));
    %>
    <h1> Ciao <%= utenti %> </h1>
    <a href="index.html"> CLicca </a>

  </body>

</html>

1 个答案:

答案 0 :(得分:1)

每次加载页面时,都会创建一个新的数组列表。您需要一个控制器,以便在提交表单时,方法会保存这些值

相关问题