This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes error page forward Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "error page forward" Watch "error page forward" New topic
Author

error page forward

ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 374
I have several forms, each has its validation logics for inputs, suppose in one of the middle forms there is anything wrong in the input, I want to direct user to an error page, but I don't want them to lose what they have done up to that point === in other words on the error page I want them to click browser's back button and go back to the last form which o they paused.

In this case, will a "forward" work for me ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

The browsers back button doesn't give you any opportunity to run code at all.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Prasad Tamirisa
Ranch Hand

Joined: Mar 26, 2007
Posts: 130
in other words on the error page I want them to click browser's back button and go back to the last form which o they paused.


I don't think this works, because when you click on the browsers back button the form will be reset and so you will see all the values blank.


Regards,
Durga Prasad
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

A better an more typical way to handle this is to return the user to the form that they've just submitted (with the form still filled out).
Have that page display an error message letting them know which fields have problems. This way they don't have to click the back button to the to the form and they'll have the error message on the same page as the form fields.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 374
Hi, I guess I didn't express it clearly. It is OK for me to have all fields appear blank. What I want is --- Suppose I am on the third form (each fomr is a separate page) and there is some error, I want it to direct to a standard error page, and on the error page, when I click BACK button, I want come back to the third page (it is fine to show me a blank third form). Does "forward" work for this ?

I appreciate there is better way to handle the error, but still want to get the answer to above question.

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Again, when the back button is clicked, you have no opportunity to run any code, so asking what code to use is pointless.

Rather, let's back up a bit and examine what you are doing. Sending the user to a separate error page is not only poor UI, it adds levels of complexity that you are stumbling over. Why not do something thats better UI, more conventional, and easier to control?

Rather than sending them to another page when you detect an error with the form data, most web apps will send the user back to the same form page with the error message embedded on the page itself. This is not only more user-friendly, but eliminates any nonsense with the back button.
Prasad Tamirisa
Ranch Hand

Joined: Mar 26, 2007
Posts: 130
Rather than sending them to another page when you detect an error with the form data, most web apps will send the user back to the same form page with the error message embedded on the page itself. This is not only more user-friendly, but eliminates any nonsense with the back button.


This how we generally do in the real time situation.
[ May 27, 2008: Message edited by: Prasad Tamirisa ]
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 374
Originally posted by Prasad Tamirisa:


I don't think this works, because when you click on the browsers back button the form will be reset and so you will see all the values blank.


Now I understand the back button does not work. But in servlet/JSP, once it is forwarded to the error page, is there a way for user to go back to the previous page that triggered the error without losing other info already saved in session (it is ok to view that page as blank) ? what api method can do that ? Again, I know there is better solution but just want to know if it is possible.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Originally posted by ben oliver:
what api method can do that ?

If you are expecting a method that can magically remember data that that is no longer available, you will be disappointed. Failing to follow the earlier advice, you'll need to store the data away in the session and retrieve it later when you want to repopulate the page.
Prasad Tamirisa
Ranch Hand

Joined: Mar 26, 2007
Posts: 130
The Best solution that i suggest in this case is,

When some thing goes wrong, redirect the user to the same page with the corresponding error message displayed above the text box.

You can do this using the struts error tags.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Originally posted by Prasad Tamirisa:
You can do this using the struts error tags.
Please do not assume people posting in the JSP forum are using Struts.
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 374
Now gentlemen, when some error occurs, can I stay on the same page (redisplay the current form, suppose it is the 3rd form, stay on it and add error message, the fields can be blank though) instead of going to a separate error page ? If that's the case I guess I will not lose the data I entered in the first 2 forms, correct ?

What method shall I use in this kind forward ?

And Yes, please don't assume we are using struts.

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

If you are collecting information from multiple forms, you're going to need to store it on the server or propagate from page to page using hidden elements. Most people would collect this information in the session and it will available to populate the various forms with the data.

And yes, not going to separate error page would be vastly preferred over showing the errors in-place.
Prasad Tamirisa
Ranch Hand

Joined: Mar 26, 2007
Posts: 130
Originally posted by Bear Bibeault:
Please do not assume people posting in the JSP forum are using Struts.


Thanks Bear.

:thumb:
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 374
Originally posted by Bear Bibeault:
If you are collecting information from multiple forms, you're going to need to store it on the server or propagate from page to page using hidden elements. Most people would collect this information in the session and it will available to populate the various forms with the data.

And yes, not going to separate error page would be vastly preferred over showing the errors in-place.


Bear (your name is so close to another moderator who is also on these forum..), thanks for reminding me of the hidden field. What I am thinking is --

Suppose I am on the 3rd form, after I click submit button, if the server detects some error, let's say I want it to re-display the 3rd form to user and blank out all the fields he has entered (reset). Can I do something like

..getDispather(request.getRequestURL().toString()).forward(request, response)

But I guess at this point, since I have clicked submit, it invokes the next action, so request.getRequestURL() should return me with the NEXT URL, so it will NOT forward back to the current 3rd form. DO I understand it correctly ? If so, how should I code to let it forward "back" to current form ?

Plus, for hidden fields, suppose I have hidden fields carried from 1st form, will this forward loose the hidden field values ?

Thanks.
 
Consider Paul's rocket mass heater.
 
subject: error page forward
 
Similar Threads
show different content after a choice in a combobox
Setting data to more than one form
Unix Authentication by using servlet
disable form element when an item is selected
static JTextField problem!