Package com.twelvemonkeys.servlet
Class ServletUtil
- java.lang.Object
-
- com.twelvemonkeys.servlet.ServletUtil
-
public final class ServletUtil extends java.lang.ObjectVarious servlet related helper methods.- Version:
- $Id: ServletUtil.java#3 $
- Author:
- Harald Kuhr, Eirik Torske, last modified by $Author: haku $
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.Map<java.lang.String,java.lang.String>asMap(javax.servlet.FilterConfig pConfig)Creates an unmodifiableMapview of the givenFilterConfigs init-parameters.static java.util.Map<java.lang.String,java.lang.String>asMap(javax.servlet.ServletConfig pConfig)Creates an unmodifiableMapview of the givenServletConfigs init-parameters.static java.util.Map<java.lang.String,java.lang.Object>attributesAsMap(javax.servlet.ServletContext pContext)Creates an modifiableMapview of the givenServletContexts attributes.static java.util.Map<java.lang.String,java.lang.Object>attributesAsMap(javax.servlet.ServletRequest pRequest)Creates an modifiableMapview of the givenServletRequests attributes.static javax.servlet.ServletRequestcreateWrapper(javax.servlet.ServletRequestWrapper pImplementation)Creates a wrapper that implements eitherServletRequestorHttpServletRequest, depending on the type ofpImplementation.getRequest().static javax.servlet.ServletResponsecreateWrapper(javax.servlet.ServletResponseWrapper pImplementation)Creates a wrapper that implements eitherServletResponseorHttpServletResponse, depending on the type ofpImplementation.getResponse().static booleangetBooleanParameter(javax.servlet.ServletRequest pReq, java.lang.String pName, boolean pDefault)Gets the value of the given parameter from the request converted to aboolean. If the parameter is not set or not parseable, the default value is returned.static java.lang.StringgetContextRelativeURI(javax.servlet.http.HttpServletRequest pRequest)Gets the request URI relative to the current context path.static longgetDateParameter(javax.servlet.ServletRequest pReq, java.lang.String pName, long pDefault)Gets the value of the given parameter from the request converted to aDate. If the parameter is not set or not parseable, the default value is returned.static doublegetDoubleParameter(javax.servlet.ServletRequest pReq, java.lang.String pName, double pDefault)Gets the value of the given parameter from the request converted to adouble. If the parameter is not set or not parseable, the default value is returned.static floatgetFloatParameter(javax.servlet.ServletRequest pReq, java.lang.String pName, float pDefault)Gets the value of the given parameter from the request converted to afloat. If the parameter is not set or not parseable, the default value is returned.static java.lang.StringgetForwardContextPath(javax.servlet.ServletRequest pRequest)Gets the context path of the resource this request was forwarded from.static java.lang.StringgetForwardPathInfo(javax.servlet.ServletRequest pRequest)Gets the path info of the resource this request was forwarded from.static java.lang.StringgetForwardQueryString(javax.servlet.ServletRequest pRequest)Gets the query string of the resource this request was forwarded from.static java.lang.StringgetForwardRequestURI(javax.servlet.ServletRequest pRequest)Gets the URI of the resource this request was forwarded from.static java.lang.StringgetForwardServletPath(javax.servlet.ServletRequest pRequest)Gets the servlet path of the resource this request was forwarded from.static java.lang.StringgetIncludeContextPath(javax.servlet.ServletRequest pRequest)Gets the context path of the resource currently included.static java.lang.StringgetIncludePathInfo(javax.servlet.ServletRequest pRequest)Gets the path info of the resource currently included.static java.lang.StringgetIncludeQueryString(javax.servlet.ServletRequest pRequest)Gets the query string of the resource currently included.static java.lang.StringgetIncludeRequestURI(javax.servlet.ServletRequest pRequest)Gets the URI of the resource currently included.static java.lang.StringgetIncludeServletPath(javax.servlet.ServletRequest pRequest)Gets the servlet path of the resource currently included.static intgetIntParameter(javax.servlet.ServletRequest pReq, java.lang.String pName, int pDefault)Gets the value of the given parameter from the request converted to anint. If the parameter is not set or not parseable, the default value is returned.static longgetLongParameter(javax.servlet.ServletRequest pReq, java.lang.String pName, long pDefault)Gets the value of the given parameter from the request converted to anlong. If the parameter is not set or not parseable, the default value is returned.static java.lang.StringgetParameter(javax.servlet.ServletRequest pReq, java.lang.String pName, java.lang.String pDefault)Gets the value of the given parameter from the request, or if the parameter is not set, the default value.static java.net.URLgetRealURL(javax.servlet.ServletContext pContext, java.lang.String pPath)Returns aURLcontaining the real path for a given virtual path, on URL form.static java.lang.StringgetSessionId(javax.servlet.http.HttpServletRequest pRequest)Gets the unique identifier assigned to this session.static java.io.FilegetTempDir(javax.servlet.ServletContext pContext)Gets the temp directory for the givenServletContext(web app).static java.util.Map<java.lang.String,java.util.List<java.lang.String>>headersAsMap(javax.servlet.http.HttpServletRequest pRequest)Creates an unmodifiableMapview of the givenHttpServletRequests request headers.static java.util.Map<java.lang.String,java.lang.String>initParamsAsMap(javax.servlet.ServletContext pContext)Creates an unmodifiableMapview of the givenServletContexts init-parameters.static java.util.Map<java.lang.String,java.util.List<java.lang.String>>parametersAsMap(javax.servlet.ServletRequest pRequest)Creates an unmodifiableMapview of the givenHttpServletRequests request parameters.
-
-
-
Method Detail
-
getParameter
public static java.lang.String getParameter(javax.servlet.ServletRequest pReq, java.lang.String pName, java.lang.String pDefault)Gets the value of the given parameter from the request, or if the parameter is not set, the default value.- Parameters:
pReq- the servlet requestpName- the parameter namepDefault- the default value- Returns:
- the value of the parameter, or the default value, if the parameter is not set.
-
getBooleanParameter
public static boolean getBooleanParameter(javax.servlet.ServletRequest pReq, java.lang.String pName, boolean pDefault)Gets the value of the given parameter from the request converted to aboolean. If the parameter is not set or not parseable, the default value is returned.- Parameters:
pReq- the servlet requestpName- the parameter namepDefault- the default value- Returns:
- the value of the parameter converted to a
boolean, or the default value, if the parameter is not set.
-
getIntParameter
public static int getIntParameter(javax.servlet.ServletRequest pReq, java.lang.String pName, int pDefault)Gets the value of the given parameter from the request converted to anint. If the parameter is not set or not parseable, the default value is returned.- Parameters:
pReq- the servlet requestpName- the parameter namepDefault- the default value- Returns:
- the value of the parameter converted to an
int, or the default value, if the parameter is not set.
-
getLongParameter
public static long getLongParameter(javax.servlet.ServletRequest pReq, java.lang.String pName, long pDefault)Gets the value of the given parameter from the request converted to anlong. If the parameter is not set or not parseable, the default value is returned.- Parameters:
pReq- the servlet requestpName- the parameter namepDefault- the default value- Returns:
- the value of the parameter converted to an
long, or the default value, if the parameter is not set.
-
getFloatParameter
public static float getFloatParameter(javax.servlet.ServletRequest pReq, java.lang.String pName, float pDefault)Gets the value of the given parameter from the request converted to afloat. If the parameter is not set or not parseable, the default value is returned.- Parameters:
pReq- the servlet requestpName- the parameter namepDefault- the default value- Returns:
- the value of the parameter converted to a
float, or the default value, if the parameter is not set.
-
getDoubleParameter
public static double getDoubleParameter(javax.servlet.ServletRequest pReq, java.lang.String pName, double pDefault)Gets the value of the given parameter from the request converted to adouble. If the parameter is not set or not parseable, the default value is returned.- Parameters:
pReq- the servlet requestpName- the parameter namepDefault- the default value- Returns:
- the value of the parameter converted to n
double, or the default value, if the parameter is not set.
-
getDateParameter
public static long getDateParameter(javax.servlet.ServletRequest pReq, java.lang.String pName, long pDefault)Gets the value of the given parameter from the request converted to aDate. If the parameter is not set or not parseable, the default value is returned.- Parameters:
pReq- the servlet requestpName- the parameter namepDefault- the default value- Returns:
- the value of the parameter converted to a
Date, or the default value, if the parameter is not set. - See Also:
StringUtil.toDate(String)
-
getIncludeRequestURI
public static java.lang.String getIncludeRequestURI(javax.servlet.ServletRequest pRequest)
Gets the URI of the resource currently included. The value is read from the request attribute"javax.servlet.include.request_uri"- Parameters:
pRequest- the servlet request- Returns:
- the URI of the included resource, or
nullif no include - Since:
- Servlet 2.2
- See Also:
HttpServletRequest.getRequestURI()
-
getIncludeContextPath
public static java.lang.String getIncludeContextPath(javax.servlet.ServletRequest pRequest)
Gets the context path of the resource currently included. The value is read from the request attribute"javax.servlet.include.context_path"- Parameters:
pRequest- the servlet request- Returns:
- the context path of the included resource, or
nullif no include - Since:
- Servlet 2.2
- See Also:
HttpServletRequest.getContextPath()
-
getIncludeServletPath
public static java.lang.String getIncludeServletPath(javax.servlet.ServletRequest pRequest)
Gets the servlet path of the resource currently included. The value is read from the request attribute"javax.servlet.include.servlet_path"- Parameters:
pRequest- the servlet request- Returns:
- the servlet path of the included resource, or
nullif no include - Since:
- Servlet 2.2
- See Also:
HttpServletRequest.getServletPath()
-
getIncludePathInfo
public static java.lang.String getIncludePathInfo(javax.servlet.ServletRequest pRequest)
Gets the path info of the resource currently included. The value is read from the request attribute"javax.servlet.include.path_info"- Parameters:
pRequest- the servlet request- Returns:
- the path info of the included resource, or
nullif no include - Since:
- Servlet 2.2
- See Also:
HttpServletRequest.getPathInfo()
-
getIncludeQueryString
public static java.lang.String getIncludeQueryString(javax.servlet.ServletRequest pRequest)
Gets the query string of the resource currently included. The value is read from the request attribute"javax.servlet.include.query_string"- Parameters:
pRequest- the servlet request- Returns:
- the query string of the included resource, or
nullif no include - Since:
- Servlet 2.2
- See Also:
HttpServletRequest.getQueryString()
-
getForwardRequestURI
public static java.lang.String getForwardRequestURI(javax.servlet.ServletRequest pRequest)
Gets the URI of the resource this request was forwarded from. The value is read from the request attribute"javax.servlet.forward.request_uri"- Parameters:
pRequest- the servlet request- Returns:
- the URI of the resource, or
nullif not forwarded - Since:
- Servlet 2.4
- See Also:
HttpServletRequest.getRequestURI()
-
getForwardContextPath
public static java.lang.String getForwardContextPath(javax.servlet.ServletRequest pRequest)
Gets the context path of the resource this request was forwarded from. The value is read from the request attribute"javax.servlet.forward.context_path"- Parameters:
pRequest- the servlet request- Returns:
- the context path of the resource, or
nullif not forwarded - Since:
- Servlet 2.4
- See Also:
HttpServletRequest.getContextPath()
-
getForwardServletPath
public static java.lang.String getForwardServletPath(javax.servlet.ServletRequest pRequest)
Gets the servlet path of the resource this request was forwarded from. The value is read from the request attribute"javax.servlet.forward.servlet_path"- Parameters:
pRequest- the servlet request- Returns:
- the servlet path of the resource, or
nullif not forwarded - Since:
- Servlet 2.4
- See Also:
HttpServletRequest.getServletPath()
-
getForwardPathInfo
public static java.lang.String getForwardPathInfo(javax.servlet.ServletRequest pRequest)
Gets the path info of the resource this request was forwarded from. The value is read from the request attribute"javax.servlet.forward.path_info"- Parameters:
pRequest- the servlet request- Returns:
- the path info of the resource, or
nullif not forwarded - Since:
- Servlet 2.4
- See Also:
HttpServletRequest.getPathInfo()
-
getForwardQueryString
public static java.lang.String getForwardQueryString(javax.servlet.ServletRequest pRequest)
Gets the query string of the resource this request was forwarded from. The value is read from the request attribute"javax.servlet.forward.query_string"- Parameters:
pRequest- the servlet request- Returns:
- the query string of the resource, or
nullif not forwarded - Since:
- Servlet 2.4
- See Also:
HttpServletRequest.getQueryString()
-
getContextRelativeURI
public static java.lang.String getContextRelativeURI(javax.servlet.http.HttpServletRequest pRequest)
Gets the request URI relative to the current context path.As an example:
requestURI = "/webapp/index.jsp" contextPath = "/webapp"
The method will return"/index.jsp".- Parameters:
pRequest- the current HTTP request- Returns:
- the request URI relative to the current context path.
-
getRealURL
public static java.net.URL getRealURL(javax.servlet.ServletContext pContext, java.lang.String pPath) throws java.net.MalformedURLExceptionReturns aURLcontaining the real path for a given virtual path, on URL form. Note that this method will returnnullfor all the same reasons asServletContext.getRealPath(java.lang.String)does.- Parameters:
pContext- the servlet contextpPath- the virtual path- Returns:
- a
URLobject containing the path, ornull. - Throws:
java.net.MalformedURLException- if the path refers to a malformed URL- See Also:
ServletContext.getRealPath(java.lang.String),ServletContext.getResource(java.lang.String)
-
getTempDir
public static java.io.File getTempDir(javax.servlet.ServletContext pContext)
Gets the temp directory for the givenServletContext(web app).- Parameters:
pContext- the servlet context- Returns:
- the temp directory
-
getSessionId
public static java.lang.String getSessionId(javax.servlet.http.HttpServletRequest pRequest)
Gets the unique identifier assigned to this session. The identifier is assigned by the servlet container and is implementation dependent.- Parameters:
pRequest- The HTTP servlet request object.- Returns:
- the session Id
-
asMap
public static java.util.Map<java.lang.String,java.lang.String> asMap(javax.servlet.ServletConfig pConfig)
Creates an unmodifiableMapview of the givenServletConfigs init-parameters. Note: The returnedMapis optimized forgetoperations and iterating over it'skeySet. For other operations it may not perform well.- Parameters:
pConfig- the servlet configuration- Returns:
- a
Mapview of the config - Throws:
java.lang.IllegalArgumentException- ifpConfigisnull
-
asMap
public static java.util.Map<java.lang.String,java.lang.String> asMap(javax.servlet.FilterConfig pConfig)
Creates an unmodifiableMapview of the givenFilterConfigs init-parameters. Note: The returnedMapis optimized forgetoperations and iterating over it'skeySet. For other operations it may not perform well.- Parameters:
pConfig- the servlet filter configuration- Returns:
- a
Mapview of the config - Throws:
java.lang.IllegalArgumentException- ifpConfigisnull
-
initParamsAsMap
public static java.util.Map<java.lang.String,java.lang.String> initParamsAsMap(javax.servlet.ServletContext pContext)
Creates an unmodifiableMapview of the givenServletContexts init-parameters. Note: The returnedMapis optimized forgetoperations and iterating over it'skeySet. For other operations it may not perform well.- Parameters:
pContext- the servlet context- Returns:
- a
Mapview of the init parameters - Throws:
java.lang.IllegalArgumentException- ifpContextisnull
-
attributesAsMap
public static java.util.Map<java.lang.String,java.lang.Object> attributesAsMap(javax.servlet.ServletContext pContext)
Creates an modifiableMapview of the givenServletContexts attributes.- Parameters:
pContext- the servlet context- Returns:
- a
Mapview of the attributes - Throws:
java.lang.IllegalArgumentException- ifpContextisnull
-
attributesAsMap
public static java.util.Map<java.lang.String,java.lang.Object> attributesAsMap(javax.servlet.ServletRequest pRequest)
Creates an modifiableMapview of the givenServletRequests attributes.- Parameters:
pRequest- the servlet request- Returns:
- a
Mapview of the attributes - Throws:
java.lang.IllegalArgumentException- ifpContextisnull
-
parametersAsMap
public static java.util.Map<java.lang.String,java.util.List<java.lang.String>> parametersAsMap(javax.servlet.ServletRequest pRequest)
Creates an unmodifiableMapview of the givenHttpServletRequests request parameters.- Parameters:
pRequest- the request- Returns:
- a
Mapview of the request parameters - Throws:
java.lang.IllegalArgumentException- ifpRequestisnull
-
headersAsMap
public static java.util.Map<java.lang.String,java.util.List<java.lang.String>> headersAsMap(javax.servlet.http.HttpServletRequest pRequest)
Creates an unmodifiableMapview of the givenHttpServletRequests request headers.- Parameters:
pRequest- the request- Returns:
- a
Mapview of the request headers - Throws:
java.lang.IllegalArgumentException- ifpRequestisnull
-
createWrapper
public static javax.servlet.ServletResponse createWrapper(javax.servlet.ServletResponseWrapper pImplementation)
Creates a wrapper that implements eitherServletResponseorHttpServletResponse, depending on the type ofpImplementation.getResponse().- Parameters:
pImplementation- the servlet response to create a wrapper for- Returns:
- a
ServletResponseorHttpServletResponse, depending on the type ofpImplementation.getResponse()
-
createWrapper
public static javax.servlet.ServletRequest createWrapper(javax.servlet.ServletRequestWrapper pImplementation)
Creates a wrapper that implements eitherServletRequestorHttpServletRequest, depending on the type ofpImplementation.getRequest().- Parameters:
pImplementation- the servlet request to create a wrapper for- Returns:
- a
ServletResponseorHttpServletResponse, depending on the type ofpImplementation.getResponse()
-
-