Ad Home

Theme images by kelvinjay. Powered by Blogger.

Travel

Header Ads

Java

Selenium

UFT

Framework

General QA Concept

BDD

» » Difference between include and forward method with code

This section is dedicated to my viewers.Here they can ask question and I will be happy to help or redirect to some page what is best suitable for them.


Hei Animesh,I am bit confused with include and forward method mentioned by you in your previous post http://www.askqtp.com/2012/01/how-to-write-servlet-chain.htmlCan you explain a bit more?
Regads,
Dan
Hi Dan,
Thank you very much for writing me. Let me explain you more on this....



definition forward()




this method is used to forward a request to another servlet (or jsp). So no response from parent to child is propagated. Also from the point the forward method is called , control goes to called or the child servlet .

Servlet-4 coding-forward()








Raw code

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;

public class Servlet4 extends HttpServlet {
    private static final String CONTENT_TYPE = "text/html; charset=windows-1252";

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

   public void doGet(HttpServletRequest request,
                      HttpServletResponse response) throws ServletException,
                                                           IOException {
     
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>Servlet4</title></head>");
        out.println("<body>");
     
        String text1=request.getParameter("text1");
        String msg ="<p>The servlet has received a GET. Servle4.This is the reply.</p>";
        RequestDispatcher rd = request.getRequestDispatcher("/servlet5");
        request.setAttribute("text1",text1);
        out.println("came back to servlet 4");
        out.println(msg);
        if (rd != null)
        //  rd.include(request, response);
        rd.forward(request, response);
     
       

        out.println("</body></html>");
        out.close();
    }

    public void doPost(HttpServletRequest request,
                       HttpServletResponse response) throws ServletException,
                                                            IOException {
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>Servlet4</title></head>");
        out.println("<body>");
        out.println("<p>The servlet has received a POST. This is the reply.</p>");
        out.println("</body></html>");
        out.close();
    }
if you want to implement service method instead of doGet() and doPost()
  /*  public void service(HttpServletRequest request,
                          HttpServletResponse response) throws ServletException,
                                                               IOException {
            response.setContentType(CONTENT_TYPE);
            PrintWriter out = response.getWriter();
            out.println("<html>");
            out.println("<head><title>Servlet5</title></head>");
            out.println("<body>");
            String text1=request.getParameter("text1");
            String msg ="<p>The servlet has received a GET. Servle4.This is the reply.</p>";
            RequestDispatcher rd = request.getRequestDispatcher("/servlet5");
             request.setAttribute("text1",text1);
            out.println(msg);
            if (rd != null)
             //    rd.include(request, response);
             rd.forward(request, response);
            out.println("came back to servlet 4");
            out.println(msg);
            out.println("</body></html>");
            out.close();
         }*/
}

The output will be


definition include()

this method is used to call the new servlet or process the  a request to another servlet (or jsp) with some data passing through the parent to child or calling servlet to called servlet.response from parent to child is propagated. Also from the point the include method is called , control goes to called or the child servlet  and comes back .
All the out.println command before the include method is called is propagated to child servlet as part of response.


Servlet-4 coding-include()



Raw code:

Same as above but the  open include instead of forward
          rd.include(request, response);
        //rd.forward(request, response);


The output will be

Hope this will clarify your doubt...
Please let me know if need further information.

«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply