Once the user successful login to the system, the system will redirect the user to the homepage. Now my problem is, if the user clicks on the view account page without login to the system the system will redirect user to the login page. if user login to the system now the system will redirect the user to the homepage, in this case any method can redirect user to the previous page that is view account page instead of homepage?
i tried using session
String url = (String)session.getAttribute("url");
if(url != null)
response.sendRedirect(url);
else
response.sendRedirect("homepage.faces");
i put this code under public void doBtnAction(){} if the user login successful then redirect to the url. But i got this error
java.lang.IllegalStateException: Cannot forward after response has been committed
com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:322)
com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:130)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:117)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)
This exception is caused because you called response.sendRedirect()
and didn't block JSF from rendering the normal response. You need to inform JSF that it doesn't need to handle normal response by adding
FacesContext.getCurrentInstance().responseComplete();
to the action method.
Or, even better, just don't get the HttpServletResponse
from under the JSF's hoods, but instead do:
FacesContext.getCurrentInstance().getExternalContext().redirect(url);
This will automatically invoke responseComplete()
and it also keeps your code clean from unnecessary Servlet API stuff. Also see the ExternalContext
API.
Not sure, but try doing this through ExternalContext
facilities:
Something like this:
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
externalContext.redirect(externalContext.encodeResourceURL(externalContext.getRequestContextPath()+getUrl()));
You haven't described any framework facilities you use for authentication so I assume that you perform authentication in own code. Probably, the simplest approach would be to save initial destination in session before redirecting to the login page, and then after successful login you can check this attribute and redirect to right place.
I particular for JSF you can do this in action method that processes login.
Actually, I'd suggest to use some framework for authentication, for example Spring Security, because it does such things out of box, and it will be much easier to extend your security system in case you need some additional features, though it needs some additional configuration
Logic described (login-redirect) should be implemented through Filter mechanics. Also you can try standard jsf navigation rules(if it can fit you case). And if you still want to send redirect to a custom url and don't want to use Filters, do it in your servlet in render phase, not in your jsf bean.
java.lang.IllegalStateException: Cannot forward after response has been committed
The exception is fairly clear:
- The response has been committed.
- You cannot forward after the response has been committed.
What doesn't make sense?
You're too late and that is giving you the exception. Do it in a phaselistener before the render response phase.
Placing the below syntax in web.xml should work:
<context-param>
<param-name>com.sun.faces.writeStateAtFormEnd</param-name>
<param-value>false</param-value>
</context-param>