You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by David Mulligan <dm...@careclinix.com> on 2000/12/13 00:17:06 UTC

RequestDispatcher.include() appears to do nothing.

I am a Java developer who is somewhat new to web development.  I have just
installed Tomcat 3.2 and have been playing with servlets for a few days now
and I have come up against a problem trying to call a servlet from another
servlet.  
As far as I can tell I am supposed to get a RequestDispatcher object from
context using the path to my second servlet from the root of the context
then call include( request, response ).  My second servlet is not adding
anything to the page.  

I've tried using the getRequestDispatcher() method in request as well as
context.  I have tried accessing Test2 directly and I do see it.  What might
I be doing wrong?  

The code I am using to test this is as follows: 

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

public class Test extends HttpServlet {
    public void doGet( HttpServletRequest request, 
            HttpServletResponse response ) 
            throws IOException, ServletException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println( "<html><body>" );
        out.println( "before include<br>" );        
        getServletContext().getRequestDispatcher("/Test2").include( request,
response );
        out.println( "after include<br>" );        
        out.println( "</body></html>" );
    }
}

/* and */

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

public class Test2 extends HttpServlet {
    public void doGet( HttpServletRequest request, 
            HttpServletResponse response ) 
            throws IOException, ServletException {
        PrintWriter out = response.getWriter();
        out.println( "<b>this is the include</b>" );
    }
}