Understanding Servlet Include Forwarding Mechanism
The include forwarding process in Java Servlets operates as follows:
- If the target resource is a Servlet or JSP, their respective
service()method is invoked. The response content generated by this method is appended to the response body of the originating Servlet. If the target is an HTML document, its content is directly added to the originating Servlet's response. - Control returns to the service method of the originating Servlet, allowing subsequent code blocks to execute.
Characteristics of Include Forwarding
Compared to the forward() method, the include() method exhibits these distinct behaviors:
- Output data from both the source Servlet and the included target resource are combined into the final response.
- Any modifications made to the response status code or headers with in the target resource are ignored by the container.
Implementation Example
Source Servlet (DispatcherServlet.java)
package com.example.web;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
@WebServlet(urlPatterns = "/dispatcher")
public class DispatcherServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("DispatcherServlet processing request");
String paymentAmount = request.getParameter("amount");
System.out.println("Payment amount: " + paymentAmount);
// Response configuration can be set before or after include
// response.setContentType("text/html;charset=UTF-8");
// Obtain request dispatcher for target resource
RequestDispatcher dispatcher = request.getRequestDispatcher("processor");
// Alternative targets:
// request.getRequestDispatcher("data.html");
// request.getRequestDispatcher("result.jsp");
// request.getRequestDispatcher("WEB-INF/template.html");
// Include target resource response
dispatcher.include(request, response);
// Continue adding response content after inclusion
response.getWriter().println("\nAdditional content from DispatcherServlet");
/*
* Key points about include forwarding:
* 1. Server-side operation, transparent to browser
* 2. Browser address bar remains unchanged
* 3. Request parameters propagate to target resource
* 4. Same request/response objects are shared
* 5. Enables content aggregation from multiple resources
* 6. Can access resources under WEB-INF directory
* 7. Limited to internal application resources
*/
}
}
Target Servlet (PaymentProcessor.java)
package com.example.web;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(urlPatterns = "/processor")
public class PaymentProcessor extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("PaymentProcessor executing");
String paymentAmount = request.getParameter("amount");
System.out.println("Processing amount: " + paymentAmount);
// Generate response content
PrintWriter out = response.getWriter();
out.println("Payment processed successfully: $" + paymentAmount);
// Note: Header modifications here are typically ignored
// response.setStatus(HttpServletResponse.SC_ACCEPTED);
}
}