我想在 VS Code 中设置我的纯 Java Web 项目。我的项目目录如下所示:
.vscode
WEB-INF
..../classes/com/example/HelloWorldServlet.java
..../lib/servlet-api.jar
....web.xml
.classpath
index.jsp
我已将我的项目添加到 tomcat 安装文件夹中的 webapps 文件夹中,当我转到 localhost:8080/manager/html 时,我可以看到 /Project_1 作为应用程序之一:
如果我转到 localhost:8080/Project_1 它会向我显示index.jsp 文件上正文的内容,但是当我编辑该文件时,它不会更新,即使在 tomcat 重新启动后也不会更新。我缺少什么?另外,我有一个 servlet,您可以将其视为我的文件之一,并且我为其指定了一条路由,但当我继续访问 localhost:8080/Project_1/hello:
时,却给了我 404文件:
<web-app>
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>com.example.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/servlet-api.jar"/>
<!-- Any other dependencies you might have -->
</classpath>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>Hello JSP</title>
</head>
<body>
<h1>Hello World from JSP</h1>
<h1>Hello World from JSP</h1>
<h1>Hello World from JSP</h1>
</body>
</html>
package com.example;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class HelloWorldServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>Hello World from Servlet</h1>");
out.println("</body></html>");
}
}
如果您想热重载
.jsp
文件,您可以安装 Deploy 扩展并按照以下步骤操作。
下载扩展后,在settings.json中添加以下配置。
"deploy": {
"packages": [
{
"name": "frontend",
"description": "All files in webapp",
"files": [
"src/main/webapp/*",
"src/main/webapp/*/*",
"src/main/webapp/*.*",
"src/main/webapp/*/*.*",
"src/main/webapp/*/*/*.*",
"src/main/webapp/*/*/*/*.*",
"src/main/webapp/*/*/*/*/*.*",
"src/main/webapp/*/*/*/*/*",
"src/main/webapp/*/*/*/*/*/*.*",
],
"exclude": [
"src/main/webapp/test/*"
],
"deployOnSave": true,
"useTargetList": true,
"button": {
"text": "Deploy",
"tooltip": "Click here to deploy frontend to hotsite",
"targets": [ "HOTSITE" ]
},
}
],
"targets": [
{
"type": "local",
"name": "HOTSITE",
"description": "A local folder",
"dir": "target/DETE/",
"mappings": [
{
"source": "src/main/webapp",
"isRegEx": false,
"target": "/"
}
]
}
]
}
添加完成后,点击下方状态栏中出现的
Deploy
按钮。
右键单击 tomcat 服务器并选择
Add Deployment
选择
Exploded
选择只需在资源管理器中单击Deploy
生成的
DETE文件夹
在浏览器地址栏中输入
http://localhost:8080/DETE
,然后按 Enter
或者右键单击 tomcat,选择
Server Actions...
--> Show in Browser...
--> http://localhost:8080/DETE
修改
.jsp
添加一行内容