0

Basically i have a enterprise jsf application which shows some services to the user.

Different departments of the enterprise host different applications on single webserver and links are provided in our application to access them.

I have a Servlet named RedirectServlet in my application through which i am redirecting all the requests to different applications.The following is my redirectservlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LOGGER.debug("In doGet()");
        try{
            if(request.getParameter("URL") != null){
                LOGGER.debug("URL Found redirecting forward");
                String redirectUrl = request.getParameter("URL"); 
                response.sendRedirect(redirectUrl);
            }
        }catch(NullPointerException e){
            LOGGER.debug("URL not found setting new session parameters & redirecting forward");
            HttpSession session = request.getSession(true);
            try {
                response.sendRedirect(properties.getProperty(REDIRECTURL, true));
            } catch (Exception e1) {
                LOGGER.debug("Expcetion with propertiesfile: ",e1);
            }
        }
    }

What i do above is if a link is clicked(URL parameter is present) i redirect to specific application, otherwise i just create a new session of my application and redirect to my application homepage.

when the user logsout from any other application, i am redirecting back to my application This is how i am redirecting back to my application

public void Logout(){
        LOGGER.debug("In LogOut");
        try
        {
            HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            HttpServletResponse res = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
            HttpSession session = req.getSession(false);
            LOGGER.debug("report session id:" + session.getId());
            res.sendRedirect(REDIRECTURL);
            session.invalidate();
        }catch(IllegalStateException e){
            LOGGER.debug("Exception in LogOut", e);
        }
        catch (IOException e){
            LOGGER.debug("Exception in LogOut", e);
        }
    }

i am getting the following server error message at res.sendRedirect line in the above code

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

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

java.lang.IllegalStateException: Cannot forward after response has been committed
    org.apache.myfaces.context.servlet.ServletExternalContextImpl.dispatch(ServletExternalContextImpl.java:368)
    org.apache.myfaces.trinidad.context.ExternalContextDecorator.dispatch(ExternalContextDecorator.java:44)
    org.apache.myfaces.trinidad.context.ExternalContextDecorator.dispatch(ExternalContextDecorator.java:44)
    org.apache.myfaces.trinidadinternal.context.FacesContextFactoryImpl$OverrideDispatch.dispatch(FacesContextFactoryImpl.java:267)
    org.apache.myfaces.view.jsp.JspViewDeclarationLanguage.buildView(JspViewDeclarationLanguage.java:94)
    org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:66)
    org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:239)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:191)

This is how we are managing session between applications

When the user first login to our application, we store in the some details in DB and after that every applications checks in the DB for that details.

Please somebody help how to solve this.This is time critical

Both the applications are using JSF 1.2 and Apache Trinidad.

Sreeram
  • 3,160
  • 6
  • 33
  • 44
  • http://stackoverflow.com/questions/2123514/java-lang-illegalstateexception-cannot-forward-after-response-has-been-committe – Sergey Benner Jan 21 '12 at 14:33

2 Answers2

2

The above exception is gone when i added the following line in Logout() method above

FacesContext.getCurrentInstance().responseComplete();

Here is my modified Logout() method

public void Logout(){
        LOGGER.debug("In LogOut");
        try
        {
            HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            HttpServletResponse res = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
            HttpSession session = req.getSession(false);
            LOGGER.debug("report session id:" + session.getId());
            session.invalidate();
            res.sendRedirect(REDIRECTURL);
            FacesContext.getCurrentInstance().responseComplete();
        }catch(IllegalStateException e){
            LOGGER.debug("Exception in LogOut", e);
        }
        catch (IOException e){
            LOGGER.debug("Exception in LogOut", e);
        }
    }
Sreeram
  • 3,160
  • 6
  • 33
  • 44
0
session.invalidate();
res.sendRedirect(REDIRECTURL);

Try this out. It may solve your problem.

ATR
  • 2,160
  • 4
  • 22
  • 43