Java 怎么从servlet filterconfig获取integer 初始参数

2019年8月16日21:32:44Java 怎么从servlet filterconfig获取integer 初始参数已关闭评论 153 1082字阅读3分36秒

1.简介

Java 怎么从servlet filterconfig获取integer 初始参数?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

 
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;

public class Main{
    
    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;
    }
}

 
继续阅读
编程十万个怎么办