This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Servlets and the fly likes sendRedirect or RequestDispatcher forward - error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "sendRedirect or RequestDispatcher forward - error" Watch "sendRedirect or RequestDispatcher forward - error" New topic

sendRedirect or RequestDispatcher forward - error

Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Hi guys

Having a little trouble deciding the best way around this issue I'm having with page navigation through Servlets...

To try to hide the URLs (path to the JSPs) for each part of my system I have decided to make all page navigation a Servlet call (PageLoader - Servlet), this gets an initParam from the web.xml with the JSP location and passes it into a RequestDispatcher.forward.

Now, everything is fine for the most part... Except!
Because requestDispatcher is a server side call, as you know if you refresh (F5) your browser window, the App will 'run' the Servlet again.

this causes a big issue if you are coming from say a CreateUserAccountServet, because when the user account is created and the .forward sends you to your /myaccount page, you hit refresh (F5) and the App will run the CreateUserAccountServlet again and try to create another new account!!! PROBLEM

So I decided to move to .sendRedirect. This is fine expect, because this is a browser call, it replaces the /myaccount (Servlet URL) with the full JSP path from the web.xml (eg: /secure/pages/account/index/myaccount.jsp) this is not what I want.


Is there a way of either stopping RequestDispatcher from re-running the Servlet when you refresh the page or is there a way of hiding the JSP url when using .sendRedirect?

Any help, please?

Ben Souther

Joined: Dec 11, 2004
Posts: 13410

We have a forum dedicated to Servlets.

To answer your question, yes, look up the Post-Redirect-Get pattern.
This technique insures that the rendering of your screens is never the result of a post request.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63227

You might find this article helpful.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Another technique I've been playing with lately is replacing form submissions with AJAX calls. Once the call has been made, if the response contains the correct status, I refresh or redirect the page with Javascript.
This is another way of insuring that no screens are ever drawn as the result of request of type 'POST'.

One downside to this approach is, older browsers might not support it.
S Reddy
Ranch Hand

Joined: May 17, 2007
Posts: 45
If you don't want to show actual path to the user, you can map JSPs in web.xml same as you do with servlets.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Srikanth Reddy Lankireddy:
If you don't want to show actual path to the user, you can map JSPs in web.xml same as you do with servlets.

The original poster's question had more to do with preventing double submission of forms when page is reloaded or backed into.
Mapping JSPs will allow you to change the URLs for them but will do nothing to prevent this issue.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: sendRedirect or RequestDispatcher forward - error
jQuery in Action, 3rd edition