Java 严重:为 servlet 分配异常,尝试从 JSP 页面调用 servlet 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23119018/
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-13 20:51:30  来源:igfitidea点击:

SEVERE: Allocate exception for servlet, error when trying to call a servlet from JSP page

javajsptomcatservlets

提问by SteveK

When I run the jsp on the tomcat server and it runs ok, but when I click submit it throws the following error, I have checked over everything and I am not sure where to go from here because it seems like it should work, any help would be much appreciated, Thank you.

当我在 tomcat 服务器上运行 jsp 并且它运行正常时,但是当我单击提交时它会抛出以下错误,我已经检查了所有内容,但我不确定从哪里开始,因为它似乎应该可以工作,任何帮助将不胜感激,谢谢。

Error Stack Trace:

错误堆栈跟踪:

SEVERE: Allocate exception for servlet RegistrationServlet
java.lang.ClassNotFoundException: logon.RegistrationServlet.java
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:529)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:511)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:139)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1148)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:864)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:134)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

web.xml

网页.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns /javaee    /web-app_2_5.xsd" 
id="WebApp_ID" version="2.5">
<display-name>RegistrationTest</display-name>
 <servlet>
        <display-name>RegistrationServlet</display-name>
        <servlet-name>RegistrationServlet</servlet-name>
        <servlet-class>logon.RegistrationServlet</servlet-class>
 </servlet>
 <servlet-mapping>
     <servlet-name>RegistrationServlet</servlet-name>
     <url-pattern>/RegistrationServlet</url-pattern>
 </servlet-mapping>
</web-app>

The JSP page:

JSP页面:

<h2>Signup Details</h2>
        <form name="actionForm" action="RegistrationServlet" method ="post">
        <br/>Username:<input type="text" name="name">
        <br/>Password:<input type="password" name="password">
        <br/>Email:<input type="text" name="email">
        <br/><input type="submit" value="Submit">
        </form>

The Servlet:

小服务程序:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
  * Servlet implementation class RegistrationServlet
 */
@WebServlet("/RegistrationServlet")
public class RegistrationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public RegistrationServlet() {
    super();

}


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try{
        System.out.println("In the registration Servlet");
        User user = new User();
        user.setName(request.getParameter("name"));
        user.setPassword(request.getParameter("password"));
        user.setEmail(request.getParameter("email"));
        RegisterUser.addUser(user);
        //response.sendRedirect("welcome.jsp");        
    } catch (Throwable exc)
    {
        System.out.println(exc);
    }
}

}

采纳答案by SteveK

For anybody that has a similar problem in the future, after cleaning the project, ensure that the build automatically box is checked under the project drop down menu as this is required to rebuild after clean, mine was unchecked by default for some reason.

对于将来遇到类似问题的任何人,在清理项目后,请确保在项目下拉菜单下选中自动构建框,因为这是清理后重建所必需的,由于某种原因,我的默认情况下未选中。

Also I made a small error in the code, I used annotations (@webservlet) in my servlet and I also added the servlet to my web.xml which caused a conflict. This is now done automatically with annotations.

另外我在代码中犯了一个小错误,我在我的 servlet 中使用了注释(@webservlet),我还将 servlet 添加到我的 web.xml 中,这导致了冲突。这现在通过注释自动完成。

回答by Panky031

I also got the same exception and resolved just by putting full qualified calssname in the web.xml fle. $fully qualified class name$

我也遇到了同样的异常,只需将完全限定的 calssname 放在 web.xml 文件中即可解决。 $完全限定类名$