Reputation: 866
I'm using a Glassfish 4 server with a JSF2 framework.
My problem
When calling the response.sendRedirect method, I'm having a IllegalStateException.
I don't have a single clue why this exception was raised.
The context
For every xhtml page, I have this method called to check if the user is still logged in.
<body id="body" onload="#{utilisateurBean.isUserLoggedIn()}">
The method
public void isUserLoggedIn() throws IOException {
if(this.user == null){
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
response.sendRedirect("Project/accueil/index.xhtml");
}
}
Exception
Infos: Exception when handling error trying to reset the response.
java.lang.IllegalStateException
Or
Avertissement: Servlet.service() for servlet session.AppExceptionHandler threw exception
java.lang.IllegalStateException: Cannot forward after response has been committed
I don't know why the response is being commited.
Thank for your help :)
Upvotes: 0
Views: 1683
Reputation: 3904
Change your code with below snippet:
public void isUserLoggedIn() throws IOException {
if(this.user == null){
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
response.sendRedirect("Project/accueil/index.xhtml");
return;
}
}
OR add a check to verify whether response is committed.
response.isCommitted()
For more info related to response already committed, check this.
Upvotes: 1
Reputation: 1965
You cannot alter the response when the response is already commit/defined.
Here you are displaying the page, so your response is already defined.
You have to do it in another way like showing an error message saying user is not connected and ask user to login again.
You could also do the redirect using javascript.
Upvotes: 1