Java Servlet & JSP Cookbook
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.
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© 2003-2004 EmbraceAndExtend Corp."); } } The Copyright servlet outputs a line of text that includes the character entity code for the copyright symbol (©), 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.
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 .
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. |