File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes requestDispatcher.forward() throws error when forwarded to #topic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "requestDispatcher.forward() throws error when forwarded to #topic" Watch "requestDispatcher.forward() throws error when forwarded to #topic" New topic
Author

requestDispatcher.forward() throws error when forwarded to #topic

Jeya Balaji
Ranch Hand

Joined: Jan 02, 2003
Posts: 40
Hi friends,
From a dispatcher servlet, I am forwarding the control to a .jsp page. It is working fine if just the jsp page is given. However, the forward fails when #topic is attached to the url.

I am getting this error :
HTTP Status 404 - /spectator/sample.jsp#issue
type Status report
message /spectator/sample.jsp#issue
description The requested resource (/spectator/sample.jsp#issue) is not available.

I when I copied 'sample.jsp#issue' from the error message and pasted in the url, I am able to view the correct topic in sample.jsp
I tried leaving a space between sample.jsp and '#topic'. That didn't help.
Couldn't get much info from servlet api.
How do I forward to a page and get to the #topic in that page?
Thanks,
Balaji
[ January 28, 2003: Message edited by: Jeya Balaji ]

Regards, Balaji
Mark Howard
Ranch Hand

Joined: Feb 14, 2001
Posts: 285
Hi Jeya
Don't know too much about this, but my impression of a RequestDispatcher path is that it must be a real pathname to an actual resource within the web application.
As such, I wonder whether the Servlet container understands the # symbol in your RequestDispatcher path? Is not the # unique to HTML? Perhaps you could control it in another way (using JavaScript possibly?)
Sorry this isn't much help
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi,
can i find logical explanation for this anywhere? i tried this. forward() and include() used via RequestDispatcher doesn't allow use of '#' where as if we do response.sendRedirect() then it works...
what can be the reason for not supporting '#' in RequestDispatcher?
thanks
maulin.
Jignesh Malavia
Author
Ranch Hand

Joined: May 18, 2001
Posts: 81
The # character is used by the browser to scroll down to the named link <a name='topic'> after the page is loaded. The # character is not part of the query string. To make it clear, consider this:
Two JSP pages, X.jsp and Y.jsp.
X.jsp forwards the request to Y.jsp
Y.jsp generates HTML and one of the tags in the returned HTML is an anchor named topic.
If you type http://.../Y.jsp you get the whole page with the document positioned at the top in the browser.
If you type http://.../Y.jsp#topic you get the whole page, but the browser scrolls down upto the anchor named topic. Here, # is not sent as part of the URL. The servlet container does not know anything about it. If you do System.out.println(request.getRequestURL()); in Y.jsp, it will not print #topic at the end.
Now, if you type http://.../X.jsp you get the output of Y.jsp as expected.
But, if you type http://.../X.jsp#topic the X.jsp page forwards the request to Y.jsp as usual, you get the output of Y.jsp as usual, and the browser scrolls down to the anchor named topic. Again, in this case too, the servlet container is unaware of the #topic at the end of the URL.
As a rule, just remember that on the server side, you may use two, three, or any number of components includ()ing or forward()ing each other. But the # character is applied at the client side on the final output of the original URL as seen by the browser.
Hope that helps
-j
Dharmin Desai
Ranch Hand

Joined: Feb 28, 2002
Posts: 81
A nice answer, jignesh.
- Dharmin


SCJP2 (93%),SCWCD(88%)<br />-------------------------------<br />Never under estimate yr self, just represent yr profile in proper manner.
Dharmin Desai
Ranch Hand

Joined: Feb 28, 2002
Posts: 81
A nice answer, jignesh.
- Dharmin
Jeya Balaji
Ranch Hand

Joined: Jan 02, 2003
Posts: 40
Jignesh, by your explanation, I understand that it is not possible to make the browser point to a particular topic (thru java calls). Is that right?

Another observation related to this discussion :
Advantage I see in using response.sendRedirect() instead of requestDispatcher.forward() is :
After redirecting, the browser shows the destination url with sendRedirect()
whereas, rd.forward() shows the url to which the previous page forwards.
for eg, if sample.jsp calls a servlet named process and the servlet redirects to result.jsp,
when redirect() is used, the browser shows http://<server>/<context>/result.jsp in the url
if forward() is used, the browser shows http://<server>/<context>/process in the url
Are there any issues with using response.sendRedirect() instead of requestDispatcher.forward()? Can I safely use sendRedirect()?
Regards,
Balaji
[ January 29, 2003: Message edited by: Jeya Balaji ]
Jignesh Malavia
Author
Ranch Hand

Joined: May 18, 2001
Posts: 81
Originally posted by Jeya Balaji:
Jignesh, by your explanation, I understand that it is not possible to make the browser point to a particular topic (thru java calls). Is that right?

It is not possibe from the Java code of the dispatcher servlet the way you are trying. What you can do though is when you generate the previous HTML page, the page that has a link pointing to the Dispatcher Servlet, you can append #topic in the URLs of that dispatcher servlet itself.
Some thing like this:

Let the dispatcher servlet then forward the request to forwardpage.jsp without using #. When the browser receives the response it will scroll down to the topic that was clicked (provided the returned page contains the corresponding <a name='topic'> tag.
Jignesh Malavia
Author
Ranch Hand

Joined: May 18, 2001
Posts: 81
Originally posted by Jeya Balaji:
Advantage I see in using response.sendRedirect() instead of requestDispatcher.forward() is :
After redirecting, the browser shows the destination url with sendRedirect()
whereas, rd.forward() shows the url to which the previous page forwards.
for eg, if sample.jsp calls a servlet named process and the servlet redirects to result.jsp,
when redirect() is used, the browser shows http://<server>/<context>/result.jsp in the url
if forward() is used, the browser shows http://<server>/<context>/process in the url
Are there any issues with using response.sendRedirect() instead of requestDispatcher.forward()? Can I safely use sendRedirect()?

SendRedirect() and RequestDispatcher.forward() are two different things for different purposes and they cannot be substituted for one another. Which one to use depends on the requirements and the URL shown in the browser's address bar should not be a deciding factor.
-j
Jeya Balaji
Ranch Hand

Joined: Jan 02, 2003
Posts: 40
Jignesh, after appending #topic after dispatcher made the browser scroll as expected. Thanks.

However, I failed to see the difference between sendRedirect() and rd.forward() (after going thru the api).
In my case, control flows like this :
* sample.jsp invokes dispatcher (thru get or post)
* dispatcher does couple of house-keeping
* then dispatcher passes control back to sample.jsp
* sample.jsp displays new content based on the action performed by dispatcher
Here, I use sendRedirect (or rd.forward) for the control to go back from dispatcher to sample.jsp
So, what is the right method to pass control in this situation?
Regards,
Balaji
Sudd Ghosh
Ranch Hand

Joined: Oct 23, 2002
Posts: 187
Jeya -
Theoretically, I know the following differences between HttpServletResponse.sendRedirect() and RequestDispatcher.forward() methods:
1. sendRedirect() works on the client side, that is why you can see the redirected URL on the browser. forward() happens on the server side.
2. sendRedirect() can take an absolute URL (starting with protocol) as its parameter, whereas forward() can accept ONLY webapp root relative or non-root relative path (this will differ depending on if the RequestDispatcher is obtained from ServletContext or HttpServletRequest). Anyway, forward() cannot accept an absolute URL.
3. forward() is preferred if you don't want to display the forwarded path on the browser.
Thanks, Sudd


SCJP 1.4, SCWCD, SCBCD 1.3
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: requestDispatcher.forward() throws error when forwarded to #topic