Constructor and Description |
---|
WebUtil() |
Modifier and Type | Method and Description |
---|---|
static String |
absoluteEncPath(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse res,
String path)
URL encodes and returns the path obtained via
absolutePath(javax.servlet.http.HttpServletRequest, java.lang.String) . |
static String |
absolutePath(javax.servlet.http.HttpServletRequest req,
String path)
Creates an absolute path starting from the web document root directory.
|
static void |
clientRedirect(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse res,
String location)
Redirects the browser to the specified URL via a client side redirect URL.
|
static void |
debugRequestParams(javax.servlet.http.HttpServletRequest req)
writes all request params received in the specied request to
System.out.
|
static void |
debugRequestParams(javax.servlet.http.HttpServletRequest req,
OutputStream out)
writes all request params received in the specied request to
the specified output stream.
|
static void |
forward(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse res,
String URL)
Redirects the request (server side redirect) to the specified relative URL
via the
ServletRequest.getRequestDispatcher(java.lang.String) . |
static Object |
getAttribute(javax.servlet.http.HttpServletRequest request,
String name,
String backup)
Gets the specified attribute from the request object.
|
static Object |
getAttribute(javax.servlet.ServletContext context,
String name,
String backup)
Gets the specified attribute from the servlet context.
|
static boolean |
getBooleanParameter(javax.servlet.http.HttpServletRequest request,
String name)
Convenience method that returns the specified parameter as a boolean
value.
|
static javax.servlet.http.Cookie |
getCookie(javax.servlet.http.HttpServletRequest req,
String cookieName)
Returns the cookie with the specified name or null if no cookie
was found.
|
static String |
getParam(javax.servlet.http.HttpServletRequest request,
String name,
String backup)
Gets the specified parameter from the request object.
|
static String |
getParam(javax.servlet.ServletContext context,
String name,
String backup)
Gets the specified initialization parameter from the servlet context.
|
static String |
getParam(javax.servlet.Servlet servlet,
String name,
String backup)
Gets the specified initialization parameter for the specified servlet
(via it's ServletConfig).
|
static StringBuffer |
getRequestURL(javax.servlet.http.HttpServletRequest req) |
static Object |
getRequiredAttribute(javax.servlet.http.HttpServletRequest request,
String name)
Gets the specified required attribute from the request object.
|
static Object |
getRequiredAttribute(javax.servlet.ServletContext context,
String name)
Gets the specified required attribute from the servlet context.
|
static String |
getRequiredParam(javax.servlet.http.HttpServletRequest request,
String name)
Gets the specified required parameter from the request
object.
|
static String |
getRequiredParam(javax.servlet.ServletContext context,
String name)
Gets the specified required initialization parameter from
the servlet context.
|
static String |
getRequiredParam(javax.servlet.Servlet servlet,
String name)
Gets the specified initialization parameter for the specified servlet
(via it's ServletConfig).
|
static String |
getSessionString(javax.servlet.http.HttpSession session,
String name)
Returns a HttpSession attribute as a String or null if the attribute
was absent.
|
static boolean |
isIOSClient(javax.servlet.http.HttpServletRequest req)
Returns true if the client is an iOS mobile device.
|
static boolean |
isMobileClient(javax.servlet.http.HttpServletRequest req)
Returns true if the client is a mobile device
|
static boolean |
isSelected(javax.servlet.http.HttpServletRequest request,
String name)
The returned value will be true if the specified parameter
is present in the request and is a non-null non-empty string (of
any value).
|
static Map |
parseQuery(String query) |
static boolean |
requestCancelled(javax.servlet.http.HttpServletRequest req)
Returns true only if a request parameter with the
name confirm is seen.
|
static boolean |
requestConfirmed(javax.servlet.http.HttpServletRequest req)
Returns true only if a request parameter with the
name cancel is seen.
|
static void |
setExpireHeaders(javax.servlet.http.HttpServletResponse res)
set expire headers to cover all cases
|
public WebUtil()
public static final String getRequiredParam(javax.servlet.ServletContext context, String name) throws javax.servlet.ServletException
UnavailableException
.javax.servlet.ServletException
public static final String getParam(javax.servlet.ServletContext context, String name, String backup)
public static final String getRequiredParam(javax.servlet.Servlet servlet, String name) throws javax.servlet.ServletException
UnavailableException
.javax.servlet.ServletException
public static final String getParam(javax.servlet.Servlet servlet, String name, String backup)
public static final String getRequiredParam(javax.servlet.http.HttpServletRequest request, String name) throws javax.servlet.ServletException
javax.servlet.ServletException
public static final String getParam(javax.servlet.http.HttpServletRequest request, String name, String backup)
public static final boolean isSelected(javax.servlet.http.HttpServletRequest request, String name)
public static final boolean getBooleanParameter(javax.servlet.http.HttpServletRequest request, String name)
Boolean.parseBoolean(String)
(for true, the
value should be non-null and equal ignore case to "true").public static final Object getRequiredAttribute(javax.servlet.ServletContext context, String name) throws javax.servlet.ServletException
UnavailableException
.javax.servlet.ServletException
public static final Object getAttribute(javax.servlet.ServletContext context, String name, String backup) throws javax.servlet.ServletException
javax.servlet.ServletException
public static final Object getRequiredAttribute(javax.servlet.http.HttpServletRequest request, String name) throws javax.servlet.ServletException
javax.servlet.ServletException
public static final Object getAttribute(javax.servlet.http.HttpServletRequest request, String name, String backup) throws javax.servlet.ServletException
javax.servlet.ServletException
public static final String getSessionString(javax.servlet.http.HttpSession session, String name)
name
- the name of the session attributeClassCastException
- if the attribute was not a StringNullPointerException
- if the specified session or name parameters
were nullpublic static javax.servlet.http.Cookie getCookie(javax.servlet.http.HttpServletRequest req, String cookieName)
public static final void forward(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, String URL) throws javax.servlet.ServletException, IOException
ServletRequest.getRequestDispatcher(java.lang.String)
. See
RequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
for restrictions while
forwarding to another page (in particular, the response must not have been
committed before calling this method).
The calling code must have a return statement immediately after calling this method, otherwise RequestDispatching will probably not work as intended.
req
- the current requestres
- the current responseURL
- The pathname specified may be relative, although it
cannot extend outside the current servlet context. If the
path begins with a "/" it is interpreted as starting from
the root of the current context. (i.e., the webapp context,
if present, does not need to be specified).javax.servlet.ServletException
IOException
public static final void clientRedirect(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, String location) throws javax.servlet.ServletException, IOException
Response.sentRedirect
Session information is preserved if using container provided URL-based sessions.
If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.
req
- the current requestres
- the current responselocation
- See
javax.servlet.http.HttpResponse#sendRedirect
.
Essentially, the location can be relative to the
specified request's URI or relative to the
context root if it contains a leading '/'javax.servlet.ServletException
IOException
public static final String absolutePath(javax.servlet.http.HttpServletRequest req, String path)
absoluteEncPath(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String)
).
This method should be used for all HTML links (in html pages and forms) and also for client side redirects. This ensures the ability to move the calling code to a different web app (other than the default root ("/") web app). If it's certain that there will always only be 1 web app (the default) then calling this method is not necessary.
Note also that server-side redirects and includes (with RequestDispatcher) should *not* use this method since all such server side redirects etc automatically work as expected inside the web app context and are always relative to the web app context itself.
req
- the current HttpServletRequestpath
- an absolute path starting from the root of
the context associated with the request.
If the current webapp (context) is /foo and
the specified path is /bar, then this
method will return /foo/barpublic static final String absoluteEncPath(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, String path)
absolutePath(javax.servlet.http.HttpServletRequest, java.lang.String)
.public static final StringBuffer getRequestURL(javax.servlet.http.HttpServletRequest req)
RequestURL
method in HttpServletRequest does not return the query part of the URL)public static final boolean requestCancelled(javax.servlet.http.HttpServletRequest req)
public static final boolean requestConfirmed(javax.servlet.http.HttpServletRequest req)
public static final void debugRequestParams(javax.servlet.http.HttpServletRequest req)
public static final void debugRequestParams(javax.servlet.http.HttpServletRequest req, OutputStream out)
public static final void setExpireHeaders(javax.servlet.http.HttpServletResponse res)
public static Map parseQuery(String query) throws UnsupportedEncodingException
UnsupportedEncodingException
public static boolean isMobileClient(javax.servlet.http.HttpServletRequest req)
public static boolean isIOSClient(javax.servlet.http.HttpServletRequest req)