java Web应用程序中的Servlet示例

前端之家收集整理的这篇文章主要介绍了java Web应用程序中的Servlet示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我是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("

来自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" >

这是我的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">
  

然后我输入
mvn包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)
最佳答案

I have put HelloWorld.java in the src.main.resources folder

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

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

原文链接:https://www.f2er.com/java/438199.html

猜你在找的Java相关文章