Pregunta

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

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));
              } 
¿Fue útil?

Solución

Use a Hashmap, as it does not allow duplicates and will replace the original key with the new one.

HashMap hm = new HashMap();

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

Otros consejos

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));
                  } 
             }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top