Tomcat 7 - java.lang.NoClassDefFoundError:无法初始化类 javax.servlet.http.Cookie

发布于 2024-12-27 22:36:11 字数 1585 浏览 0 评论 0原文

我有一个使用 j_security 进行基于表单的身份验证的应用程序。首次部署应用程序(或重新启动 tomcat)时,用户尝试直接(无需登录)访问受保护的内容时会出现此异常。

GRAVE: Servlet.service() for servlet [default] in context with path [/mycontext]  threw exception [Could not initialize class javax.servlet.http.Cookie] with root cause
    java.lang.NoClassDefFoundError: Could not initialize class javax.servlet.http.Cookie
    at org.apache.catalina.core.ApplicationSessionCookieConfig.createSessionCookie(ApplicationSessionCookieConfig.java:127)
    at org.apache.catalina.connector.Request.doGetSession(Request.java:2875)
    at org.apache.catalina.connector.Request.getSession(Request.java:2307)
    at org.apache.catalina.connector.RequestFacade$GetSessionPrivilegedAction.run(RequestFacade.java:216)
    at org.apache.catalina.connector.RequestFacade$GetSessionPrivilegedAction.run(RequestFacade.java:205)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:894)
    at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:909)
    at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:238)
    at com.mycompany.myfilter.SaasComponentImpl.getTenantId(SaasComponentImpl.java:284)

例外的行为是将用户重定向到登录表单,并在用户登录时将其重定向到请求的受保护资源。

但奇怪的是,如果第一个请求发送到重新启动的服务器要求登录表单,那么它可以正常工作!看来,在这个 tomcat 找到并正确加载 cookie 类之后,所有请求都按预期处理。

为什么tomcat在lib/servlet-api.jar中找不到确定的Cookie类?恕我直言,当第一次要求加载此类时,没有任何问题...它应该存在于任何阀门、servlet、过滤器等...我是对的吗?

PS:在应用程序从 tomcat 6 迁移之前,这工作正常。

I have an application that use j_security for a form-based authentication. When the application is deployed for the first time (or the tomcat is restarted) user gets this exception when trying access a protected content directly (without login).

GRAVE: Servlet.service() for servlet [default] in context with path [/mycontext]  threw exception [Could not initialize class javax.servlet.http.Cookie] with root cause
    java.lang.NoClassDefFoundError: Could not initialize class javax.servlet.http.Cookie
    at org.apache.catalina.core.ApplicationSessionCookieConfig.createSessionCookie(ApplicationSessionCookieConfig.java:127)
    at org.apache.catalina.connector.Request.doGetSession(Request.java:2875)
    at org.apache.catalina.connector.Request.getSession(Request.java:2307)
    at org.apache.catalina.connector.RequestFacade$GetSessionPrivilegedAction.run(RequestFacade.java:216)
    at org.apache.catalina.connector.RequestFacade$GetSessionPrivilegedAction.run(RequestFacade.java:205)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:894)
    at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:909)
    at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:238)
    at com.mycompany.myfilter.SaasComponentImpl.getTenantId(SaasComponentImpl.java:284)

The excepted behaviour is to redirect the user to the login form and when the user logs in redirect him to the requested protected resource.

But the weird thing is this works correctly if the first request send to the rebooted server ask for the login form!!! It seems that after this tomcat find and load correctly the cookie class and then all the request are handled as expected.

Why tomcat cannot find the Cookie class that is for sure in the lib/servlet-api.jar? IMHO, no mmetter when this class is asked to be load for the first time ... it should be there for any valve, servlet, filter, etc ... I'm right?

PS: This was working fine before the app was migrated from tomcat 6.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋你朝朝暮暮 2025-01-03 22:36:11

确保应用程序的 WEB-INF/lib 中没有 servlet-api.jar。

如果有,它将由两个不同的类加载器加载,并且可能会导致像您遇到的那样奇怪的错误消息。

除此之外,尝试在全新安装的 tomcat 中运行您的应用程序。

Make sure you don't have the servlet-api.jar in your application's WEB-INF/lib.

If you have, it will be loaded by two different class loaders, and might lead to strange error messages like the one you have.

Other than that, try to run your application in a fresh install of tomcat.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文