使用jsf进行自动化,异常java.lang.IllegalStateException:在提交响应后无法转发

时间:2014-05-06 21:04:27

标签: jsf

我尝试使用jsf 1.2制作autorisation表单,但我在重定向方面有一些例外。

Bean代码

public class UserAutorisationBean implements Serializable {

    private Boolean isLogin = false;
    private String login = "";
    private String password = "";

    public void autoriseLoginAndPass() throws IOException {
        if (this.login == "user" && this.password == "password") {
            this.isLogin = true;
            FacesContext context = FacesContext.getCurrentInstance();
            HttpServletResponse response = 
                    (HttpServletResponse) context.getExternalContext().getResponse();

            response.sendRedirect("web/HelloWorld.jsp");
        } else {
            FacesContext context = FacesContext.getCurrentInstance();
            HttpServletResponse response = 
                    (HttpServletResponse) context.getExternalContext().getResponse();

            response.sendRedirect("web/Error.jsp");
        }

    }

过滤代码

public class Filter implements javax.servlet.Filter {

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        HttpSession session = null;
        session = httpRequest.getSession(false);

        if (session != null) {
            UserAutorisationBean userAutorisationBean = null;
            userAutorisationBean =
                    (UserAutorisationBean) session.getAttribute("userautorisationbean");

            if (userAutorisationBean != null) {
                if (!userAutorisationBean.isLoginIn()) {
                    httpResponse.sendRedirect(httpRequest.getContextPath() + "/Error.jsp");
                }
            }
            chain.doFilter(request, response);
        }
        if (session == null) {
            httpResponse.sendRedirect(httpRequest.getContextPath() + "/Error.jsp");
        }
    }

    public void destroy() {

    }

}

堆栈: message在提交响应后无法转发

说明服务器遇到内部错误,导致无法完成此请求。

例外

javax.servlet.ServletException: Cannot forward after response has been committed
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:256)
root cause

java.lang.IllegalStateException: Cannot forward after response has been committed
    com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:414)
    com.sun.faces.application.ViewHandlerImpl.executePageToBuildView(ViewHandlerImpl.java:455)
    com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:139)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:108)
    com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:266)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:159)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:245)

我试图通过搜索找到一些建议,但它并没有帮助我解决问题。

0 个答案:

没有答案