[ Team LiB ] Previous Section Next Section

Recipe 6.1 Including a Resource Each Time a Servlet Handles a Request


You want to include information from an external file in a servlet each time the servlet handles a request.


Use the javax.servlet.RequestDispatcher.include(request,response) method in the doGet( ) method of the servlet that includes the external file.


Including the content in the javax.servlet.http.HttpServlet's doGet( ) method initiates the include mechanism whenever the web container receives a GET request for the servlet.

When using this design, implement the servlet's doPost( ) method to call doGet(request,response).

Example 6-1 shows a servlet that imports a copyright template in the doGet( ) method using the javax.servlet.RequestDispatcher.include( ) method.

Example 6-1. Including content in the HttpServlet's init( ) method
package com.jspservletcookbook;           

import javax.servlet.*;
import javax.servlet.http.*;

public class IncludeServlet extends HttpServlet {
 public void doGet(HttpServletRequest request, 
   HttpServletResponse response) throws ServletException,
   java.io.IOException {
        java.io.PrintWriter out = response.getWriter( );
        out.println("<title>Include Servlet</title>");  
        out.println("<h1>Welcome To Our Universe</h1>");
        out.println("Imagine the rest of the page here.<br><br>");
        //Include the copyright information
        RequestDispatcher dispatcher = request.getRequestDispatcher(
        dispatcher.include(request, response);


Example 6-1 gets a RequestDispatcher object by calling the javax.servlet.ServletRequest.getRequestDispatcher( ) method . The parameter to the getRequestDispatcher( ) method in this case is the servlet path to the resource that the include servlet imports: /copyright. This path is mapped in web.xml to the Copyright servlet, which is shown in Example 6-2.

Example 6-2. The imported Copyright servlet
public class Copyright extends HttpServlet {
    public void doGet(HttpServletRequest request, 
      HttpServletResponse response) throws ServletException,
      java.io.IOException {
        java.io.PrintWriter out = response.getWriter( );
        out.println("Copyright&copy; 2003-2004 EmbraceAndExtend Corp.");

The Copyright servlet outputs a line of text that includes the character entity code for the copyright symbol (&copy;), so that the copyright symbol is displayed correctly in the resulting HTML. When the importing servlet calls the include( ) method, the copyright text is inserted in the method call's code location.

A servlet can import an HTML page, as well as the output of a JSP page or servlet. If you are importing HTML fragments in this manner, make sure that the imported text does not break your HTML page, such as by repeating HTML tags or failing to close certain tags.

Figure 6-1 shows the page generated by the IncludeServlet in a browser.

Figure 6-1. The IncludeServlet's page in a browser

Recipe 6.2 describes how to configure the imported resource in an external configuration file, such as web.xml.

Jason Hunter, who provided a technical review of this book, points out that many people are using an offline build process to pregenerate static (e.g., HTML) files when a lot of the site's web content uses includes, such as importing headers and footers into most of the web site's pages. In most cases, the server can handle the requests for static files much more efficiently than requests for dynamic pages (such as a JSP that includes other resources). See Chapter 3, Servlet Best Practices, in the book Java Enterprise Best Practices (O'Reilly).

See Also

Recipe 6.2 and Recipe 6.3 on including resources in servlets; Recipe 6.4-Recipe 6.7 on using jsp:include, the include directive, as well as including resources into JSP documents or XML files; Chapter SRV.14.2.5 of the Servlet 2.4 specification; Chapter JSP.5.4 on of the JSP 2.0 specification on jsp:include; Chapter JSP.1.10.3 of the JSP 2.0 specification on the include directive.

    [ Team LiB ] Previous Section Next Section