【问题标题】:Create a servlet in VScode maven project在 VScode maven 项目中创建一个 servlet
【发布时间】:2021-05-27 02:33:12
【问题描述】:

我在 VScode 中创建了一个 maven-archetype-webapp,并在 src/main/ 中创建了一个文件夹 Servlets。此文件夹 Servlets 包含 servlet。而且我还添加了tomcat服务器,但是当我尝试运行此服务器时出现错误- [Tomcat 9.0]:'C:\Program' 未被识别为内部或外部命令, 可运行的程序或批处理文件不断出现在控制台上。

此外,当我尝试使用 tomcat 服务器运行项目时,它会显示 - 此文件夹不是有效的 webapp

[![这是我项目的树形结构][1]][1] [1]:https://i.stack.imgur.com/YPV3K.png

FirstServlet.java 的代码 -

import javax.servlet.http.HttpServlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.*;

public class FirstServlet extends HttpServlet{
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException  {
        
        System.out.println("this is get method....");
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        out.println("<h1> this is get method of servlet</h2>");
        
        
    }

}

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

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>


  <servlet>
    <servlet-name>first</servlet-name>
    <servlet-class>FirstServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>first</servlet-name>
    <url-pattern>/first</url-pattern>
  </servlet-mapping>

</web-app>

在此文件中,web-app 标签显示一条带有错误消息的红线 - web-app 元素是 Web 应用程序部署描述符的根。

来源:web-app_2_3.dtd

元素类型“web-app”的内容必须匹配“(icon?,display-name?,description?,distributable?,context-param*,filter*,filter-mapping*,listener*,servlet*, servlet-mapping*,session-config?,mime-mapping*,welcome-file-list?,error-page*,taglib*,resource-env-ref*,resource-ref*,security-constraint*,login-config ?,security-role*,env-entry*,ejb-ref*,ejb-local-ref*)".xml(MSG_CONTENT_INVALID)

【问题讨论】:

标签: java maven servlets visual-studio-code


【解决方案1】:

Java:创建 Java 项目 ---> Maven ---> ma​​ven-archetype-webapp:

【讨论】:

  • 嗨@Molly,我已经编辑了这个问题。我创建了一个 maven-archetype-webapp,但仍然存在一些问题。请帮忙。
猜你喜欢
  • 2012-07-07
  • 2022-08-14
  • 1970-01-01
  • 2018-07-15
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 2011-11-10
  • 2020-10-10
相关资源
最近更新 更多