Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sendRedirect or RequestDispatcher forward - error

 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

So...

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?

KS
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keith,
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65220
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might find this article helpful.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic