Flylib.com

Books Software

 
 
 

Chapter 6. Dynamically Including Contentin Servlets and JSPs


Chapter 6. Dynamically Including Contentin Servlets and JSPs

Introduction

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

Recipe  6.2.   Using an External Configuration to Include a Resource in a Servlet

Recipe  6.3.   Including Resources Nested at Multiple Levels in a Servlet

Recipe  6.4.   Including a Resource that Seldom Changes into a JSP

Recipe  6.5.   Including Content in a JSP Each Time the JSP Handles a Request

Recipe  6.6.   Using an External Configuration File to Include a Resource in a JSP

Recipe  6.7.   Including an XML Fragment in a JSP Document

Recipe  6.8.   Including Content from Outside a Context in a JSP


Introduction

Servlets and JSPs often include fragments of information that are common to an organization, such as logos, copyrights, trademarks, or navigation bars. The web application uses the include mechanisms to import the information wherever it is needed, since it is easier to change content in one place then to maintain it in every piece of code where it is used. Some of this information is static and either never or rarely changes, such as an organization's logo. In other cases, the information is more dynamic and changes often and unpredictably, such as a textual greeting that must be localized for each user . In both cases, you want to ensure that the servlet or JSP can evolve independently of its included content, and that the implementation of the servlet or JSP properly updates its included content as necessary.

This chapter recommends recipes for including content in both servlets and JSPs under several conditions:

  • When the included information is refreshed every time a user makes a request.

  • When the included information involves two or more nested levels ”for example, when an included file in turn includes another piece of information, and so on.

  • When you want to use the deployment descriptor to update the item that a servlet includes, which is a handy, less error-prone way of including content when the content is configurable and changes rather often.

  • When you want to import resources into a JSP from outside the web application.

Recipe 6.1 describes how to import a resource each time the servlet handles a request.


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

Problem

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

Solution

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

Discussion

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 {
        
        response.setContentType("text/html");
        java.io.PrintWriter out = response.getWriter( );
        
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Include Servlet</title>");  
        out.println("</head>");
        out.println("<body>");
        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(
        "/copyright");
        dispatcher.include(request, response);

out.println("</body>");
        out.println("</html>");
  }//doGet 
}

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.


{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}

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

Figure 6-1. The IncludeServlet's page in a browser
figs/jsjc_0601.gif

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.