简体   繁体   English

在arraylist中存储值并打印它

[英]Storing values in arraylist and printing it

i am storing logged in user name in arraylist and than putting the arraylist in session .Whenever the user logged in for the first time it is printing the user name but when refreshing the page the same name is printing twice but i only want to print the username only one time no matter how many times the user refresh the page please help 我将登录的用户名存储在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));
              } 

Use a Hashmap, as it does not allow duplicates and will replace the original key with the new one. 使用Hashmap,因为它不允许重复,并将使用新密钥替换原始密钥。

HashMap hm = new HashMap();

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

I am not sure what you are asking for but as far as i understood your code following changes can be done to your programme.Let me know if it helps. 我不确定你要求的是什么,但据我所知,您的代码可能会对您的程序进行更改。让我知道它是否有帮助。

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));
                  } 
             }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM