Class WebUtil
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final StringabsoluteEncPath(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res, String path) URL encodes and returns the path obtained viaabsolutePath(HttpServletRequest, String).static final StringabsolutePath(jakarta.servlet.http.HttpServletRequest req, String path) Creates an absolute path starting from the web document root directory.static final voidclientRedirect(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res, String location) Redirects the browser to the specified URL via a client side redirect URL.static final voiddebugRequestParams(jakarta.servlet.http.HttpServletRequest req) writes all request params received in the specied request to System.out.static final voiddebugRequestParams(jakarta.servlet.http.HttpServletRequest req, OutputStream out) writes all request params received in the specied request to the specified output stream.static final voidforward(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res, String URL) Redirects the request (server side redirect) to the specified relative URL via theServletRequest.getRequestDispatcher(String).static final ObjectgetAttribute(jakarta.servlet.http.HttpServletRequest request, String name, String backup) Gets the specified attribute from the request object.static final ObjectgetAttribute(jakarta.servlet.ServletContext context, String name, String backup) Gets the specified attribute from the servlet context.static final booleangetBooleanParameter(jakarta.servlet.http.HttpServletRequest request, String name) Convenience method that returns the specified parameter as a boolean value.static jakarta.servlet.http.CookieReturns the cookie with the specified name or null if no cookie was found.static final StringGets the specified parameter from the request object.static final StringGets the specified initialization parameter from the servlet context.static final StringGets the specified initialization parameter for the specified servlet (via it's ServletConfig).static final StringBuffergetRequestURL(jakarta.servlet.http.HttpServletRequest req) static final ObjectgetRequiredAttribute(jakarta.servlet.http.HttpServletRequest request, String name) Gets the specified required attribute from the request object.static final ObjectgetRequiredAttribute(jakarta.servlet.ServletContext context, String name) Gets the specified required attribute from the servlet context.static final StringgetRequiredParam(jakarta.servlet.http.HttpServletRequest request, String name) Gets the specified required parameter from the request object.static final StringgetRequiredParam(jakarta.servlet.ServletContext context, String name) Gets the specified required initialization parameter from the servlet context.static final StringgetRequiredParam(jakarta.servlet.Servlet servlet, String name) Gets the specified initialization parameter for the specified servlet (via it's ServletConfig).static final StringgetSessionString(jakarta.servlet.http.HttpSession session, String name) Returns a HttpSession attribute as a String or null if the attribute was absent.static booleanisIOSClient(jakarta.servlet.http.HttpServletRequest req) Returns true if the client is an iOS mobile device.static booleanisMobileClient(jakarta.servlet.http.HttpServletRequest req) Returns true if the client is a mobile devicestatic final booleanisSelected(jakarta.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 MapparseQuery(String query) static final booleanrequestCancelled(jakarta.servlet.http.HttpServletRequest req) Returns true only if a request parameter with the name confirm is seen.static final booleanrequestConfirmed(jakarta.servlet.http.HttpServletRequest req) Returns true only if a request parameter with the name cancel is seen.static final voidsetExpireHeaders(jakarta.servlet.http.HttpServletResponse res) set expire headers to cover all cases
-
Constructor Details
-
WebUtil
public WebUtil()
-
-
Method Details
-
getRequiredParam
public static final String getRequiredParam(jakarta.servlet.ServletContext context, String name) throws jakarta.servlet.ServletException Gets the specified required initialization parameter from the servlet context. If this param is not found, throws a permanentUnavailableException.- Throws:
jakarta.servlet.ServletException
-
getParam
-
getRequiredParam
public static final String getRequiredParam(jakarta.servlet.Servlet servlet, String name) throws jakarta.servlet.ServletException Gets the specified initialization parameter for the specified servlet (via it's ServletConfig). Note, servlet init parameters are servlet specific whereas context parameters are shared by all servlets within that context. If this param is not found, throws a permanentUnavailableException.- Throws:
jakarta.servlet.ServletException
-
getParam
Gets the specified initialization parameter for the specified servlet (via it's ServletConfig). Note, servlet init parameters are servlet specific whereas context parameters are shared by all servlets within that context. If this param is not found, then the specified backup value is returned. -
getRequiredParam
public static final String getRequiredParam(jakarta.servlet.http.HttpServletRequest request, String name) throws jakarta.servlet.ServletException Gets the specified required parameter from the request object. If this param is not found, throws a ServletException- Throws:
jakarta.servlet.ServletException
-
getParam
-
isSelected
public static final boolean isSelected(jakarta.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). This is useful for radioboxes and checkboxes. -
getBooleanParameter
public static final boolean getBooleanParameter(jakarta.servlet.http.HttpServletRequest request, String name) Convenience method that returns the specified parameter as a boolean value. The returned value will be converted via theBoolean.parseBoolean(String)(for true, the value should be non-null and equal ignore case to "true"). -
getRequiredAttribute
public static final Object getRequiredAttribute(jakarta.servlet.ServletContext context, String name) throws jakarta.servlet.ServletException Gets the specified required attribute from the servlet context. If this attribute is not found, throws a permanentUnavailableException.- Throws:
jakarta.servlet.ServletException
-
getAttribute
public static final Object getAttribute(jakarta.servlet.ServletContext context, String name, String backup) throws jakarta.servlet.ServletException Gets the specified attribute from the servlet context. If this attribute is not found, then the specified backup value is returned.- Throws:
jakarta.servlet.ServletException
-
getRequiredAttribute
public static final Object getRequiredAttribute(jakarta.servlet.http.HttpServletRequest request, String name) throws jakarta.servlet.ServletException Gets the specified required attribute from the request object. If this param is not found, throws a ServletException.- Throws:
jakarta.servlet.ServletException
-
getAttribute
public static final Object getAttribute(jakarta.servlet.http.HttpServletRequest request, String name, String backup) throws jakarta.servlet.ServletException Gets the specified attribute from the request object. If this attribute is not found, returns the backup value.- Throws:
jakarta.servlet.ServletException
-
getSessionString
Returns a HttpSession attribute as a String or null if the attribute was absent.- Parameters:
name- the name of the session attribute- Throws:
ClassCastException- if the attribute was not a StringNullPointerException- if the specified session or name parameters were null
-
getCookie
-
forward
public static final void forward(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res, String URL) throws jakarta.servlet.ServletException, IOException Redirects the request (server side redirect) to the specified relative URL via theServletRequest.getRequestDispatcher(String). SeeRequestDispatcher.forward(ServletRequest, 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.
- Parameters:
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).- Throws:
jakarta.servlet.ServletExceptionIOException
-
clientRedirect
public static final void clientRedirect(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res, String location) throws jakarta.servlet.ServletException, IOException Redirects the browser to the specified URL via a client side redirect URL. Automatically creates a full URL (including the webapp context path) suitable for this purpose. This method is a thin wrapper aroundResponse.sentRedirectSession 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.
- Parameters:
req- the current requestres- the current responselocation- See. Essentially, the location can be relative to the specified request's URI or relative to the context root if it contains a leading '/'invalid reference
jakarta.servlet.http.HttpResponse#sendRedirect- Throws:
jakarta.servlet.ServletExceptionIOException
-
absolutePath
Creates an absolute path starting from the web document root directory. It does so by prepending the webapp context path (if any) of the specified request to the specified path. The path is not URL encoded with session informtion (seeabsoluteEncPath(HttpServletRequest, HttpServletResponse, 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.
- Parameters:
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/bar
-
absoluteEncPath
public static final String absoluteEncPath(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res, String path) URL encodes and returns the path obtained viaabsolutePath(HttpServletRequest, String). -
getRequestURL
- Returns:
- the full requested URL including any query parameters. (the invalid input: '{@link jakarta.servlet.Http.HttpServletRequest.getRequestURL()' RequestURL} method in HttpServletRequest does not return the query part of the URL)
-
requestCancelled
Returns true only if a request parameter with the name confirm is seen. Forms can set a submit button with this name to signal a confirm request. -
requestConfirmed
Returns true only if a request parameter with the name cancel is seen. Forms can set a submit button with this name to signal a cancel request. -
debugRequestParams
writes all request params received in the specied request to System.out. Useful for debugging. -
debugRequestParams
public static final void debugRequestParams(jakarta.servlet.http.HttpServletRequest req, OutputStream out) writes all request params received in the specied request to the specified output stream. Useful for debugging. -
setExpireHeaders
set expire headers to cover all cases -
parseQuery
- Throws:
UnsupportedEncodingException
-
isMobileClient
Returns true if the client is a mobile device -
isIOSClient
Returns true if the client is an iOS mobile device.
-