views:

2828

answers:

6

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)
A: 

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

Maksym Govorischev
Seam also allows you to do this out of the box
mtpettyp
I am using JSF and FIlter to check whether the session is null if null redirect user to login page.
+1  A: 

Not sure, but try doing this through ExternalContext facilities:

Something like this:

ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
externalContext.redirect(externalContext.encodeResourceURL(externalContext.getRequestContextPath()+getUrl()));
Maksym Govorischev
A: 

You're too late and that is giving you the exception. Do it in a phaselistener before the render response phase.

Gene De Lisa
This makes no sense.
BalusC
+4  A: 

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.

BalusC
I love stackoverflow! I have yet to need to ask a question, because I always find mine already answered here. Thanks BalusC.
Naganalf
A: 

java.lang.IllegalStateException: Cannot forward after response has been committed

The exception is fairly clear:

1) the response has been committed. 2) you cannot forward after the response has been committed.

What doesn't make sense?

Gene De Lisa
A: 

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.

Karoff