问题
The following stacktrace is what I get when i try to access a servlet, which seems running fine from Tomcat manager.
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)
All the jars which was used inside eclipse classpath is avaiable in Tomcat lib folder. The webapp is getting deployed with out any error logs, but when i try to access one servlet, this exception occurs, and i get it logged in the tomcat logs. Can any one help with this?
The package names are proper inside 'classes', also, the web.xml is proper.
I dont know then why this happens. There are certain other webapps, which almost uses the same libs running fine. Expecting some one can help me with this..
Thanks
回答1:
Finally , got my mistake. This was a problem caused by the eclipse build. Since the build was not proper, the class files where corrupted. I cleaned up the whole project, took a fresh build, now its working fine. Thanks for your replies.
回答2:
After looking into the Exceptions, you got, some where you have misplaced the jar files
(servlet-api.jar). It should be under your Project/WEB-INF/lib folder. I think it will
works if your jar files are in the path specified above.
回答3:
Somehow you are missing the servlet-api.jar. It should be located in the tomcat\lib folder.
回答4:
If you get this error "SEVERE: Allocate exception for servlet" coupled with a "java.lang.InstantiationException" exception, make sure that your servlet class has a public no-argument constructor since the servlet container needs to be able to instantiate it.
If your servlet class does not have an explicit non-zero argument constructor defined (which is usually the case), the default constructor provided by the compiler would suffice.
回答5:
I had the same problem! Spent 2 hours searching for solution! And here it is!
WebContent (under Dynamic Web Project) -> WEB-INF -> lib (it's empty- all right) -> right click on it (lib) -> Build Path -> Use as Source Folder.
DONE!
回答6:
"java.lang.Error: Unresolved compilation problems: "
says that application is not compiled properly. Please put the servlet-api.jar in classpath, not just in lib folder of project, but also attach that to classpath.
Please make sure that same jar also available in tomcat/common/lib folder.
回答7:
I also saw this exception where the @Resource annotation caused a conflict with two technologies stepping on each other's toes (tomcat servlet api 3, and spring).
回答8:
My case was using Java 8 lamda expressions with older version of Rest that was not based on Java 8
回答9:
For me, it happened when I was using Lambda expression inside my junit tests. After commenting from the tests, the server returned the proper api response.
来源:https://stackoverflow.com/questions/12366201/servlet-severe-allocate-exception-for-servlet