
org.ocpsoft.rewrite.servlet.util.ContextUtil Maven / Gradle / Ivy
The newest version!
package org.ocpsoft.rewrite.servlet.util;
import jakarta.servlet.ServletContext;
import org.ocpsoft.common.util.Assert;
/**
* Utility method for easily interacting with the {@link ServletContext}
*
* @author Lincoln Baxter, III
*
*/
public class ContextUtil
{
/**
* Retrieve the named context-parameter from the given {@link ServletContext}. If the parameter is not defined,
* return false, otherwise return the value as a boolean. If the value cannot be converted to a boolean type, return
* false.
*/
public static boolean getInitParamBoolean(ServletContext context, String name)
{
boolean result = false;
String value = getInitParam(context, name, null);
if ("true".equalsIgnoreCase(value))
{
result = true;
}
return result;
}
/**
* Retrieve the named context-parameter from the given {@link ServletContext}. If the parameter is not defined,
* return null.
*/
public static String getInitParam(ServletContext context, String name)
{
return getInitParam(context, name, null);
}
/**
* Retrieve the named context-parameter from the given {@link ServletContext}. If the parameter is not defined,
* return the default value instead.
*/
public static String getInitParam(ServletContext context, String name, String deflt)
{
Assert.notNull(context, "Servlet context must not be null.");
Assert.notNull(context, "Cookie name must not be null.");
String value = context.getInitParameter(name);
if (value == null)
{
value = deflt;
}
return value;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy