设置 VS Code 进行 Java Web 开发

问题描述 投票:0回答:1

我想在 VS Code 中设置我的纯 Java Web 项目。我的项目目录如下所示:

screenshot from VS Code

.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 作为应用程序之一:tomcat webapp manager

如果我转到 localhost:8080/Project_1 它会向我显示index.jsp 文件上正文的内容,但是当我编辑该文件时,它不会更新,即使在 tomcat 重新启动后也不会更新。我缺少什么?另外,我有一个 servlet,您可以将其视为我的文件之一,并且我为其指定了一条路由,但当我继续访问 localhost:8080/Project_1/hello: web.xml file

时,却给了我 404

文件:

  • web.xml:
    <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>
  • index.jsp
<%@ 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>
  • HelloWorldServlet.java
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>");
    }
}

java visual-studio-code jsp servlets
1个回答
0
投票

如果您想热重载

.jsp
文件,您可以安装 Deploy 扩展并按照以下步骤操作。

  1. 下载扩展后,在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": "/"
                        }
                    ]
                }
            ]
        }
    
  2. 添加完成后,点击下方状态栏中出现的

    Deploy
    按钮。

  3. 右键单击 tomcat 服务器并选择

    Add Deployment

  4. 选择

    Exploded

  5. 选择只需在资源管理器中单击Deploy生成的

    DETE
    文件夹

  6. 在浏览器地址栏中输入

    http://localhost:8080/DETE
    ,然后按 Enter

    或者右键单击 tomcat,选择

    Server Actions...
    -->
    Show in Browser...
    -->
    http://localhost:8080/DETE

  7. 修改

    .jsp
    添加一行内容

  8. 刷新页面即可看到更改后的内容。

© www.soinside.com 2019 - 2024. All rights reserved.