dsto.dfc.servlets
Class ServletSupport

java.lang.Object
  extended by dsto.dfc.servlets.ServletSupport

public final class ServletSupport
extends Object

General servlet support routines.

Author:
Matthew Phillips

Method Summary
static void clearError(javax.servlet.http.HttpServletRequest request)
          Clear the current error (if any) for the session.
static String getError(javax.servlet.http.HttpServletRequest request)
          Get the current error (if any) for the session.
static String getNonEmptyParam(javax.servlet.ServletRequest request, String param)
          Get a non-empty parameter value.
static String getOptionalNonEmptyParam(javax.servlet.ServletRequest request, String param)
          Get an optional non-empty parameter value.
static String getParam(javax.servlet.ServletRequest request, String param)
          Get an a required parameter value.
static int getParam(javax.servlet.ServletRequest request, String name, int defaultValue)
          Shortcut to extract an integer parameter from a servlet request.
static String getParam(javax.servlet.ServletRequest request, String param, String defaultValue)
           
static String htmlAttr(String attr, Object value)
          Generate a HTML attr="value" statement.
static String htmlChecked(boolean checked)
          Generate a HTML checked="true" if the parameter is true or "" if not.
static String htmlLink(String text, String url)
          Generate a HTML <a href="...">blah</a> value.
static String htmlNameValue(String field, IDataObject values)
          Generate a HTML name="field" value="value" pair.
static String htmlNameValue(String field, Object value)
          Generate a HTML name="field" value="value" pair.
static String htmlValueAttr(Object value)
           
static void postError(javax.servlet.http.HttpServletRequest request, String message)
          Post an error by lodging it as the "error" attribute on the current session.
static String toHTML(String string)
          Escape a string for use in HTML.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getParam

public static int getParam(javax.servlet.ServletRequest request,
                           String name,
                           int defaultValue)
Shortcut to extract an integer parameter from a servlet request.

Parameters:
request - The request.
name - The parameter name.
defaultValue - The value to return if name is not defined or invalid.

getNonEmptyParam

public static String getNonEmptyParam(javax.servlet.ServletRequest request,
                                      String param)
                               throws MissingParameterException
Get a non-empty parameter value.

Parameters:
request - The request.
param - The parameter name.
Returns:
The parameter value. This will be non-null and non-empty.
Throws:
MissingParameterException - if the parameter is either null or the empty string.

getOptionalNonEmptyParam

public static String getOptionalNonEmptyParam(javax.servlet.ServletRequest request,
                                              String param)
Get an optional non-empty parameter value. An empty parameter value is treated as non existent.

Parameters:
request - The request.
param - The parameter name.
Returns:
The parameter value. This will either be null or non-empty.

getParam

public static String getParam(javax.servlet.ServletRequest request,
                              String param)
                       throws MissingParameterException
Get an a required parameter value.

Parameters:
request - The request.
param - The parameter name.
Returns:
The parameter value. This will be be non-null.
Throws:
MissingParameterException - if the parameter is not present.

getParam

public static String getParam(javax.servlet.ServletRequest request,
                              String param,
                              String defaultValue)

htmlNameValue

public static String htmlNameValue(String field,
                                   IDataObject values)
Generate a HTML name="field" value="value" pair.

Parameters:
field - The field name.
values - The value set. The value is read using values.getValue (field).
Returns:
The HTML string.
See Also:
htmlNameValue(String, Object)

htmlNameValue

public static String htmlNameValue(String field,
                                   Object value)
Generate a HTML name="field" value="value" pair.

Parameters:
field - The field name.
value - The value. If null, the value= part is left out.
Returns:
The HTML string.

htmlValueAttr

public static String htmlValueAttr(Object value)

htmlAttr

public static String htmlAttr(String attr,
                              Object value)
Generate a HTML attr="value" statement.

Parameters:
attr - The attribute name.
value - The value. If null, "" is returned..
Returns:
The HTML string.

htmlLink

public static String htmlLink(String text,
                              String url)
Generate a HTML <a href="...">blah</a> value.

Parameters:
text - The link text.
url - The URL. May be null in which case just the text is returned.

htmlChecked

public static String htmlChecked(boolean checked)
Generate a HTML checked="true" if the parameter is true or "" if not.


toHTML

public static String toHTML(String string)
Escape a string for use in HTML. Courtesy of http://www.rgagnon.com/javadetails/java-0306.html.

Parameters:
string - The string to escape. May be null.
Returns:
The escaped string.

postError

public static void postError(javax.servlet.http.HttpServletRequest request,
                             String message)
Post an error by lodging it as the "error" attribute on the current session.

Parameters:
request - The request that the error happened in.
message - The error message.
See Also:
getError(HttpServletRequest), clearError(HttpServletRequest)

getError

public static String getError(javax.servlet.http.HttpServletRequest request)
Get the current error (if any) for the session.

Parameters:
request - The request that the error happened in.
Returns:
The error message or null for none.
See Also:
postError(HttpServletRequest, String), clearError(HttpServletRequest)

clearError

public static void clearError(javax.servlet.http.HttpServletRequest request)
Clear the current error (if any) for the session.

Parameters:
request - The request that the error happened in.
See Also:
postError(HttpServletRequest, String), getError(HttpServletRequest)


Copyright © 2008 Commonwealth of Australia