[ Team LiB ] Previous Section Next Section

Recipe 13.5 Viewing Internal Resources in a Servlet

Problem

You want to use a servlet to fetch internal resources from a web application for viewing by authenticated users.

Solution

Use the javax.servlet.ServletContext.getResource(String path) method to generate the input stream from the web resource.

Discussion

A servlet could be used while a web application is in development to provide a view of the deployment descriptor. Web developers often have to double-check web.xml for the values of context-param elements, a servlet's registered name, and other information. Wouldn't it be nice to just request a servlet in the browser to view web.xml?

Example 13-5 opens up web.xml using the ServletContext.getResource( ) method, which returns a java.net.URL object representing the deployment descriptor at the path WEB-INF/web.xml.

The code opens a connection to the XML file by calling the URL object's openConnection( ) method, which returns a java.net.URLConnection object. Then the code buffers the input stream to the resource by wrapping it in a BufferedInputStream:

buf = new BufferedInputStream(urlConn.getInputStream( ));

The urlConn variable refers to a URLConnection.

If the browser is not savvy about displaying XML files in a readable fashion (Netscape 7.1 and Internet Explorer can display these files properly), you can use XSLT to convert the XML into HTML before it is sent to the browser.


Example 13-5. Displaying the deployment descriptor via a servlet
package com.jspservletcookbook;           

import java.io.BufferedInputStream;
import java.io.PrintWriter;
import java.io.IOException;

import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;

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

public class ResourceServlet extends HttpServlet {

 public void doGet(HttpServletRequest request, 
   HttpServletResponse response) throws ServletException, 
     IOException {
    
     //get web.xml for display by a servlet
     String file = "/WEB-INF/web.xml";
     
     URL url = null;
     URLConnection urlConn = null;
     PrintWriter out = null;
     BufferedInputStream buf = null;

     try{

         out = response.getWriter( );

         //access a web resource within the same web application
         // as a URL object
         url = getServletContext( ).getResource(file);

         //set response header
         response.setContentType("text/xml");
    
         urlConn = url.openConnection( );

         //establish connection with URL representing web.xml
         urlConn.connect( );

         buf = new BufferedInputStream(urlConn.getInputStream( ));
         int readBytes = 0;

         //read from the file; write to the PrintWriter
         while((readBytes = buf.read( )) != -1)
             out.write(readBytes);

     } catch (MalformedURLException mue){
    
           throw new ServletException(mue.getMessage( ));
           
     } catch (IOException ioe){
     
        throw new ServletException(ioe.getMessage( ));
         
     } finally {
     
         //close the input/output streams
         if(out != null)
             out.close( );

         if(buf != null)
             buf.close( );

     }
    
 } //doGet
   
 public void doPost(HttpServletRequest request, 
   HttpServletResponse response)
     throws ServletException, IOException {
        
        doGet(request,response);
 } 
}

This servlet is designed for developers; if just anyone has a chance to study the deployment descriptor, it will compromise the web application's security. Therefore, you should remove the servlet from production versions of the web application, or use authentication to allow only authorized users to view the servlet's output (see Chapter 15 for details).


The code uses a PrintWriter to write the bytes received from the input stream, because the servlet intends to display the response as characters (instead of offering the response to the client as a downloaded resource). The ServletContext.getResource(String path) method takes a path that beings with the / character. The path is interpreted as beginning at the context root, or top-level directory, of the web application. Therefore, the servlet obtains web.xml with the following code:

String file = "/WEB-INF/web.xml";
...
url = getServletContext( ).getResource(file);

The ServletContext.getResouce( ) method returns null if it is unable to return a valid resource representing the path parameter.


See Also

Recipe 13.1-Recipe 13.4 on sending PDF, Word, XML, and audio files, respectively, as binary data; the RFC technical documents on MIME: ftp://ftp.rfc-editor.org/in-notes/rfc2045.txt and ftp://ftp.rfc-editor.org/in-notes/rfc2046.txt; RFC 2183 at ftp://ftp.rfc-editor.org/in-notes/rfc2183.txt for background information on the Content-Disposition header; the Media Types section of the HTTP Pocket Reference by Clinton Wong (O'Reilly); Chapter 1 introducing the development of a servlet.

    [ Team LiB ] Previous Section Next Section