wood burning stoves 2.0
The moose likes Servlets and the fly likes RequestDispatching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "RequestDispatching" Watch "RequestDispatching" New topic


Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
Hello Everybody,
Can anybody give me a code for the following purpose:
I want to forward a request from Servlet to JSP using RequestDispatcher interface's forward method.problem i am facing is how to give a url(either absolute or relative for JSP).

Extra Info: I am using Weblogic 6.1 where i have copied Servlet .class files in classes folder, whereas JSP is located in DefaultWebApp.

thanx in advance
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Check out ServletContext.getRequestDispatcher() and getNamedDispatcher()

Heonkoo Lee
Ranch Hand

Joined: Feb 10, 2005
Posts: 85
It depends on the servlet url path defined in web.xml. Say, your servlet is called by http://server.com/MyApp/MyServlet. Then from the servlet you can forward to a JSP like:

// Use the relative path from the servlet.
RequestDispatcher rd = request.getRequestDispatcher("Result.jsp");
rd.forward(request, response);

// If servlet uri is http://server.com/MyApp/someDir/MyServlet and jsp is right under web root directory,
// by using relative path from servlet
RequestDispatcher rd = request.getRequestDispatcher("../Result.jsp");
// by using relative path from host server
RequestDispatcher rd = request.getRequestDispatcher("/MyApp/Result.jsp");
rd.forward(request, response);

Hope this helps.
[ April 03, 2005: Message edited by: Heonkoo Lee ]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

If you want an example, go get SimpleMVC from http://simple.souther.us

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link: http://aspose.com/file-tools
subject: RequestDispatching
It's not a secret anymore!