tags:

views:

35

answers:

1

Hi,

I'm trying to finalize a signup process for my webapp, but am getting confused with servlet forwarding/redirects (again). The flow looks like this:

// /signup/index.jsp
<form action='/user/signup'>
</form>
<% 
if (request.getAttribute("msg") != null) {
    %><div><%= request.getAttribute("msg") %><%
}
%>

// Servlet which maps to '/user/signup':
public void doPost(..) {
    // process signup params..

    // I want to show the user /signup/index.jsp again after completion:
    req.setAttribute("msg", "signup worked!");
    req.getRequestDispatcher("/signup/index.jsp").forward(req, resp); 
}

when I use the forward() method, the browser shows the url of the post action still:

/user/signup

but the content is of:

/signup/index.jsp 

this is what forward() is supposed to do. I am not sure if this is confusing to the user though - the url is different than the page that actually generated the content.

I stepped through what twitter does, looks like they are foward()ing the user around:

// twitter.com/signup
if (ok) {
    forward("/find_sources/suggestions");
    if (ok) {
        forward("/find_sources/contacts");
        if (ok) {
            forward("/find_sources/anyone");
            if (ok) {
                forward("/");
            }
        }
    }
else {
    forward("/signup"); // error msg displayed here etc
}

In the above, if you mess up on the initial signup page, your browser shows the /account/create url, but the content of the page looks like /signup - so I guess they're doing the same thing I am (I am just assuming the underlying forward()/redirect() options are the same for all http servers, not specific to jsp).

Is my flow normal then? Should I be doing this some other way? I guess the options are:

  1. Use forward() and accept that the url will be of the action url.
  2. Use redirect() and store any messages I want to show the user in a session.

Thank you

+1  A: 

If you wish to do what twitter does - make all the validation in-line - i.e. display validation errors with AJAX. Then the users will click "register" only if they have completed everything correctly and the fact that forward is used won't be much of a problem.

However, in the general case, try to find some conversation framework (like spring web flow), or something similar to JSF's @ViewScope - i.e. something between request and session to accommodate this use case.

Another option is to use redirect and clear the contents of the session that have been just set.

Bozho