Class WebUtil

java.lang.Object
fc.web.servlet.WebUtil

public final class WebUtil extends Object
Misc web related utility functions
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static final String
    absoluteEncPath(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res, String path)
    URL encodes and returns the path obtained via absolutePath(HttpServletRequest, String).
    static final String
    absolutePath(jakarta.servlet.http.HttpServletRequest req, String path)
    Creates an absolute path starting from the web document root directory.
    static final void
    clientRedirect(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 void
    debugRequestParams(jakarta.servlet.http.HttpServletRequest req)
    writes all request params received in the specied request to System.out.
    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.
    static final void
    forward(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res, String URL)
    Redirects the request (server side redirect) to the specified relative URL via the ServletRequest.getRequestDispatcher(String).
    static final Object
    getAttribute(jakarta.servlet.http.HttpServletRequest request, String name, String backup)
    Gets the specified attribute from the request object.
    static final Object
    getAttribute(jakarta.servlet.ServletContext context, String name, String backup)
    Gets the specified attribute from the servlet context.
    static final boolean
    getBooleanParameter(jakarta.servlet.http.HttpServletRequest request, String name)
    Convenience method that returns the specified parameter as a boolean value.
    static jakarta.servlet.http.Cookie
    getCookie(jakarta.servlet.http.HttpServletRequest req, String cookieName)
    Returns the cookie with the specified name or null if no cookie was found.
    static final String
    getParam(jakarta.servlet.http.HttpServletRequest request, String name, String backup)
    Gets the specified parameter from the request object.
    static final String
    getParam(jakarta.servlet.ServletContext context, String name, String backup)
    Gets the specified initialization parameter from the servlet context.
    static final String
    getParam(jakarta.servlet.Servlet servlet, String name, String backup)
    Gets the specified initialization parameter for the specified servlet (via it's ServletConfig).
    static final StringBuffer
    getRequestURL(jakarta.servlet.http.HttpServletRequest req)
     
    static final Object
    getRequiredAttribute(jakarta.servlet.http.HttpServletRequest request, String name)
    Gets the specified required attribute from the request object.
    static final Object
    getRequiredAttribute(jakarta.servlet.ServletContext context, String name)
    Gets the specified required attribute from the servlet context.
    static final String
    getRequiredParam(jakarta.servlet.http.HttpServletRequest request, String name)
    Gets the specified required parameter from the request object.
    static final String
    getRequiredParam(jakarta.servlet.ServletContext context, String name)
    Gets the specified required initialization parameter from the servlet context.
    static final String
    getRequiredParam(jakarta.servlet.Servlet servlet, String name)
    Gets the specified initialization parameter for the specified servlet (via it's ServletConfig).
    static final String
    getSessionString(jakarta.servlet.http.HttpSession session, String name)
    Returns a HttpSession attribute as a String or null if the attribute was absent.
    static boolean
    isIOSClient(jakarta.servlet.http.HttpServletRequest req)
    Returns true if the client is an iOS mobile device.
    static boolean
    isMobileClient(jakarta.servlet.http.HttpServletRequest req)
    Returns true if the client is a mobile device
    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).
    static Map
     
    static final boolean
    requestCancelled(jakarta.servlet.http.HttpServletRequest req)
    Returns true only if a request parameter with the name confirm is seen.
    static final boolean
    requestConfirmed(jakarta.servlet.http.HttpServletRequest req)
    Returns true only if a request parameter with the name cancel is seen.
    static final void
    setExpireHeaders(jakarta.servlet.http.HttpServletResponse res)
    set expire headers to cover all cases

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • 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 permanent UnavailableException.
      Throws:
      jakarta.servlet.ServletException
    • getParam

      public static final String getParam(jakarta.servlet.ServletContext context, String name, String backup)
      Gets the specified initialization parameter from the servlet context. If this param is not found, then the specified backup value is returned.
    • 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 permanent UnavailableException.
      Throws:
      jakarta.servlet.ServletException
    • getParam

      public static final String getParam(jakarta.servlet.Servlet servlet, String name, String backup)
      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

      public static final String getParam(jakarta.servlet.http.HttpServletRequest request, String name, String backup)
      Gets the specified parameter from the request object. If this param is not found, returns the backup value.
    • 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 the Boolean.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 permanent UnavailableException.
      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

      public static final String getSessionString(jakarta.servlet.http.HttpSession session, String name)
      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 String
      NullPointerException - if the specified session or name parameters were null
    • getCookie

      public static jakarta.servlet.http.Cookie getCookie(jakarta.servlet.http.HttpServletRequest req, String cookieName)
      Returns the cookie with the specified name or null if no cookie was found.
    • 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 the ServletRequest.getRequestDispatcher(String). See RequestDispatcher.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 request
      res - the current response
      URL - 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.ServletException
      IOException
    • 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 around 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.

      Parameters:
      req - the current request
      res - the current response
      location - See
      invalid reference
      jakarta.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 '/'
      Throws:
      jakarta.servlet.ServletException
      IOException
    • absolutePath

      public static final String absolutePath(jakarta.servlet.http.HttpServletRequest req, String path)
      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 (see absoluteEncPath(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 HttpServletRequest
      path - 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 via absolutePath(HttpServletRequest, String).
    • getRequestURL

      public static final StringBuffer getRequestURL(jakarta.servlet.http.HttpServletRequest req)
      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

      public static final boolean requestCancelled(jakarta.servlet.http.HttpServletRequest req)
      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

      public static final boolean requestConfirmed(jakarta.servlet.http.HttpServletRequest req)
      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

      public static final void debugRequestParams(jakarta.servlet.http.HttpServletRequest req)
      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

      public static final void setExpireHeaders(jakarta.servlet.http.HttpServletResponse res)
      set expire headers to cover all cases
    • parseQuery

    • isMobileClient

      public static boolean isMobileClient(jakarta.servlet.http.HttpServletRequest req)
      Returns true if the client is a mobile device
    • isIOSClient

      public static boolean isIOSClient(jakarta.servlet.http.HttpServletRequest req)
      Returns true if the client is an iOS mobile device.