Oliver Kaljuvee wrote:

> Hi,
>
> Does anyone know how 'carry' the input values once a JSP is processed.  For
> instance, if the user gave different passwords on the original form; once you
> process the form, you will notify the user that they have to provide a new
> password.  However, you want that the user does not have to fill out every field
> again.
>
> Thanks.
>
> -- Oliver

I approach this kind of issue by using a bean that maintains the values.  I prefer
the "put the logic in servlets and the presentation in JSP pages" approach to app
design -- I suppose you could do this with only JSP pages, but the separation makes
the structure clearer (at least to me).  Here's an example of a login form that
does not require the user to type in both values if they make a mistake.

The same general approach can be used to maintain input values (from the user's
perspective) for *any* input form where you are doing server-side validation, and
might ask the user to fix something and submit again.  I find it easiest to define
a bean that maintains all the fields for each particular input form.


(1) Login JSP Page (relevant excerpts only)

    <jsp:useBean id="attemptBean" scope="request"
     class="com.mycompany.AttemptBean" />

    <div align=center>
    <form action="http://www.mycompany.com/loginServlet">
    <table border=0 width="100%">
        <tr>
            <th colspan=2>Please Log In</th>
        </tr>
        <tr>
            <th align=right>Username:</th>
            <td><input type="text" name="username"
                 value="<%= attemptBean.getUsername() %>"></td>
        </tr>
        <tr>
            <th align=right>Password:</th>
            <td><input type="password" name="password"
                 value="<%= attemptBean.getPassword() %>"></td>
        </tr>
        <tr>
            <td colspan=2>
                <input type="submit" value="Log In">
                <input type="reset" value="Reset">
            </td>
        </tr>
    </table>
    </form>
    </div>

(2) Login Servlet - basic logic flow:

Accepts the "username" and "password" parameters from the request.
Attempts to validate this username/password combination.
If valid, stash some stuff in the session and use a request dispatcher to forward
to the main menu JSP page.
If invalid:
    Create an attempt bean with the values last submitted
    Store this bean under key "attemptBean" in the current request
    Use a request dispatcher to forward to the login JSP page.

(3) AttemptBean

The attempt bean has two String properties -- username and password.  The only
trick is that if you set one of these values to null, it returns a zero-length
string instead.

Note that this bean gets created by the login JSP page if it's not already in the
request.  The constructor initializes the values to zero-length strings, so that is
what you see the very first time you access the login JSP page.

In my real application, I also display an error message alert if you are returning
to the login page after an invalid attempt -- this was left out of the example
above for clarity.

Craig McClanahan

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to