Hi.
Sam Rose wrote:
> can do this without making it look messy??
>
> e.g. this is what I've got,
>
> if (newfirstname.equals("")){
> out.println("<b>You did not fill in your name</b>");
>
> }
> else if (newsurname.equals("")){
> out.println("<b>You did not fill in your surname</b>");
This is basicly a java question, but as you sent it to the list I'll
followup here :-)
To avoid the nesting blocklevels you could do something like this:
void doPost(...) {
/* ... */
htmlHeader(out);
theBody(request,out);
htmlFooter(out);
}
protected void theBody(HttpServletRequest request, PrintWriter out) {
/* Fetch needed data */
if(newfirstName.equals("")) {
out.println("<B>You did not fiill in your surename</B>");
return;
}
if(...) {
return;
}
Then the tests will for sure look nicer. And ofcourse, if you want these
tests to be applied in more than one context, you could put them in a
spesific procedure and so on.
Now, if you got a lot of fields you need to check, and want the same
error message when they are unfilled, you could put the field names in
an array and traverse them one by one.
static string theFields[] = { "firstName","sureName", ... };
boolean abort = false;
for(i=0;i<theFields.length;i++) {
String theData = request.getParameter(theFields[i]);
if(theData.equals("")) {
out.println("<B>Missing parameter "+theFields[i]+".</B>");
abort = true;
}
else {
/* put the data in a hash or whatever */
}
}
if(abort) {
/* Stop the further prosessing */
}
Now, if you want an object to be filled, that is with the relevant
fields based on the form fields name, you should could apply functions
found in java.lang.reflect. (Now, you could even create a servlet so
dynamic that it would ask the relevant object of what fields it should
fill in as well, though that would be overkill in most cases I would
think:-)
Hope this helps you in your quest.
KEB
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html