Hi,

I think I can explain forward and include using the following cases.

Let say there are 2 servlet A and B.

If A forwards request to B, the control will first go to A and then B. Servlet A
does not generate any output to the output stream. All output is generated by
Servlet B (including <html>....</html> in case of html generation).

Browser -> A (do some processing) -> B (do something and generate output) ->
Browser

If A includes B, the control will first go to A, then B and back to A again.
Servlet A will generate the some output and then B will generate some other
output. After B has completed, the control will go back to A and A may produce
some more output. B will only generate fragment of the output.

Browser -> A (generate some output) -> B (generate some output) -> A (generate
some output) -> Browser

In my experience, I usually use include to generate common part (using B servlet
in the example) of my pages like header and footer.

Best regards,
Jacky


Sushil Singh wrote:

> Hi,
>
> I am again posting this question in shorter, please help me.
>
> I am having one servlet "Servlet 1" which displays a form, on form submit, it
> calls another servlet "Servlet 2" which will do some process and then redirects
> to servlet 1 using RequestDispatcher.
> 1.   Before redirecting to servlet 1, servlet 2 should produce html which will
> display a popup window.
> 2.   Its perfoming perfectly i.e. it redirects to servlet 1 and display a popup
> window. The only thing I am unable to understand is that still the url on
> browser is Servlet 2, which I think should be Servlet 1.
> 3.   I can not use "response.sendRedirect()" since my requirement is to
> redirect and display a popup window , which will give impression that this
> popup winodow is displayed by Servlet 1.
> This type of scnerio is required when dealing with multiple websites. Here you
> can assume that "Servlet 1" is on one website and "Servlet 2" on another
> website.
>
> Also if anyone can provide difference between forward and include.
>
> Thanks in advance.
>
> Sushil
>
> Sushil Singh wrote:
>
> > 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
>
> ===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to