Here is some excellent example code that Geeta posted a few days ago....
It should give you all the info you need.
(P.S. Thanks Geeta.)

<Chop'n'pasted more or less identically>
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.


----------------------------------------------------------------------------
-----------------------------

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();
 }

}


***********************************************************************
Privileged/confidential information may be contained in this message.
If you are not the addressee indicated in this message (or responsible
for delivery of the message to such person), you may not copy or
deliver this message to anyone. In such case, you should destroy this
message and notify the sender and [EMAIL PROTECTED]
immediately.

If you or your employer do not consent to Internet E-mail messages of
this kind, please advise us immediately.

Opinions, conclusions and other information expressed in this message
 (including any attachments) are not given or endorsed by ebeon ltd
 (or ebeon inc., as applicable) unless otherwise confirmed in writing
by an authorised representative independent of this message. Any
liability arising from reliance placed on this message (including its
attachments) without such independent confirmation is hereby excluded.

This message (including attachments) is protected by copyright laws
but has no other legal or contractual standing. The presence of this
footnote indicates that this message (including its attachments) has
been processed by an automated anti-virus system; however it is the
responsiblity of the recipient to ensure that the message (and
attachments) are safe and authorised for use in their environment.
***********************************************************************

___________________________________________________________________________
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