Hello, I want to get the username from session after the user logged in and store in my database when I’m using insert or update.
When the user is logged in, I setAttribute like this
try
{
LoginBean user = new LoginBean();
user.setUserName(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
user = DAO.login(user);
if (user.isValid())
{
HttpSession session = request.getSession(true);
session.setAttribute("currentSessionUser",user);
response.sendRedirect("/oosd/login/member.jsp"); //logged-in page
}
else
response.sendRedirect("/oosd/login/invalidLogin.jsp"); //error page
}
How do I retrieve the username later on other servlets as a string for inserting ?
Use
HttpSession#getAttribute()with the same attribute name to reobtain the set attribute.