Sure, here goes..
---------------------------------------------------------------------------------------------------------

Code for RDTest.html:
<HTML>
<BODY>
<FORM ACTION="/servlet/TestReqDispatcher" METHOD="POST">
<CENTER>Testing Request Dispatcher Object</CENTER>
<INPUT TYPE="TEXT" NAME="name" VALUE="Geeta">
<INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="Try this!">
</FORM>
</BODY>
</HTML>
---------------------------------------------------------------------------------------------------------

Code for TestReqDispatcher.java:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TestReqDispatcher extends HttpServlet
{
 public String getServletInfo()
 {
  return "Testing the way a request dispatcher works.";
 }

 public void doGet(HttpServletRequest req, HttpServletResponse resp)
 throws ServletException, IOException
 {
  System.out.println("In doGet..");
  ServletContext sc = this.getServletContext();
  if (sc == null) System.out.println("Cannot get servlet context!");
  else {
      System.out.println("Just got servlet context..");
      try{
          RequestDispatcher rd =
sc.getRequestDispatcher("/servlet/SecondServlet");
          if (rd == null) System.out.println("Cannot get requst dispatcher!");
          else {
              System.out.println("Request dispatcher is non null.. Going to
forward.");
              rd.forward(req, resp);
          }
      }
      catch(Exception e){
          e.printStackTrace();
          System.out.println("In TestReqDispatcher.." + e);
      }

  }
 }

 public void doPost(HttpServletRequest req, HttpServletResponse resp)
 throws ServletException, IOException
 {
  System.out.println("In doPost of First Servlet..");
  ServletContext sc = this.getServletContext();
  if (sc == null) System.out.println("Cannot get servlet context!");
  else {
      System.out.println("Just got servlet context..");
      try{
          RequestDispatcher rd =
sc.getRequestDispatcher("/servlet/SecondServlet");
          if (rd == null) System.out.println("Cannot get requst dispatcher!");
          else {
              System.out.println("Request dispatcher is non null.. Going to
forward.");
              rd.forward(req, resp);
          }
      }
      catch(Exception e){
          e.printStackTrace();
          System.out.println("In TestReqDispatcher.." + e);
      }

  }
 }

}
---------------------------------------------------------------------------------------------------------

Code for SecondServlet.java:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SecondServlet extends HttpServlet
{
 public String getServletInfo()
 {
  return "Testing the way a request dispatcher works: this is the forwarded
servlet.";
 }

 public void doGet(HttpServletRequest req, HttpServletResponse resp)
 throws ServletException, IOException
 {
  System.out.println("In doGet of SecondServlet..");
  resp.setContentType("text/html");
  PrintWriter out = new PrintWriter(resp.getOutputStream());

  // to do: code goes here.

  out.println("<HTML>");
  out.println("<HEAD><TITLE>TestReqDispatcher Output</TITLE></HEAD>");
  out.println("<BODY>");

  out.println("In doGet of SecondServlet<P>");
  if (req.getParameter("name") != null)
      out.println("Name parameter is " + req.getParameter("name"));
  else out.println("Use queryString var if you want a param value with a Get!");

  out.println("</BODY>");
  out.println("</HTML>");
  out.close();

 }

 public void doPost(HttpServletRequest req, HttpServletResponse resp)
 throws ServletException, IOException
 {
     System.out.println("In doPost of SecondServlet..");
  resp.setContentType("text/html");
  PrintWriter out = new PrintWriter(resp.getOutputStream());

  // to do: code goes here.

  out.println("<HTML>");
  out.println("<HEAD><TITLE>TestReqDispatcher Output</TITLE></HEAD>");
  out.println("<BODY>");

  out.println("In doPost of SecondServlet..<P>");
        out.println("Val of name parameter is " + req.getParameter("name"));
  out.println("</BODY>");
  out.println("</HTML>");
  out.close();
 }

}
---------------------------------------------------------------------------------------------------------

At the risk of stating the obvious, here are some "notes":
After compiling, setting the classpath and other such boring details, bring up
the RDTest.html. And submit it. The first servlet's doPost will be executed,
which will result in the second servlet's doPost being executed, and the value
of the "name" parameter will be displayed. On the other hand, call
TestReqDispatcher directly via a url (something like
http://www.myServer.com/servlet/TestReqDispatcher) as well as a url with a
querystring var with name "name" (as in
http://www.myServer.com/servlet/TestReqDispatcher?name=Alec). In both cases, the
doGet of the first servlet will be executed, which will result in the doGet
SecondServlet being executed.

Geeta

Alec Belsky wrote:

> i would be interested in looking at the code
> why don't you post to the group
> thanks
>
> > Anshu:
> >
> > Is there any particular reason why you decided to use requestDispatcher
> and not
> > Response.sendRedirect()? Because one usually uses the
> > RequestDispatcher in the case of wanting to forward the request, not just
> to
> > redirect to another url.. I have some trivial code i wrote over the
> weekend
> > which illustrates the use of RequestDispatcher, which I will be happy to
> send to
> > you if you would like.
> >
> > Sorry I haven't actually answered your question..
> > Geeta
> >
> > "Garg, Anshu (CAP, RAIL)" wrote:
> >
> > > I invoke a servlet from an HTML page using method = "POST" and ACTION =
> "URL
> > > to the servlet1". It works fine.
> > >
> > > Now suppose this servlet forwards the request to second HTML page (using
> > > RequestDispathcher). The HTML page invokes second servlet using method =
> > > "POST" and ACTION = "URL to the servlet2". It does not work. The browser
> > > gives the error message "405 Method not allowed".
> > >
> > > If I change method from POST to GET, it works fine.
> > >
> > > Can you throw some light as to why this is happening.
> > >
> > > Anshu Garg
> > >
> > >
> ___________________________________________________________________________
> > > To unsubscribe, send email to [EMAIL PROTECTED] and include in the
> body
> > > of the message "signoff SERVLET-INTEREST".
> > >
> > > Archives: http://archives.java.sun.com/archives/servlet-interest.html
> > > Resources: http://java.sun.com/products/servlet/external-resources.html
> > > LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
> >
> >
> ___________________________________________________________________________
> > To unsubscribe, send email to [EMAIL PROTECTED] and include in the
> body
> > of the message "signoff SERVLET-INTEREST".
> >
> > Archives: http://archives.java.sun.com/archives/servlet-interest.html
> > Resources: http://java.sun.com/products/servlet/external-resources.html
> > LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
> >
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to