java.lang.IllegalStateException:ServletConfig尚未初始化

时间:2016-03-22 22:41:47

标签: servlets illegalstateexception servletconfig

我正在尝试创建一个允许用户自定义会话超时的UI组件。所以我创建了一个servlet,如下所示:

public class SessionTimeoutServlet extends AbstractBaseServlet {
    private static final long serialVersionUID = 2567293974465204729L;

    public static final String REQUEST_TIMEOUT_PARAMETR_NAME = "timeout";
    private static final String TIMEOUT_TYPE_INIT_PARAMETER_NAME = "timeoutType";
    private static final String WEB_TYPE_TIMEOUT = "web";
    private static final String WEBSERVICE_TYPE_TIMEOUT = "webService";

    @EJB(mappedName = SessionSettingsRemote.BEAN_NAME)
    private SessionSettingsRemote sessionSettingsBean;

    @PostConstruct
    public void initTimeout() {
        try {
            String timeoutType = getServletContext().getInitParameter(TIMEOUT_TYPE_INIT_PARAMETER_NAME);
            if (WEBSERVICE_TYPE_TIMEOUT.equals(timeoutType)) {
                setCustomTimeout(sessionSettingsBean.getSessionSettingsDTO().getWebServiceSessionTimeoutInterval());
            } else if (WEB_TYPE_TIMEOUT.equals(timeoutType)) {
                setCustomTimeout(sessionSettingsBean.getSessionSettingsDTO().getWebSessionTimeoutInterval());
            } else {
                setCustomTimeout(30);
            }
        } catch (ApplicationException e) {
            setCustomTimeout(30);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int timeout = Integer.parseInt(request.getParameter(REQUEST_TIMEOUT_PARAMETR_NAME));
        setCustomTimeout(timeout);
    }

    public static void setCustomTimeout(int customTimeout) {
        SessionManagerListener.setCustomTimeout(customTimeout);
    }

}

但是,当我在GlassFish上部署它时,我会遇到异常。

Caused by: java.lang.IllegalStateException: ServletConfig has not been initialized
    at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:199)
    at com.accedian.ems.uiapplication.server.servlets.SessionTimeoutServlet.initTimeout(SessionTimeoutServlet.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl$3.run(InjectionManagerImpl.java:766)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.invokeLifecycleMethod(InjectionManagerImpl.java:760)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.inject(InjectionManagerImpl.java:531)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectInstance(InjectionManagerImpl.java:141)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectInstance(InjectionManagerImpl.java:127)
    at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject(InjectionManagerImpl.java:347)
    at com.sun.enterprise.web.WebContainer.createServletInstance(WebContainer.java:991)
    at com.sun.enterprise.web.WebModule.createServletInstance(WebModule.java:2130)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1404)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1381)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5732)

我在getServletConfig()之前使用了getServletContext(),但它是null。那么为什么我的配置和上下文没有正确设置?

1 个答案:

答案 0 :(得分:3)

Servlet早于托管bean和注释以及自Java EE 6以来您看到的所有花哨的东西。以前,您必须显式实现预定义的抽象/模板方法,以便在生命周期的某个时刻执行任务。

对于servlet,为了挂起它的初始化,就像你在" normal"上使用@PostConstruct一样。托管bean,您必须覆盖预定义的GenericServlet#init()方法。

@Override
public void init() {
    // ...
}

getServletContext()将在那里提供。

如果您注意GenericServlet javadoc,您会注意到init(ServletConfig)也是init()。但强烈建议使用该方法,但请改用init(ServletConfig)ServletContext的默认实现即注意super.init(config)已正确设置。您必须介意致电@PostConstruct不要犯同样的错误。作为一个历史记录,请参阅您在托管bean上看到的@PreDestroy方法的规范名称," init",从这个Servlet API继承。

万一你想知道,@Override public void destroy() { // ... } 等价物是GenericServlet#destroy()方法。

owners