DLESE Tools
v1.6.0

org.dlese.dpc.webapps.tools
Class GeneralServletTools

java.lang.Object
  extended by org.dlese.dpc.webapps.tools.GeneralServletTools

public final class GeneralServletTools
extends Object

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

Version:
$Id: GeneralServletTools.java,v 1.9 2010/05/03 05:24:03 jweather Exp $
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 getContextPath(ServletContext servletContext)
          Returns the context path for the webapp, for example '/dds' or '/' for the root context.
static String getContextUrl(HttpServletRequest req)
          Gets the URL that refers to the current server and servlet context, for example "http://www.dlese.org/dds" or "http://domain.org:8080/context" or or "http://localhost:8080/context".
static String getQueryString(HttpServletRequest request)
          Gets the query string supplied in the request, for example "q=ocean&s=0&n=10".
static String getRequestURI(HttpServletRequest req)
          Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.
static StringBuffer getRequestURL(HttpServletRequest req)
          Reconstructs the URL the client used to make the request, even if the page has been forwarded for example via struts (action.do).
static String getServerUrl(HttpServletRequest req)
          Gets the URL that refers to the current server (scheme, hostname and port), for example "http://www.dlese.org" or "http://host:8080" or "http://localhost:8080".
 
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

getContextPath

public static String getContextPath(ServletContext servletContext)
Returns the context path for the webapp, for example '/dds' or '/' for the root context.

Parameters:
servletContext - The ServletContext object for the webapp
Returns:
The context path for the webapp, for example '/dds' or '/', or null if not able to determine

getServerUrl

public static String getServerUrl(HttpServletRequest req)
Gets the URL that refers to the current server (scheme, hostname and port), for example "http://www.dlese.org" or "http://host:8080" or "http://localhost:8080".

Parameters:
req - The request.
Returns:
The URL to the server.

getContextUrl

public static String getContextUrl(HttpServletRequest req)
Gets the URL that refers to the current server and servlet context, for example "http://www.dlese.org/dds" or "http://domain.org:8080/context" or or "http://localhost:8080/context".

Parameters:
req - The request.
Returns:
The URL to the context.

getQueryString

public static String getQueryString(HttpServletRequest request)
Gets the query string supplied in the request, for example "q=ocean&s=0&n=10". This is the same as HttpServletRequest.getQueryString(), but works even if the request has been forwarded.

Parameters:
request - The request
Returns:
The query string, for example q=ocean&s=0&n=10

getRequestURL

public static StringBuffer getRequestURL(HttpServletRequest req)
Reconstructs the URL the client used to make the request, even if the page has been forwarded for example via struts (action.do). The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.

Parameters:
req - The request.
Returns:
The URL the client used to make the request.

getRequestURI

public static String getRequestURI(HttpServletRequest req)
Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request. Works properly, even if the page has been forwarded for example via struts (action.do).

Parameters:
req - The request.
Returns:
The URI the client used to make the request.

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.6.0