无法实例化servlet,无法找到类异常

时间:2016-11-22 22:03:16

标签: java eclipse maven servlets pom.xml

我使用以下命令创建了一个示例maven项目:

mvn archetype:generate -DgroupId = com.rohit -DartifactId = RohitLoginApp -DarchetypeArtifactId = maven-archetype-webapp -DinteractiveMode = false

以下是eclipse中项目结构的图像链接:

Maven project structure in eclipse

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.rohit</groupId>
  <artifactId>RohitLoginApp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>RohitLoginApp Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>   
  </dependencies>
  <build>
    <finalName>RohitLoginApp</finalName>
  </build>
</project>

的web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>RohitTestServlet</servlet-name>
    <display-name>RohitTestServlet</display-name>
    <description></description>
    <servlet-class>com.company.rohit.RohitTestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>RohitTestServlet</servlet-name>
    <url-pattern>/RohitTestServlet</url-pattern>
  </servlet-mapping>
</web-app>

RohitTestServlet.java

package com.company.rohit;

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

/**
 * Servlet implementation class RohitTestServlet
 */
public class RohitTestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public RohitTestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String userId = request.getParametker("userName");
        String pwd = request.getParametker("psw");
        response.getWriter().write("User ID and password are received");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

的index.jsp

<html>
<body>
<h2>Login Page</h2>
<form action="RohitTestServlet">
  User Name<br>
  <input type="text" name="userName">
  <br>
  Password<br>
  <input type="password" name="psw">
  <br><br>
  <input type="submit" value="Submit">
</form>
</body>
</html>

当我在tomcat上部署此应用程序时,它首先加载欢迎屏幕,我在其中输入用户ID和密码,然后单击提交按钮。然后它重定向到servlet网址,即http://localhost:8080/RohitLoginApp/RohitTestServlet?userName=&#34; xyz&#34;&amp; psw =&#34; www&#34;。但是它无法命中servlet并获得响应。以下是错误:

SEVERE:为servlet RohitTestServlet分配异常 java.lang.ClassNotFoundException:com.company.rohit.RohitTestServlet     在org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1856)     在org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1705)     at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:506)     在org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:488)     在org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:115)     在org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1148)     在org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:864)     在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:134)     在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)     在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)     在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)     在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)     在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)     在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)     在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)     在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)     at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:625)     at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:318)     在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)     at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)     at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     在java.lang.Thread.run(Thread.java:745)

无法加载课程。我已经做了很多次干净的构建,但仍然无法加载servlet。

1 个答案:

答案 0 :(得分:0)

看起来你的类没有编译成war文件。您需要确保使用适当的包子文件夹在src / main / java中定义servlet类。您可以通过转到webapps目录并查看&#34;类&#34;来验证这一点。文件夹,看你的班级是否在那里。