• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

request URL reset to forwarded servlet

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Google for "servlet specification"
[ August 04, 2006: Message edited by: Ulf Dittmer ]
 
Arun Somasundaram
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic