Get a boolean init parameter from a servlet FilterConfig - Java Servlet JSP

Java examples for Servlet JSP:Filter

Description

Get a boolean init parameter from a servlet FilterConfig

Demo Code

/*//w w w . j ava2 s . c  o m
 * JCaptcha, the open source java framework for captcha definition and integration
 * Copyright (c)  2007 jcaptcha.net. All Rights Reserved.
 * See the LICENSE.txt file distributed with this package.
 */
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;

public class Main{
    /**
     * Get a boolean init parameter from a FilterConfig
     *
     * @param theFilterConfig      the FilterConfig from wich the parameter should be extracted
     * @param theInitParameterName the name of the init parameter
     * @param isMandatory          a boolean indicating if the init parameter is mandatory
     *
     * @return the init parameter value as a boolean, or null if this init parameter is not defined but not mandatory
     *
     * @throws javax.servlet.ServletException is the initParameter is undefined whereas mandatory (a message is provided
     *                                        in the exception)
     */
    public static boolean getBooleanInitParameter(
            FilterConfig theFilterConfig, String theInitParameterName,
            boolean isMandatory) throws ServletException {
        String returnedValueAsString = theFilterConfig
                .getInitParameter(theInitParameterName);
        if (isMandatory && (returnedValueAsString == null)) {
            throw new ServletException(theInitParameterName
                    + " parameter must be declared for "
                    + theFilterConfig.getFilterName() + " in web.xml");
        }
        boolean returnedValue = false;
        if (returnedValueAsString != null) {
            returnedValue = new Boolean(returnedValueAsString)
                    .booleanValue();
        }
        return returnedValue;
    }
}

Related Tutorials