注销后重定向失败并出现 java.lang.IllegalStateException:提交响应后无法创建会话

发布于 2024-12-29 00:11:13 字数 755 浏览 2 评论 0原文

我有一个 JSF2 应用程序。我有一个会话范围的登录 bean 和一个视图范围的注销 bean。当我登录时,我使用重定向并且工作正常。但是,注销因重定向而失败。如果我注销而不重定向它就可以了。

@ManagedBean
@ViewScoped
public class MbLogout extends BaseJsf {
    private static final long serialVersionUID = 2992671241358926373L;

    public String logout() throws DfException {
        getFacesContext().getExternalContext().invalidateSession();

        //return "login?faces-redirect=true"; // fails with this
        return "login";
    }
}

登录页面绑定到登录 bean,所以我怀疑这可能与它有关,尽管我不明白为什么它不起作用。错误是:

java.lang.IllegalStateException: Cannot create a session after the response has been committed

我的猜测是它试图在登录页面上创建一个会话,因为我访问了会话 bean,尽管我没有看到这有什么问题,并且它无需重定向即可工作。

我正在使用 MyFaces 2.1。

I have a JSF2 application. I have a login bean which is session scoped and a logout bean which is view scoped. When I login I use redirect and it works fine. However the logout fails with redirect. If I logout without redirect it works.

@ManagedBean
@ViewScoped
public class MbLogout extends BaseJsf {
    private static final long serialVersionUID = 2992671241358926373L;

    public String logout() throws DfException {
        getFacesContext().getExternalContext().invalidateSession();

        //return "login?faces-redirect=true"; // fails with this
        return "login";
    }
}

The login page has bindings to the login bean so I suspect this may have something to do with it, although I don't see why it doesn't work. The error is:

java.lang.IllegalStateException: Cannot create a session after the response has been committed

My guess is it's trying to create a session on the login page since I access the session bean although I don't see anything wrong with this and it works without redirect.

I'm using MyFaces 2.1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单 2025-01-05 00:11:13

我建议使用 Servlet 而不是 Bean 进行注销,托管 Bean(尤其是视图范围)不适合注销的目的。例如:

@WebServlet(name = "LogoutServlet", urlPatterns = {"/logout"}) // Can be configured in web.xml aswell
public class LogoutServlet extends HttpServlet {

    private static final String redirectURL = "http://www.somepage.com";

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Destroys the session for this user.
        if (request.getSession(false) != null) {
            request.getSession(false).invalidate();
            }
        response.sendRedirect(redirectURL );
    }
}

I would recommend using a Servlet rather than a bean for logout, a managed bean (especially view scoped) is not fitting for the purpose of logging out. For example:

@WebServlet(name = "LogoutServlet", urlPatterns = {"/logout"}) // Can be configured in web.xml aswell
public class LogoutServlet extends HttpServlet {

    private static final String redirectURL = "http://www.somepage.com";

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Destroys the session for this user.
        if (request.getSession(false) != null) {
            request.getSession(false).invalidate();
            }
        response.sendRedirect(redirectURL );
    }
}
森林散布 2025-01-05 00:11:13

它似乎与视图范围内的 bean 有关,该 bean 本身应该在会话中序列化。改为将其设置为请求范围。无论如何,视图范围对于注销来说没有多大意义。

@ManagedBean
@RequestScoped
public class MbLogout extends BaseJsf {
    // ...
}

It seems to be related to the bean being in the view scope which should by itself be serialized in the session. Make it request scoped instead. The view scope doesn't make much sense for logout anyway.

@ManagedBean
@RequestScoped
public class MbLogout extends BaseJsf {
    // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文