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

Java examples for Servlet JSP:Filter

Description

Get a String init parameter from a servlet FilterConfig

Demo Code

/*// w ww .  ja v a 2 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 String 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 string, 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 String getStringInitParameter(
            FilterConfig theFilterConfig, String theInitParameterName,
            boolean isMandatory) throws ServletException {
        String returnedValue = theFilterConfig
                .getInitParameter(theInitParameterName);
        if (isMandatory && (returnedValue == null)) {
            throw new ServletException(theInitParameterName
                    + " parameter must be declared for "
                    + theFilterConfig.getFilterName() + " in web.xml");
        }
        return returnedValue;
    }
}

Related Tutorials