wood burning stoves 2.0*
The moose likes Struts and the fly likes Redisplay Variables in JSP after Failed Validation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Redisplay Variables in JSP after Failed Validation?" Watch "Redisplay Variables in JSP after Failed Validation?" New topic
Author

Redisplay Variables in JSP after Failed Validation?

David Ogasawara
Greenhorn

Joined: Oct 11, 2001
Posts: 13
I have a jsp that contains a name (populated from database) and a blank email field. When the user presses the submit button, I validate the email and send it back to the jsp page if it fails.

How am I supposed to redisplay the name? I have been placing it into a session variable, but is that the only/best way? What if I have a lot of variables (name, address, phone, arrays)?


The entire app:
1. jsp -- User enters ID
2. action -- grab name from database based on ID and place in session variable
3. jsp -- display name and ask for email
4. action -- validate email
5. jsp -- display name (problem here), ask for email, display validation error
[ August 09, 2005: Message edited by: David Martin ]
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Keeping the name in session is not better idea if it is just required to display back. If you want to have this variable thru out application in different pages you can keep it in session. Other wise just use HttpRequest method getAttribute() and setAttribute() to share the name.

If you have more than one variable then you can create a java bean classes with all properties of name, address, phone. And share that java bean between the pages.

Hope it helps you.
[ August 09, 2005: Message edited by: KJ Reddy ]
David Ogasawara
Greenhorn

Joined: Oct 11, 2001
Posts: 13
Thank you for the response! Unfortunately, using HttpRequest does not work. I can initially get it to display the name in the jsp, but after a failed validation, the redisplayed jsp is blank.


EmailAction: get name from database
email.jsp: display name, email field (form action="email")
EmailAction: validate emailAddress (failed) ---> mapping.findForward("failure")
XML: <forward name="failure" path="/email.jsp"/>
email.jsp: name not displayed, email field, error message


I will continue to experiment.
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

When you display (as in text-only) a variable like that, you need to also set it with a html:hidden tag to propogate across validation.


A good workman is known by his tools.
David Ogasawara
Greenhorn

Joined: Oct 11, 2001
Posts: 13
So that's how you do it!!!

THANK YOU!!!

I'm going to print the page and nail it to my wall.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Redisplay Variables in JSP after Failed Validation?