Hi,
Can anyone help me in understanding the function of RequestDispatcher.
=======================================================================
First Servlet: RdTest1
=======================================================================
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class RdTest1 extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
// Creating printerwriter object
PrintWriter out = res.getWriter();
// Set content type and other response header fields first
res.setContentType("text/HTML");
// Building page
out.println("<HTML>");
out.println("<BODY>");
out.println("<FORM name=\"rdForm\" action=\"/servlet/RdTest2\"
method=\"post\">");
out.println("<CENTER><H3>URL Redirection - Request
Dispatcher</H3></CENTER>");
out.println("<BR><BR><BR><BR><BR>");
out.println("Content from RdTest1");
out.println("<BR><BR><BR><BR><BR>");
out.println("<INPUT TYPE=\"SUBMIT\">");
out.println("</FORM>");
out.println("</BODY>");
out.println("</HTML>");
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
doGet(req, res);
}
}
=======================================================================
Secound Servlet: RdTest2
=======================================================================
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class RdTest2 extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
doPost(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
// Creating printerwriter object
PrintWriter out = res.getWriter();
// Set content type and other response header fields first
res.setContentType("text/HTML");
out.println("Content from RdTest2");
//res.sendRedirect("/servlet/UrlRed1");
RequestDispatcher rd = null;
rd =
getServletContext().getRequestDispatcher("/servlet/RdTest1");
rd.forward(req, res);
}
}
======================================================================
The flow is: First servlet (RdTest1) is called from browser, on pressing
submit button, it will call secound servlet (RdTest2) which will forward
that request again to first servlet (RdTest1) using RequestDispather
service.
The output which I am seeing is merged output of RdTest2 and RdTest1. On
browser, the url indicates the second servlet:
http://localhost/servlet/RdTest2
Here which servlet is having control, RdTest1 or RdTest2? Also if
anybody can help me understanding the difference between forward and
include, it will be highly appreciated.
==========
OUTPUT
==========
Content from RdTest2
<HTML>
<BODY>
<FORM name="rdForm" action="/servlet/RdTest2" method="post">
<CENTER><H3>URL Redirection - Request Dispatcher</H3></CENTER>
<BR><BR><BR><BR><BR>
Content from RdTest1
<BR><BR><BR><BR><BR>
<INPUT TYPE="SUBMIT">
</FORM>
</BODY>
</HTML>
======================================================================
Thanks in advance.
Sushil
___________________________________________________________________________
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