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


You want to use an external configuration file (such as web.xml ) to configure the resource that is included in a servlet.


Use init parameters with the including servlet to allow the external configuration of the include mechanism, then include the resource with the javax.servlet.RequestDispatcher.include(request,response) method.


You may want to periodically change the resource that a servlet includes, without changing and recompiling the servlet code. You can make these changes by altering the servlet's init parameters in web.xml . Using this strategy, either the included resource's file location itself or the method of retrieving the resource (such as from a database) can change. You can ensure that the servlet imports the correct resource by altering the content of the param-value element. Example 6-3 shows a servlet that is configured to include a file named privacy.jspf . This represents a standard privacy statement for the web application.

Example 6-3. Specifying an included resource by using the servlet's init-param element
 <servlet>     <servlet-name>PrivacyServlet</servlet-name>     <servlet-class>com.jspservletcookbook.IncludeServlet</servlet-class>     <init-param>         <param-name>included-resource</param-name>         <param-value>privacy.jspf</param-value>     <init-param> </servlet> 

Example 6-4 shows the doGet( ) method of the PrivacyServlet. This method gets the value of the included-resource init parameter ( privacy.jspf ), then includes the JSP segment.

Example 6-4. Including a resource specified by an init parameter
 public void doGet(HttpServletRequest request,    HttpServletResponse response) throws ServletException, {                  response.setContentType("text/html"); 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 privacy information based on an init-param value  String includeRes = (String) getInitParameter(           "included-resource");         //get a RequestDispatcher object based on the init-param value         RequestDispatcher dispatcher = request.           getRequestDispatcher(includeRes);         dispatcher.include(request, response);  out.println("</body>");         out.println("</html>");                        } 

Example 6-4 gets a RequestDispatcher representing the configured init-param value with this code:

 //the includeRes variable holds the init-param value "privacy.jspf" RequestDispatcher dispatcher = request.getRequestDispatcher(includeRes); 

Then the dispatcher.include(request,response) method is replaced by the output of the privacy.jspf file. Example 6-5 shows the JSP segment that the PrivacyServlet includes. The JSP's content has some HTML tags that fit into the HTML represented by the including page.

Example 6-5. A JSP segment included in a servlet with a RequestDispatcher
 <%@page errorPage="/error.jsp"%> <p><strong>Parker River Net Solutions Privacy Policy</strong></p> <p>Any personal information you provide to us regarding Web- or software-development services or shareware software, such as your name, address, telephone number, and e-mail address, will not be released, sold, or rented to any entities or individuals outside of Parker River Net Solutions.</p> 

Included segments or pages cannot set or change response headers, so any attempts to set the content type in an included servlet or JSP as in:

 <%@ page contentType="text/xml" %> 

are ignored.

All the included JSP does is specify an error page composed of some formatting- related HTML tags and text. Figure 6-2 shows the browser page for the PrivacyServlet .

Figure 6-2. A web page with an included JSP segment

You may also want to augment Example 6-4 to provide a default resource for inclusion in the servlet, just in case the deployment descriptor ( web.xml ) mistakenly omits an init parameter for the servlet. The method getInitParameter returns null in the event of this omission. You could test for this null condition and then provide a default value for the included statement.

See Also

Recipe 6.3 on including resources that have nested includes; Recipe 6.4-Recipe 6.8 on including resources in JSPs; Chapter SRV.14.2.5 of the Servlet Recipe 2.4 specification; Chapter JSP.1.10.3 of the JSP 2.0 specification on including files in JSPs.

Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326 © 2008-2017.
If you may any questions please contact us: