【发布时间】: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)
【问题讨论】:
-
关于你的第一个问题,[Tomcat 9.0]: 'C:\Program' is not known as an internal or external command, operable program or batch file,请提供看看这个回复:Can't run Tomcat Server on VS code。第一个问题解决后,再次pack the project as war and run it on Tomcat server,看看问题是否消失。
标签: java maven servlets visual-studio-code