Difference between include and forward method with code

    0
    18
    Spread the love
    • 1
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
      1
      Share

    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 https://www.techtravelhub.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.
    Don't miss out!
    Subscribe To Newsletter

    Receive top technical news, lesson ideas, travel tips and more!

    Invalid email address
    Give it a try. You can unsubscribe at any time.

    Spread the love
    • 1
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
      1
      Share
    Previous articleWhy Client Side Applet is over??
    Next articleHow to solve Puzzle part-6
    I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on [email protected]

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here