com.oreilly.servlet
Class ServletUtils

java.lang.Object
  extended by com.oreilly.servlet.ServletUtils

public class ServletUtils
extends java.lang.Object

A collection of static utility methods useful to servlets. Some methods require Servlet API 2.2.

Version:
1.5, 2001/02/11, added getResource() ".." check, 1.4, 2000/09/27, finalized getResource() behavior, 1.3, 2000/08/15, improved getStackTraceAsString() to take Throwable, 1.2, 2000/03/10, added getResource() method, 1.1, 2000/02/13, added returnURL() methods, 1.0, 1098/09/18
Author:
Jason Hunter, Copyright © 1998-2000

Constructor Summary
ServletUtils()
           
 
Method Summary
static java.net.URL getResource(ServletContext context, java.lang.String resource)
          Gets a reference to the given resource within the given context, making sure not to serve the contents of WEB-INF, META-INF, or to display .jsp file source.
static Servlet getServlet(java.lang.String name, ServletRequest req, ServletContext context)
          Gets a reference to the named servlet, attempting to load it through an HTTP request if necessary.
static java.lang.String getStackTraceAsString(java.lang.Throwable t)
          Gets an exception's stack trace as a String
static void returnFile(java.lang.String filename, java.io.OutputStream out)
          Sends the contents of the specified file to the output stream
static void returnURL(java.net.URL url, java.io.OutputStream out)
          Sends the contents of the specified URL to the output stream
static void returnURL(java.net.URL url, java.io.Writer out)
          Sends the contents of the specified URL to the Writer (commonly either a PrintWriter or JspWriter)
static java.lang.String[] split(java.lang.String str, java.lang.String delim)
          Splits a String into pieces according to a delimiter.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ServletUtils

public ServletUtils()
Method Detail

returnFile

public static void returnFile(java.lang.String filename,
                              java.io.OutputStream out)
                       throws java.io.FileNotFoundException,
                              java.io.IOException
Sends the contents of the specified file to the output stream

Parameters:
filename - the file to send
out - the output stream to write the file
Throws:
java.io.FileNotFoundException - if the file does not exist
java.io.IOException - if an I/O error occurs

returnURL

public static void returnURL(java.net.URL url,
                             java.io.OutputStream out)
                      throws java.io.IOException
Sends the contents of the specified URL to the output stream

Parameters:
URL - whose contents are to be sent
out - the output stream to write the contents
Throws:
java.io.IOException - if an I/O error occurs

returnURL

public static void returnURL(java.net.URL url,
                             java.io.Writer out)
                      throws java.io.IOException
Sends the contents of the specified URL to the Writer (commonly either a PrintWriter or JspWriter)

Parameters:
URL - whose contents are to be sent
out - the Writer to write the contents
Throws:
java.io.IOException - if an I/O error occurs

getStackTraceAsString

public static java.lang.String getStackTraceAsString(java.lang.Throwable t)
Gets an exception's stack trace as a String

Parameters:
e - the exception
Returns:
the stack trace of the exception

getServlet

public static Servlet getServlet(java.lang.String name,
                                 ServletRequest req,
                                 ServletContext context)
Gets a reference to the named servlet, attempting to load it through an HTTP request if necessary. Returns null if there's a problem. This method behaves similarly to ServletContext.getServlet() except, while that method may return null if the named servlet wasn't already loaded, this method tries to load the servlet using a dummy HTTP request. Only loads HTTP servlets.

Parameters:
name - the name of the servlet
req - the servlet request
context - the servlet context
Returns:
the named servlet, or null if there was a problem

split

public static java.lang.String[] split(java.lang.String str,
                                       java.lang.String delim)
Splits a String into pieces according to a delimiter.

Parameters:
str - the string to split
delim - the delimiter
Returns:
an array of strings containing the pieces

getResource

public static java.net.URL getResource(ServletContext context,
                                       java.lang.String resource)
                                throws java.io.IOException
Gets a reference to the given resource within the given context, making sure not to serve the contents of WEB-INF, META-INF, or to display .jsp file source. Throws an IOException if the resource can't be read.

Parameters:
context - the context containing the resource
resource - the resource to be read
Returns:
a URL reference to the resource
Throws:
java.io.IOException - if there's any problem accessing the resource