【问题标题】:Storing values in arraylist and printing it在arraylist中存储值并打印它
【发布时间】:2014-03-25 19:29:25
【问题描述】:

我将登录的用户名存储在 arraylist 中,而不是将 arraylist 放入会话中。每当用户第一次登录时,它正在打印用户名,但刷新页面时,相同的名称会打印两次,但我只想要无论用户刷新多少次页面都只打印一次用户名请帮助

String username = request.getParameter("username");
            String password = request.getParameter("password");


HttpSession session = request.getSession(true);

        session.setAttribute("username", username);
        session.setAttribute("password", password);
        response.setContentType("text/html");                  
        ArrayList<user> users = (ArrayList<user>) sc
                      .getAttribute("users");

              if (users == null) {
                  System.out.println("loggedInUsers creates");
                  users = new ArrayList<user>();

              }
              users.add(new user(Name, U_ID, Pass));

              sc.setAttribute("users", users);


              users = (ArrayList<user>) sc.getAttribute("users");

              for (int i = 0; i <= users.size() - 1; i++) {
                  user user = users.get(i);
                  out.println(user.getUserName()+ "<br>");
                  //out.println("<br/>" + user.get(i));
              } 

【问题讨论】:

  • 在 ArrayList 上使用 contains() 来检查它是否已经拥有当前的用户名。如果是,则不添加,如果否,则添加。
  • 希望对您有所帮助 [Hashmap] [1]:stackoverflow.com/questions/3640648/…

标签: java jsp arraylist servlet-3.0


【解决方案1】:

使用 Hashmap,因为它不允许重复,并且会用新的键替换原始键。

HashMap hm = new HashMap();

hm.put (U_ID, new user(Name, U_ID, Pass));

【讨论】:

    【解决方案2】:

    我不确定您要什么,但据我了解,您可以对您的程序进行更改后的代码。如果有帮助,请告诉我。

    String username = request.getParameter("username");
        String password = request.getParameter("password");
    
    
        HttpSession session = request.getSession(true);
    
                session.setAttribute("username", username);
                session.setAttribute("password", password);
                response.setContentType("text/html");                  
                ArrayList<user> users = (ArrayList<user>) sc
                              .getAttribute("users");
                boolean shouldPrint = false; //declare this variable to check if printing of username is required
                      if (users == null) {
                          shouldPrint = true; //set this value to true to print username
                          System.out.println("loggedInUsers creates");
                          users = new ArrayList<user>();
    
                      }
                      users.add(new user(Name, U_ID, Pass));
    
                      sc.setAttribute("users", users);
    
    
                      users = (ArrayList<user>) sc.getAttribute("users");
    
    /**********As far as i understood your code.You need to set condition here to prevent twice printing of user name***********************************************/
                 if(shouldPrint)
                  {
                      for (int i = 0; i <= users.size() - 1; i++) {
                          user user = users.get(i);
                          out.println(user.getUserName()+ "<br>");
                          //out.println("<br/>" + user.get(i));
                      } 
                 }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-16
      • 1970-01-01
      相关资源
      最近更新 更多