DLESE Tools
v1.2

org.dlese.dpc.webapps.tools
Class GeneralServletTools

java.lang.Object
  extended byorg.dlese.dpc.webapps.tools.GeneralServletTools

public final class GeneralServletTools
extends Object

This class contains utility methods useful in servlet-based applications.

Author:
John Weatherley

Constructor Summary
GeneralServletTools()
           
 
Method Summary
static String getAbsolutePath(String fname, ServletContext servletContext)
          Gets the absolute path to a given file or directory.
static String getAbsolutePath(String fname, String docRoot)
          Gets the absolute path to a given file or directory.
static String getContextUrl(HttpServletRequest req)
          Gets the URL that refers to the current servlet context, for example "http://host:8080/my_app_context".
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GeneralServletTools

public GeneralServletTools()
Method Detail

getContextUrl

public static String getContextUrl(HttpServletRequest req)
Gets the URL that refers to the current servlet context, for example "http://host:8080/my_app_context".

Parameters:
req - The request.
Returns:
The contextUrl.

getAbsolutePath

public static final String getAbsolutePath(String fname,
                                           ServletContext servletContext)
                                    throws ServletException
Gets the absolute path to a given file or directory. Assumes the path passed in is eithr already absolute (has leading slash) or is relative to the context root (no leading slash). If the string passed in does not begin with a slash ("/"), then the string is converted. For example, an init parameter to a config file might be passed in as "WEB-INF/conf/serverParms.conf" and this method will return the corresponding absolute path "/export/devel/tomcat/webapps/myApp/WEB-INF/conf/serverParms.conf."

If the string that is passed in already begings with "/", nothing is done.

Note: the HttpServlet init() (super.init()) method must be called prior to using this method, else a ServletException is thrown.

Parameters:
fname - An absolute or relative file name or path (relative the the context root).
servletContext - The HttpServletContext of the appliection.
Returns:
The absolute path to the given file or path.
Throws:
ServletException - An exception related to this servlet

getAbsolutePath

public static final String getAbsolutePath(String fname,
                                           String docRoot)
Gets the absolute path to a given file or directory. Assumes the path passed in is eithr already absolute (has leading slash) or is relative to the context root (no leading slash). If the string passed in does not begin with a slash ("/"), then the string is converted. For example, an init parameter to a config file might be passed in as "WEB-INF/conf/serverParms.conf" and this method will return the corresponding absolute path "/export/devel/tomcat/webapps/myApp/WEB-INF/conf/serverParms.conf."

If the string that is passed in already begings with "/", nothing is done.

Note: the super.init() method must be called prior to using this method, else a ServletException is thrown.

Parameters:
fname - An absolute or relative file name or path (relative the the context root).
docRoot - The context document root as obtained by calling getServletContext().getRealPath("/");
Returns:
The absolute path to the given file or path.

DLESE Tools
v1.2