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
This chapter recommends recipes for including content in both servlets and JSPs under several conditions:
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 RequestProblemYou want to include information from an external file in a servlet each time the servlet handles a request. SolutionUse the javax.servlet.RequestDispatcher.include(request,response) method in the doGet( ) method of the servlet that includes the external file. DiscussionIncluding 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
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.
{% 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
Recipe 6.2 describes how to configure the imported resource in an external configuration file, such as web.xml .
See AlsoRecipe 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. |