Cookies are stored on the client side and sent to the server with each request. Itβs not good practice to add passwords to cookies because they are easily intercepted and in many cases remain in usersβs browsers even after they leave the site.
You have to rely on the session, Java EE allows you to create a session with the user, where he will store the session identifier, which is then sent with each request. Instead, you can store information about this user on the server.
Using your code, you can create a session.
// get the session, add argument `true` to create a session if one is not yet created. HttpSession session = request.getSession(true); session.setAttribute("userName", request.getParameter("userName")); session.setAttribute("password", request.getParameter("password")); // to get the username and password String userName = session.getAttribute("userName"); String password = session.getAttribute("password");
Now, of course, if you do this when you clear the servers, the cache usernames and passwords will be deleted. Also, unencrypted passwords in the server cache certainly have security problems.
Edit:
If 2 people were supposed to use the same computer, then no, the code above will not work well. This is because user credentials are stored only in the session, nothing is saved after the session has been destroyed or the data in the session has been overwritten. Imagine a session is an object that is directly attached to each user. So right now I'm on StackOverflow, where somewhere in my code there is a special object just for me and my browser (session!), There is something else in the session object that says that my current user is logged in. I urge you to think about how you can store user credentials outside the session and instead store the currently logged in user inside the session.
To learn more about sessions and how they work there, there is an excellent answer here: What are sessions? How do they work?
source share