JSP 为Java Web开发设置VS Code

n3h0vuf2  于 2023-09-28  发布在  Java
关注(0)|答案(1)|浏览(171)

我想在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.xml file
档案:

  • 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>
  • .classpath
<?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>");
    }
}
nzrxty8p

nzrxty8p1#

如果你想获得.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": "/"
                    }
                ]
            }
        ]
    }

1.添加完成后,单击下面状态栏中显示的Deploy按钮。

1.右键单击tomcat服务器并选择Add Deployment

1.选择Exploded

1.在资源管理器中选择单击Deploy生成的DETE文件夹

1.在浏览器地址栏中键入http://localhost:8080/DETE,然后按Enter键

  • 或者右键单击tomcat并选择Server Actions...--> Show in Browser...--> http://localhost:8080/DETE *

1.修改.jsp以添加一行内容

1.刷新页面显示更改的内容。

相关问题