注销失败后重定向,并出现java.lang.IllegalStateException:提交响应后无法创建会话 [英] Redirect after logout fails with java.lang.IllegalStateException: Cannot create a session after the response has been committed
问题描述
我有一个JSF2应用程序.我有一个会话范围内的登录bean和一个视图范围内的注销bean.登录时,我使用重定向,它可以正常工作.但是,注销失败并重定向.如果我注销但未重定向,则可以正常工作.
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";
}
}
登录页面具有到登录bean的绑定,所以我怀疑这可能与登录bean有关,尽管我不知道为什么它不起作用.错误是:
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
我的猜测是,自从我访问会话bean以来,它正在尝试在登录页面上创建会话,尽管我没有发现任何问题,并且无需重定向即可运行.
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.
我正在使用MyFaces 2.1.
I'm using MyFaces 2.1.
推荐答案
我建议使用Servlet而不是bean进行注销,托管bean(尤其是视图作用域)不适合注销.例如:
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 );
}
}
这篇关于注销失败后重定向,并出现java.lang.IllegalStateException:提交响应后无法创建会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!