home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 November / PCONLINE_11_99.ISO / filesbbs / OS2 / APCHSSL2.ZIP / OS2HTTPD / servlets / InlineServlet.java < prev    next >
Encoding:
Java Source  |  1999-02-10  |  2.0 KB  |  63 lines

  1. /*
  2.  * @(#)InlineServlet.java    1.5 97/05/22
  3.  * 
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.0
  20.  */
  21.  
  22. import java.io.*;
  23. import java.util.*;
  24. import javax.servlet.*;
  25. import javax.servlet.http.*;
  26.  
  27. /**
  28.  * This is a simple file servlet which can be used for inlineing 
  29.  * files in html pages.<p>
  30.  * Pass the name of the file to embed to the servlet in with the
  31.  * param name=file value=filename.html
  32.  */
  33. public class InlineServlet extends HttpServlet {
  34.     ServletContext sc;
  35.  
  36.     public void service(HttpServletRequest req, HttpServletResponse res)
  37.         throws ServletException, IOException
  38.     {
  39.     String fileName = req.getParameter("file");
  40.     FileInputStream fin = null;
  41.     if (fileName.charAt(0) == '/')
  42.         copy(new FileInputStream(req.getRealPath(req.getParameter("file"))),
  43.          res.getOutputStream());
  44.     else {
  45.         String pathInfo = req.getPathInfo();
  46.         fileName = pathInfo.substring(0, pathInfo.lastIndexOf('/') + 1) + 
  47.                 fileName;
  48.         copy(new FileInputStream(req.getRealPath(fileName)), 
  49.              res.getOutputStream());
  50.     }
  51.     }
  52.  
  53.     private void copy(InputStream in, OutputStream out) throws IOException {
  54.         // XXX: use buffer manager
  55.         byte buf[] = new byte[8192];
  56.         int len;
  57.         while((len = in.read(buf, 0, buf.length)) != -1) {
  58.             out.write(buf, 0, len);
  59.         }
  60.     out.flush();
  61.     }
  62. }
  63.