Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

include or forward thru RequestDispatcher

 
Gaurav Chugh
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
i have one servlet ,i want to forward to another servlet , i am doing in this way .
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
RequestDispatcher rd = request.getRequestDispatcher("/SnoopServlet");
if(request.getParameter("snoop").equals("SnoopServlet"))
rd.forward(request,response);
the SnoopServlet lies in the same directory as the calling servlet and i am getting the parameter of snoop but still its not forwarding it .what cud be the problem .shud i give the complete path as "examples/servelet/SnoopServlet" and i wanna to know that " is same thing is valid for including the servlet also " and plz clear me wahts difference in the follwing lines
RequestDispatcher rd = getServletContext().getRequestDispatcher("java.lang.String");
and
RequestDispatcher rd = request.getRequestDispatcher("java.lang.String");
Thanks in advance

 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only call response.getWriter(); once on any response Object. If you wish to use forward, you must do it prior to this call.
------------------
I Hope This Helps
Carl Trusiak, SCJP2
 
Gaurav Chugh
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi carl,
u mean to say i will modify my code
rd.forward(request,response);
PrintWriter out = response.getWriter();
in this way but it does not work out , whats cud be the problem and i tried with both (web context and request object ) but its not working , its not fwding the request.the same thing i tried for include also but the same thing is happening there too.
thanks in advance

------------------
with warm regards
..gaurav
 
Rama Raghavan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Refer to Ken's notes on getRequestDispatcher.
servletRequest.getRequestDispatcher(url) takes a relative path of the location of the request, while method in ServletContext the pathname must begin with "/" and interpreted relative to the root of the webapp.
So I'm guessing based on the location of the servlet, the following should work (without leading "/") -
request.getRequestDispatcher("SnoopServlet");
Hope this helps -
------------------
_________________________
Rama Raghavan
SCJP2
 
Gaurav Chugh
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
Thanks Rama , now its working fine , but i want to tell u it does not work without leading "/" with HttpServletRequest object and we have to specify the relative path to webapp.I tried with ServletContext object and HttpServletRequest object. In both ,we have to use "/" otherwise it throw java.lang.StackOverFlowError.Correct me if i am wrong .
------------------
with warm regards
..gaurav
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic