• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with getRequestDispatcher()'s forward. Any alternatives?

 
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was using the method:
getRequestDispatcher( "/page.jsp" ).forward( req, res );
The problem is that then when it sends to the browser, the address says the name of my servlet, not the name of my jsp page that I just forwarded to. The problem with that: when I now submit, the header "Referer" has the name of my servlet NOT the name of the jsp page that they came from!! How can I "redirect" to a jsp page (while sending the request and response objects that I altered in my servlet) AND have it actually go to the jsp page?
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I faced the same problem while forwarding to calling JSP thro. controller servlet.
There might be better ways of doing this but workaround I used was to pass a hidden variable from each page while submitting to controller.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you're asking. If a request is made for your Servlet, then you forward the request to a JSP, the request IS actually going to the JSP page.
What are you doing with the value of the "Referer" header that it needs to be the JSP? Maybe there's an alternate design that can be used.
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when I forward to "/my.jsp" it happens only server-side. So the browser still sees it as "/MyServlet" when it returns. This is because originally the form's action was "/MyServlet" (i.e. it posted to the servlet). Now what happens is this: if an error occurred, I'd like to redirect back to the form that posted to this servlet with error code. This is because I'm making a loosely coupled servlet that can handle any form that needs to post. So I can't hardcode the page that i forward to from the servlet.
So the problem is:
1. my servlet processes the first request
2. it gets an error and forwards to the refering page with error notices
3. the page that has the form now with error notices is fixed by the user and resubmitted
4. more errors, need to forward to refering page BUT AN ERROR: the refering page is now the servlet itself!!
How do i solve this?
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hopefully someone will come up with the real answer. In the meantime...
Well, it ain't pretty or clean, but my current work-around is that whatever form wants to have my servlet handle the post, will include this code:
<%
//WAS THIS A POST TO THIS PAGE
if ( request.getMethod().equalsIgnoreCase( "POST" ) )
getServletConfig().getServletContext().getRequestDispatcher( "/HandleFormServlet" ).include( request, response );
%>
The form posts to itself now instead of posting to the servlet, like this:
<form action="/form.jsp" method="POST" >
It used to be:
<form action="/HandleFormServlet" method="POST" >
And now if errors occur, no redirecting needs to occur, it stays on the page that started the whole thing and that uses the error-handling bean that the servlet usually passes.
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a problem with using .sendRedirect() ?

This is "like" forwarding, but it's the client-side version of it. In other words, the address bar updates.
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I put Attributes into the request object (a form validator holding any error messages, etc). If I use sendRedirect, that's not accessible.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could have all the forms submit their actual URI in a hidden field and use that URI to forward the request back to the form.
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I know that. But that's NOT clean. The form shouldn't have to care about that type of stuff - that's making the Form Designer do what the HTTP server does - i.e. send referer info. I refuse to do that.
But thanks for the suggestion. I did think of going that route at first (and have in the past) but it's messy. Causes more problems than it solves in the long run.
 
Bhupinder Dhillon
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Robert Paris:
Oh, I know that. But that's NOT clean. The form shouldn't have to care about that type of stuff


True enough, but then again you shouldn't be creating a generic form handler which validates form fields for basically any type of form. BTW, how are you determining which fields need to validated on a particular form and what the content should be for a specific field?
[ January 29, 2003: Message edited by: Bhupinder Dhillon ]
[ January 29, 2003: Message edited by: Bhupinder Dhillon ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic