aspose file tools*
The moose likes JSP and the fly likes JSP makes me headache Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP makes me headache" Watch "JSP makes me headache" New topic
Author

JSP makes me headache

Mark Lau
Ranch Hand

Joined: Dec 15, 2001
Posts: 120
On a customer registration page, say registration.jsp, I have
email:
password:
password:
first name:
last name:
which I want the customer to fill in and register. Well, I have a servlet to handle the registration request. But that's not my concern here.
I want the following features.
1) if the 2 passwords the customer has entered do not match, return to the same registraton page with a warning message: Your passwords do not
match!
2) if the customer fails to fill up an item and hits the submit button, return to the same registration page with appropriate warning messages like "Please enter your email", "Please enter your first name.", "Oops, you forgot to fill in your last name."
3) memorize the customer inputs except the password, so that when we return to the same registration page, everything except the password is restored in the input box. That is to say, the customer does not have to put in everything again. He merely needs to modify wherever there is an error which has been warned.
Just the kind of basic features everybody has seen somewhere on the web.
I have been trying this for quite some time. I have some confusions.
The first part of my registration.jsp file looks like this:
// registration.jsp

The problem is request.getParameter("blahblahblah") will return null the
very first time the page is visited, right?
So, for each of the 5 items(email, password1, password2, first name, last name), I need to code like the following, right?
Take emailAddress for example.
Here is the pseudo code of the registration.jsp file:

I kinda think my code is too tedious, and actually, I am not sure, I have given it a shot, but don't know if it is robust and will work for every possible situations.
How to you people achieve these features? Can you paste some code snippets? Thanks.
Gene
Brian Glodde
Ranch Hand

Joined: Jun 27, 2001
Posts: 171
My suggestion is to stop the empty submission before it leaves the client, with javascript:

however, if you still want to stick with the server side checking, you can avoid the null pointer problems like:

Additionally, you can "memorize" the user name in many different ways. If the user has to login to the application, you can save it in a database field, then pass it into session for use across the site. You could also use client side cookies, if they're enabled at the browser.
hope that helps!
[ March 18, 2002: Message edited by: Brian Glodde ]
Mark Lau
Ranch Hand

Joined: Dec 15, 2001
Posts: 120
Brian,
You smart! You know what problem I am having, exactly null pointer exception, that's why I take pains to write such tedious code. JavaScript is new to me. So it has become a necessity? I did do
if (emailAddress == null) emailAddress="";
in my servlet. That works fine. But do you mean I should also do this in the JSP file? I did, but then you will always have the warning message, even before the customer makes any attempt to fill up the form. Look this code which is probably what you meant.

Because I have assigned the empty string to emailAddress whenever I see that it is null, the red warning message is there even before the customer fills up the form. Obviously this is not what I want.
So, it seems we cannot achieve this feature without resorting to javascript?
[ March 18, 2002: Message edited by: Gene Chao ]
[ March 18, 2002: Message edited by: Gene Chao ]
Brian Glodde
Ranch Hand

Joined: Jun 27, 2001
Posts: 171
Again, this is just my opinion, but by using Javascript to check the fields before you submit the JSP may eliminate the work you must do at the servlet. Essentially you could eliminate (your choice) the code that checks against empty submitted values in the servlet because you will have already checked at the client. I typically choose to do more advanced validation within the servlet and save the basic "non-empty" code at the JSP page.
You can definitely achieve what you want without the use of Javascript and the code you have is close:

So this says "if you already know the email address, then display the value in the textbox, if not, then display the red warning text".
Here's how I would handle the whole thing:
1. A simple JSP for entering the registration information that contains the Javascript to stop the client from entering empty data. This eliminates the round trip to the server.
If it's not empty, it gets submitted to the servlet.
2. The servlet does "other" checking, such as validity of an email address. Alternatively this can be done in Javascript as well, but I am just using it as an example of more "advanced" checking.
*phew* hope that all makes sense and actually helps you!
Mark Lau
Ranch Hand

Joined: Dec 15, 2001
Posts: 120
Brian, thanks a lot. Your snippet of code helps me a lot. Meanwhile, I need to learn a little bit javascript.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP makes me headache