严重:为 servlet <myservlet-name> java.lang.ClassNotFoundException 分配异常:<myservlet> 异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24942153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:52:29  来源:igfitidea点击:

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

javaservletsnetbeans

提问by John

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.4and Apache tomcat 6.0.41

当我使用以下内容时web.xml,我的项目运行良好,我可以看到通过 index.jsp 页面显示“Hello World”。我正在使用Netbeans 7.4Apache 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,=

但是,当我创建自己的 servlet 并使用以下内容时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:

我最终在 Apache Tomcat 日志中收到以下错误:

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 postbut still getting the errors.

我已经检查了以下帖子,但仍然出现错误。

Project Structure :

项目结构:

enter image description here

在此处输入图片说明

采纳答案by SparkOn

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.xmland put it in your pom.xmlif it is a Maven project.

web.xml并把它放在你的pom.xml,如果它是一个Maven项目。

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

其次,ClassNotFoundException在指定位置找不到类时抛出。在你的情况下,正如你所拥有的

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

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

因此,我假设您有一个 name 包,restapi并且在该包下有 class TEST_Authenticate

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

所以,如果它是一个 Maven 项目,你应该有scr/main/java/yourpackagename/yourClass,如果它是一个简单的网络项目,那么它应该像src/yourpackagename/yourClass.

回答by Am_I_Helpful

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

你最后还没有关闭<web-api>in web.xmlusing /web-api>。所以,它可能会出错。此外,最好保留<session-config>servlet-mapping参数之后。

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

只需在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.jspin 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

默认情况下,欢迎页面设置index.jsp在 NetBeans IDE 中的 Web 应用程序项目中。因此,如果您没有指定任何内容,那么它只会搜索该文件并打开它。如果要打开特定页面,请编辑

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

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

and replace the login.jspwith your_required_jsp_page.

并将 替换为login.jspyour_required_jsp_page。

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

我希望它能回答你的问题。如果没有帮助,请随时发表评论。