使用maven在kotlin中编写JSP - 实例化servlet类时出错,ClassNotFoundException

时间:2017-11-29 18:50:59

标签: maven jsp tomcat kotlin

所以我现在已经尝试了JSP一段时间了,我想和kotlin和maven一起使用它,我遇到了一些问题,

抛出ClassNotFoundException,这是我的文件:

  • web.xml

    <web-app>
      <display-name>Archetype Created Web Application</display-name>
        <servlet>
            <servlet-name>Servlet</servlet-name>
            <servlet-class>com.erezbiox1.Servlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>Servlet</servlet-name>
            <url-pattern>/test</url-pattern>
        </servlet-mapping>
    </web-app>
    
  • 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.erezbiox1</groupId>
      <artifactId>KotlinJsp</artifactId>
      <packaging>war</packaging>
      <version>1.0</version>
      <name>KotlinJsp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.jetbrains.kotlin</groupId>
          <artifactId>kotlin-stdlib</artifactId>
          <version>1.2.0</version>
        </dependency>
          <dependency>
              <groupId>javax.servlet</groupId>
              <artifactId>servlet-api</artifactId>
              <version>2.5</version>
          </dependency>
      </dependencies>
      <build>
        <finalName>KotlinJsp</finalName>
        <plugins>
          <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <version>1.2.0</version>
            <executions>
              <execution>
                <id>compile</id>
                <phase>compile</phase>
                <goals>
                  <goal>compile</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>
    
  • Servlet.kt

    // Tried with, and without this:
    @file:JvmName("Servlet")
    
    package com.erezbiox1
    
    import javax.servlet.http.HttpServlet
    import javax.servlet.http.HttpServletRequest
    import javax.servlet.http.HttpServletResponse
    
    class Servlet : HttpServlet(){
    
    override fun doGet(req: HttpServletRequest, resp: HttpServletResponse) {
        resp.writer.println("<h1>IT WORKS!</h1>")
        resp.writer.flush()
    }
    
    }
    

这是项目结构: Project Structure

这是error

// Exception:

javax.servlet.ServletException: Error instantiating servlet class com.erezbiox1.Servlet
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)

// Root Cause:

java.lang.ClassNotFoundException: com.erezbiox1.Servlet
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)

甚至可能吗? jsp,kotlin和maven? 谢谢!

1 个答案:

答案 0 :(得分:0)

项目中的课程位于:source文件夹中。

这不是应该的位置 - 此文件夹仅用于资源。

创建一个新文件夹,将其标记为源根目录,然后使用该文件夹。