Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

request URL reset to forwarded servlet  RSS feed

 
Arun Somasundaram
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the request is forwarded from one servlet to another servlet the request URI is set to the forwarded servlet. i.e, when request.getRequestURL() in the forwarded servlet returns the forwarded servlet's URL and not the URL of the servlet which forwards the request.Why it is so?this can happen when one servlet redirects the client to another servlet but it too happens when the request is forwarded.The request is forwarded and not redirected.Even though the actual requested URL is got through
request.getAttribute("javax.servlet.forward.request_uri"); in the forwarded servlet
why it is not got through request.getRequestURL()? why the request URL is set to the forwarded servlet.

sample code for the above mentioned problem is given below:-
Code for Servlet_Forward1.java



Code for Servlet_Forward2.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class Servlet_Forward2 extends HttpServlet
{

public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<html>");
out.println("<head>");
out.println("<title>Forwarded Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<center>");
out.println("<br><br>");
out.println("<h2>Actual Requested URL" + request.getRequestURL() + "</h2>");
out.println("</center>");
out.println("</body>");
out.println("</html>");
out.close();

}
}

Output Rendered by the browser:-

Actual Requested URLhttp://sun/jsp-practice/Servlet_Forward2
[ August 04, 2006: Message edited by: Arun Somasundaram ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this has to do with the following specification :

SRV.8.4 The Forward Method
The path elements of the request object exposed to the target servlet must
reflect the path used to obtain the RequestDispatcher.


So the path retrieved via getRequestURI will refer to the path used to obtain the dispatcher.
 
Arun Somasundaram
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Satou kurinosuke,but could you post the links related to the specification so that it is easy to get clear with the whole process.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Google for "servlet specification"
[ August 04, 2006: Message edited by: Ulf Dittmer ]
 
Arun Somasundaram
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really got confused when I saw SRV.8.4 .Initially I thought it was some specification.Now only I got clear.I think you have made mistake in typing.
I think it is SERVLET 2.4 .
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Somasundaram:
I think you have made mistake in typing.
I think it is SERVLET 2.4 .



The link is in my signature.
The sections in the servlet spec are labeled SRV.n.n

Example:

SRV.8.1 Obtaining a RequestDispatcher
An object implementing the RequestDispatcher interface may be obtained from
the ServletContext via the following methods:
� getRequestDispatcher
� getNamedDispatcher


As is typical with Satou's answers, this one was well thought out, researched, and typed correctly.
[ August 04, 2006: Message edited by: Ben Souther ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!