如何在java web中使用和无效会话?

时间:2017-08-15 09:59:15

标签: java session web

在我的Web应用程序中,我使用会话来存储正在更改的用户的信息,并且在数据库中我仅在会话结束时更新此数据,为此我使用HttpsessionListener并且一切正常,但是当用户登录到他们的帐户时,会有上一次会话的数据,我不知道是什么事。

当用户登录帐户时:

String name = request.getParameter(Attribute.ATTRIBUTE_USERNAME);
    String password = request.getParameter(Attribute.ATTRIBUTE_PASSWORD);
    String page;
    User user;
    try {
        user = userService.find(name, password); //I get data from the database
    } catch (ServiceException e) {
        log.error("Exception occurred in Login command ", e);
        request.setAttribute(Attribute.ATTRIBUTE_ERROR_MESSAGE, Messages.UNKNOWN_ERROR);
        return PagePath.PAGE_INFO;
    }

    HttpSession session = request.getSession(true);
    session.setAttribute(Attribute.ATTRIBUTE_USER, user);
    page = "controller?command=goToHome";

    return page;

和我的sessionListener:

public class UserListener implements HttpSessionListener {
    public void sessionDestroyed(HttpSessionEvent e) {
        HttpSession session = e.getSession();
        User user = (User) session.getAttribute(Attribute.ATTRIBUTE_USER);
        DAOFactory factory = new MySqlDAOFactory();
        UserService userService = new UserServiceImpl(factory.getUserDAO());
        userService.update(user);
        session.removeAttribute(Attribute.ATTRIBUTE_USER);
    }
}

0 个答案:

没有答案