I haven't really looked at the 2.1 API yet, but I solved the
problem for me w/ the attached class.  Works nicely to put your
whole servlet and everything in 1 jar file w/o any hassle setting
it up.

jason

On Tue, 11 May 1999, Craig R. McClanahan wrote:

> Andy Jefferson wrote:
> 
> You are right ... there should be such mechanisms.  What you need was added to the
> version 2.1 API specification for servlets, in the form of
> ServletContext.getResource().  However, at this point in time, Apache JServ
> version 1.0b4 supports only the 2.0 API, which does not include this capability.
> 
> Work is in progress on 2.1 compatibility for Apache JServ version 1.1.  Volunteers
> are needed to complete the coding, testing, and documentation.

Jason Gilbert | http://www.scott.net/~jason/
------------------------------------------------------
I wish I could make the garbage collector thread in my
brain less aggressive.

/*_______________________________________________________________________
 *
 * COPYRIGHT (c) 1999     DOOZER SOFTWARE, INC. - ALL RIGHTS RESERVED
 *                                                                       |
 *                               _/_/           S O F T W A R E, I N C.  |
 *                               _/  _/                                  |
 *                               _/  _/  _/_/   _/_/  _/_/_/  _/_/  _/_/ |
 *                               _/  _/ _/  _/ _/  _/   _/    _/    _/_/ |
 *                               _/_/    _/_/   _/_/  _/_/_/  _/_/  _/ _/|
 *                               ________________________________________|
 *_______________________________________________________________________
 */

/**
 * You may use this class under the terms of the LGPL as specified by the
 * Free Software Foundation.  http://www.fsf.org/copyleft/lesser.html
 */

/**
 * This servlet allows for loading a resource using the PATHINFO of the calling
 * URL.  The easiest way to use this servlet is to make a subclass of it in
 * your own package hierarchy and then use paths relative to that directory.
 * 
 * So, if you have a package under com/yoyodyne/servlet/ with your servlets in
 * it.  Make a subclass com.yoyodyne.servlet.ResourceServlet (or whatever you
 * wnat to call it and you can create an alias to it.  Then put your images or
 * whatever files in directories under com/yoyodyne/servlet.  You can then
 * access those images, etc. using URLs like:
 *
 *   http://hostname/servlet-dir/resource-alias/images/image1.gif
 *   http://hostname/servlet-dir/resource-alias/js/javascript.js
 */
import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;

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

public class ResourceServlet extends HttpServlet
{
   private static final int MAX_RESOURCE_SIZE = 4096;

   private String resourcePrefix = null;

   public void init(ServletConfig config) throws ServletException
   {
      super.init(config);
      
      resourcePrefix = config.getInitParameter("resourcePrefix");
      if (resourcePrefix != null && resourcePrefix.charAt(0) != '/') {
         resourcePrefix = "/" + resourcePrefix;
      }
   }

   /**
    * checks for path info information from the request URL and if it's
    * available calls the doResource method to load the resource using
    * Class.getResource() and sends it back to the client w/ the proper
    * content-type
    */
   protected void service(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException
   {
      String resource = req.getPathInfo();
      if (resource != null && !resource.equals("")) {
         for (int i = 0; i < resource.length(); i++) {
            if (resource.charAt(i) != '/') {
               resource = resource.substring(i);
               break;
            }
         }
         if (!resource.equals("/")) {
            doResource(req, resp, resource);
            resource = null;
            return;
         }
      }
      super.service(req, resp);
   }
   
   /**
    * @param resource the name of the resource to load with any preceding '/'
    *                 removed
    */
   protected void doResource(HttpServletRequest req, HttpServletResponse resp,
                             String resource)
      throws ServletException, IOException
   {
      URL url = this.getClass().getResource(resource);
      if (url == null) {
         url = this.getClass().getResource(resourcePrefix + "/" + resource);
         if (url == null) {
            resp.sendError(HttpServletResponse.SC_NOT_FOUND,
                           "Resource not found" + HttpUtils.getRequestURL(req).
                           append("/").append(resource).append("."));
            return;
         }
      }

      URLConnection con = url.openConnection();
      con.setUseCaches(true);
      
      resp.setContentType(con.getContentType());
      resp.setDateHeader("Last-Modified", con.getLastModified());
      
      int count = con.getContentLength();
      
      byte[] buf = null;
      if (count > 0) {
         resp.setContentLength(count);
         if (count < MAX_RESOURCE_SIZE) {
            buf = new byte[count];
         } else {
            buf = new byte[MAX_RESOURCE_SIZE/2];
         }
      }
      
      InputStream in = con.getInputStream();
      OutputStream out = resp.getOutputStream();
      while ((count = in.read(buf, 0, buf.length)) > 0) {
         out.write(buf, 0, count);
      }
      in.close();
      out.flush();
      out.close();
      url = null;
      con = null;
      buf = null;
   }
}



----------------------------------------------------------------
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
READ THE FAQ!!!!     <http://java.apache.org/faq/>
Archives and Other:  <http://java.apache.org/main/mail.html/>
Problems?:           [EMAIL PROTECTED]

Reply via email to