views:

1187

answers:

3

I need to validate this simple pick list:

<select name="<%= key %>">
    <option value="ETC" SELECTED>Select an option...</option>
    <option value="ONE">Lorem ipsum</option>
    <option value="TWO">dolor sit amet</option>
</select>

So the user would never submit the form with the, excuse the repetition, "Select an option..." option selected. In principle I'm allowed to use JavaScript but It'd be interesting to learn how to solve it within JSP too.

+2  A: 

You can never really satisfy the condition 'never submit a given value' because you don't have control over the client side. The user can always manipulate HTML to submit whatever they want.

It is a good approach is to use JavaScript to do client-side validation and give the user quick feedback and catch 99%+ of the cases, then do a server-side validation of the submitted parameters to catch the minority that don't have JS enabled or who manipulate the HTML to submit non-expected values.

Just remember that the client-side validation is optional, and is good for those 'common mistakes' input validation, but the server-side validation is mandatory for all input whether or not any client-side checks have been done on the given input.

Pete
Thanks for the comment Pete!
Nano Taboada
+1  A: 

Nowadays you usually don't validate in JSPs, because they only visualize whatever was processed earlier. So the only validation that you do "in jsps" is usually Javascript. For the rest (the real validation) I second what Pete answered: You have to do it serverside in whatever technique you are using there. When it's displayed in the JSP again, validation has hopefully long been done.

As I said "nowadays": When JSP was a shiny new concept, a lot more was done inside the boundaries of a JSP and sometimes even Forms were posted to JSPs. How to validate was even more nonstandard at that time.

Olaf
Olaf, thanks much for the comment!
Nano Taboada
+1  A: 

JSP is a "view" in MVC pattern, and should therefore only be used to present the data to the user. Any application logic, including validation logic, should be done server-side. If this JSP is intended to be a part of a large app, I would recommend using Spring MVC to set up your app, and writing a validator to validate the input. But even if we're not talking about some large application here, the validation should still be done server-side, as others before me have already noted.

Sandman
Thanks much for your comment, I'd definitely like to follow your advice and learn Spring, but the thing is, I'm not starting an app from scratch, I'm dealing with an existing, propietary, production application.
Nano Taboada