Get an Integer init parameter from a servlet FilterConfig - Java Servlet JSP

Java examples for Servlet JSP:Filter

Description

Get an Integer init parameter from a servlet FilterConfig

Demo Code

/*/*from   w  ww.  j  a  v  a  2s .  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 an Integer 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
     * @param theMinValue          the minimum value the init parameter can have
     * @param theMaxValue          the maximum value the init parameter can have
     *
     * @return the init parameter value as an Integer, or null if this Integer parameter is not defined but not
     *         mandatory
     *
     * @throws javax.servlet.ServletException if : <UL> <LI>the initParameter is undefined whereas mandatory </LI>
     *                                        <LI>the initParameter is defined but is not an integer value </LI> <LI>the
     *                                        initParameter is < minValue or > maxValue </LI> </UL> (a message is
     *                                        provided in the exception for each case).
     */
    public static Integer getIntegerInitParameter(
            FilterConfig theFilterConfig, String theInitParameterName,
            boolean isMandatory, int theMinValue, int theMaxValue)
            throws ServletException {
        Integer returnedValue = null;
        String returnedValueAsString = theFilterConfig
                .getInitParameter(theInitParameterName);
        if (isMandatory && returnedValueAsString == null) {
            throw new ServletException(theInitParameterName
                    + " parameter must be declared for "
                    + theFilterConfig.getFilterName() + " in web.xml");
        }
        try {
            returnedValue = new Integer(returnedValueAsString);
        } catch (NumberFormatException e) {
            throw new ServletException(theInitParameterName
                    + " parameter must be an integer value "
                    + theFilterConfig.getFilterName() + " in web.xml");
        }
        if ((returnedValue.intValue() < theMinValue)
                || (returnedValue.intValue() > theMaxValue)) {
            throw new ServletException(theInitParameterName
                    + " parameter for " + theFilterConfig.getFilterName()
                    + " in web.xml must be >= " + theMinValue + " and <= "
                    + theMaxValue);
        }
        return returnedValue;
    }
}

Related Tutorials