SEVERE: Allocate exception for servlet <myservlet-name> java.lang.ClassNotFoundException: <myservlet> exception

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

When I use the following web.xml, my project runs fine and I can see "Hello World" getting displayed via index.jsp page. I am using Netbeans 7.4 and Apache tomcat 6.0.41

<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">     <session-config>         <session-timeout>             30         </session-timeout>     </session-config>  </web-app> 

However, when I created my own servlet and use the following web.xml,=

 <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">     <session-config>         <session-timeout>             30         </session-timeout>     </session-config>   <servlet>     <servlet-name>TEST_Authenticate</servlet-name>     <servlet-class>restapi.TEST_Authenticate</servlet-class> </servlet>   <servlet-mapping>     <servlet-name>TEST_Authenticate</servlet-name>     <url-pattern>/TEST_Authenticate</url-pattern> </servlet-mapping> <dependency>     <groupId>javax.servlet</groupId>     <artifactId>javax.servlet-api</artifactId>     <version>3.0.1</version>       <scope>provided</scope> </dependency> 

I end up getting the following error in the Apache Tomcat Log:

Jul 24, 2014 9:36:14 AM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Allocate exception for servlet TEST_Authenticate java.lang.ClassNotFoundException: TEST_Authenticate     at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)     at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)     at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1128)     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:103)     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.Http11Processor.process(Http11Processor.java:861)     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)     at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)     at java.lang.Thread.run(Thread.java:744) 

I have checked the following post but still getting the errors.

Project Structure :

回答1:

First of all remove

<dependency>     <groupId>javax.servlet</groupId>     <artifactId>javax.servlet-api</artifactId>     <version>3.0.1</version>     <scope>provided</scope> </dependency> 

from the web.xml and put it in your pom.xml if it is a Maven project.

Secondly, ClassNotFoundException is thrown when the class cannot be found in the specified location. In your case, as you have

<servlet-class>restapi.TEST_Authenticate</servlet-class> 

So, I am assuming that you have a package of name restapi and under which you have the class TEST_Authenticate.

So, if it is a Maven project you should have scr/main/java/yourpackagename/yourClass and if it is a simple web-project then it should be like src/yourpackagename/yourClass.



回答2:

You haven't closed the <web-api> in web.xml using /web-api> at the end. SO,it might be giving error. Also,it is better to keep <session-config> after the servlet-mapping parameters.

Just try the following piece of code in web.xml(COPY-PASTE my code in your web.xml) :-

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.1"  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">  <welcome-file-list> <welcome-file>restapi/TEST_Authenticate.java</welcome-file>    </welcome-file-list>  <servlet> <servlet-name>TEST_Authenticate</servlet-name> <servlet-class>restapi.TEST_Authenticate</servlet-class> </servlet>  <servlet-mapping> <servlet-name>TEST_Authenticate</servlet-name> <url-pattern>/TEST_Authenticate</url-pattern> </servlet-mapping>  <session-config> <session-timeout>30</session-timeout> </session-config>  </web-app> 

EDIT --->

By default,the welcome page is set to index.jsp in a web-application project in NetBeans IDE. So,if you don't specify anything even then it'll search for that file only and open it. If you want to open a specific page,edit the

<welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list>

and replace the login.jsp with your_required_jsp_page.

I hope it answers your question. Feel free to comment if it doesn't help.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!