Wednesday, May 31, 2006

include() and forward()

The include method is used to include content from another resource, such as another servlet, a JSP page, or an HTML page.

public void include(javax.servlet.ServletRequest request, javax.servlet.ServletResponse
response) throws javax.servlet.ServletException, java.io.IOException {
RequestDispatcher rd = request.getRequestDispatcher("SecondServlet");
rd.include(request, response);
}

If you are including from a doPost method, the doPost method of the second servlet will be invoked. If including from a doGet method, the doGet method of the second servlet will be called.

The forward method is used to forward a request from one servlet to another.

public void forward(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
RequestDispatcher rd = request.getRequestDispatcher("SecondServlet"); rd.forward(request, response);

}

Unlike the include method, the forward method of the RequestDispatcher interface may be called only by the calling servlet if no output has been committed to the client. If output exists in the response buffer that has not been committed, the buffer must be cleared before the target servlet's service method is called. If the response has been committed prior to calling the forward method, an IllegalStateException will be thrown.

0 Comments:

Post a Comment

<< Home