Hi folks;
As it turns out, i *have* already posted the code to the list serve, but I think
it may have been missed because i had replied to a post which had no subject.
Since there seems to be some interest in this, i am posting this again;
apologies in advance for the repost. 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 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();
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
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);
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
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=Cedric
In both cases, the doGet of the first servlet will be executed, which will
result in the doGet SecondServlet being executed.
Hope this helps,
Geeta
___________________________________________________________________________
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