Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes Problem with getRequestDispatcher()'s forward. Any alternatives? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem with getRequestDispatcher() Watch "Problem with getRequestDispatcher() New topic
Author

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

Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
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?
Bhushan Jawle
Ranch Hand

Joined: Nov 22, 2001
Posts: 249
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.
Rich Raposa
Ranch Hand

Joined: Dec 06, 2001
Posts: 46
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.


Rich Raposa<br /><a href="http://www.javalicense.com" target="_blank" rel="nofollow">http://www.javalicense.com</a><br />Buy Java courseware -> get a free XBox!
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
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

Joined: Jul 28, 2002
Posts: 585
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.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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

Joined: Jul 28, 2002
Posts: 585
Yeah, I put Attributes into the request object (a form validator holding any error messages, etc). If I use sendRedirect, that's not accessible.
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
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

Joined: Jul 28, 2002
Posts: 585
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

Joined: Oct 12, 2000
Posts: 124
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with getRequestDispatcher()'s forward. Any alternatives?
 
Similar Threads
Internal Server 500 error: No context configured to process this request
Interesting problem in Forwarding request dispatcher
problem with using URIs as parameters
Hide action name in browser address bar?
requestdispatcher stuff