Servlet:SEVERE:为servlet分配异常

时间:2012-09-11 09:00:52

标签: java exception tomcat servlets

以下stacktrace是我尝试访问servlet时获得的,这似乎从Tomcat管理器运行良好。

11 Sep, 2012 11:50:12 AM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet LoginServlet as unavailable
11 Sep, 2012 11:50:12 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet LoginServlet
java.lang.Error: Unresolved compilation problems: 
    The import javax.servlet cannot be resolved
    The import javax.servlet cannot be resolved
    The import javax.servlet cannot be resolved
    The import javax.servlet cannot be resolved
    The import javax.servlet cannot be resolved
    The import javax.servlet cannot be resolved
    HttpServlet cannot be resolved to a type
    ServletConfig cannot be resolved to a type
    ServletException cannot be resolved to a type
    HttpServlet cannot be resolved to a type
    ServletContext cannot be resolved to a type
    ServletException cannot be resolved to a type
    HttpServletRequest cannot be resolved to a type
    HttpServletResponse cannot be resolved to a type
    ServletException cannot be resolved to a type
    request cannot be resolved
    The method getLoggedin() from the type LoginHandler refers to the missing type ServletException
    The method destroy() of type LoginManager must override or implement a supertype method
    HttpServlet cannot be resolved to a type

    at com.login.servlets.LoginManager.<init>(LoginManager.java:5)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1149)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:827)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:877)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:594)
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1675)
    at java.lang.Thread.run(Unknown Source)
11 Sep, 2012 12:05:12 PM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet Log4JInitServlet as unavailable
11 Sep, 2012 12:05:12 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /logintest threw load() exception
java.lang.Error: Unresolved compilation problems: 
    The import javax.servlet cannot be resolved
    The import javax.servlet cannot be resolved
    The import javax.servlet cannot be resolved
    The import javax.servlet cannot be resolved
    The import org.apache cannot be resolved
    The import org.apache cannot be resolved
    HttpServlet cannot be resolved to a type
    ServletConfig cannot be resolved to a type
    ServletException cannot be resolved to a type
    ServletContext cannot be resolved to a type
    BasicConfigurator cannot be resolved
    PropertyConfigurator cannot be resolved
    BasicConfigurator cannot be resolved

    at com.logger.WebLogger.<init>(WebLogger.java:5)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1149)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1026)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4421)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4734)
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3461)
    at org.apache.catalina.manager.ManagerServlet.reload(ManagerServlet.java:943)
    at org.apache.catalina.manager.HTMLManagerServlet.reload(HTMLManagerServlet.java:557)
    at org.apache.catalina.manager.HTMLManagerServlet.doGet(HTMLManagerServlet.java:121)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.filters.CsrfPreventionFilter.doFilter(CsrfPreventionFilter.java:186)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:563)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:877)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:594)
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1675)
    at java.lang.Thread.run(Unknown Source)

在eclipse类路径中使用的所有jar都可以在Tomcat lib文件夹中找到。 webapp正在部署时没有任何错误日志,但是当我尝试访问一个servlet时,会发生此异常,并且我将其记录在tomcat日志中。任何人都可以帮忙吗?

The package names are proper inside 'classes', also, the web.xml is proper. 

我不知道为什么会这样。还有一些其他的webapps,几乎使用相同的libs运行良好。期待一个人可以帮助我...

由于

11 个答案:

答案 0 :(得分:10)

最后,弄错了。这是eclipse构建引起的问题。由于构建不正确,类文件已损坏。我清理了整个项目,采取了新的构建,现在它工作正常。谢谢你的回复。

答案 1 :(得分:4)

在查看例外情况之后,你得到了一些错误放置jar文件的地方

(servlet的api.jar文件)。它应该在您的Project / WEB-INF / lib文件夹下。我认为它会

如果您的jar文件位于上面指定的路径中,

将起作用。

答案 2 :(得分:3)

不知怎的,你错过了servlet-api.jar。它应位于tomcat\lib文件夹中。

答案 3 :(得分:2)

如果出现此错误“SEVERE:为servlet分配异常”并加上“java.lang.InstantiationException”异常,请确保您的servlet类具有公共无参数构造函数,因为servlet容器需要能够实例化它。

如果你的servlet类没有定义明确的非零参数构造函数(通常就是这种情况),编译器提供的默认构造函数就足够了。

答案 4 :(得分:2)

我遇到了同样的问题!花了2个小时寻找解决方案! 这就是它!

  

WebContent(在Dynamic Web Project下) - &gt; WEB-INF - &gt; lib(它是空的 -   好的) - &gt;右键单击它(lib) - &gt;构建路径 - &gt;用作来源   夹。

DONE!

答案 5 :(得分:1)

java.lang.Error: Unresolved compilation problems:”

表示应用程序未正确编译。请将servlet-api.jar放在类路径中,而不仅仅放在项目的lib文件夹中,还要将它附加到类路径中。

请确保tomcat/common/lib文件夹中也有相同的jar。

答案 6 :(得分:0)

我还看到了这个异常,其中@Resource注释导致与两个技术相互踩踏的冲突(tomcat servlet api 3和spring)。

答案 7 :(得分:0)

我的情况是使用J ava 8 lamda表达式与不基于Java 8的旧版Rest一样

答案 8 :(得分:0)

对我来说,发生在我在junit测试中使用Lambda表达式时。在对测试发表评论后,服务器返回了正确的api响应。

答案 9 :(得分:0)

我也遇到类似的异常
从Java 8移至Java 11时,出现以下错误

异常org.apache.catalina.core.ApplicationContext.log servlet [health-servlet]的Servlet.init()抛出异常java.lang.IllegalArgumentException

为解决错误,我在pom中升级了版本

<jersey.version>2.23.2</jersey.version><!-- Remove this  -->
<jersey.version>2.30.1</jersey.version><!-- Add this  -->

我的web.xml中有以下条目

    <servlet>
    <servlet-name>health-servlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.xyz.platform.registry.service.ServiceRegistryApplication</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

答案 10 :(得分:0)

转到项目属性->部署程序集->检查maven依赖项是否可见,如果缺少它们,我们也会得到相同的错误。我可以解决将maven依赖项添加到Deployment Assembly中的问题。

[在将Maven依赖项添加到Deployment Assembly中之后,看起来像这样]

https://i.stack.imgur.com/7iwaW.jpg