java Web应用程序中的Servlet示例

时间:2012-09-24 09:53:27

标签: java tomcat maven

我是java web应用程序开发的新手,你能告诉我,我做错了什么: 我使用maven生成了Web应用程序:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=webapp -DarchetypeArtifactId=maven-archetype-webapp

我已将 HelloWorld.java 放在src.main.resources文件夹中,以下是代码:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet{ 
  public void doGet(HttpServletRequest request, 
  HttpServletResponse response)
  throws ServletException,IOException{
  response.setContentType("text/html");
  PrintWriter pw = response.getWriter();
  pw.println("<html>");
  pw.println("<head><title>Hello World</title></title>");
  pw.println("<body>");
  pw.println("<h1>Hello World</h1>");
  pw.println("</body></html>");
  }
}
来自src.main.webapp.WEB-INF的 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>Hello</servlet-name>
        <servlet-class>HelloWorld</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Hello</servlet-name>
        <url-pattern>/HelloWorld</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.mycompany.app</groupId>
  <artifactId>my-webapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-webapp Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>my-webapp</finalName>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>tomcat-maven-plugin</artifactId>
        <configuration>
            <url>http://localhost:8080/manager/html</url>
            <server>mytomcat</server>
            <path>/my-app</path>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

然后我输入 mvn package tomcat:deploy 并在链接localhost:8080 / web / HelloWorld -

HTTP Status 500 - Error instantiating servlet class HelloWorld

type Exception report

message Error instantiating servlet class HelloWorld

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Error instantiating servlet class HelloWorld    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)  org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)     org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)     org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)     org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)     java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)     java.lang.Thread.run(Thread.java:680) root cause

java.lang.ClassNotFoundException: HelloWorld    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)     org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)     org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)  org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)     org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)     org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)     org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)     java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)     java.lang.Thread.run(Thread.java:680)

1 个答案:

答案 0 :(得分:4)

  

我已将HelloWorld.java放在src.main.resources文件夹

java代码不应该在src/main/resources中,它应该在src/main/java中。

您的HelloWorld.java从未构建过,因此tomcat无法找到它。您的war文件不包含HelloWorld.class