gpt4 book ai didi

java - 使用 IntelliJ IDEA 将 servlet 部署到本地 Tomcat 服务器

转载 作者:行者123 更新时间:2023-11-28 22:15:44 25 4
gpt4 key购买 nike

尝试将简单的 servlet 部署到 Tomcat 服务器。选择 Run Tomcat... 后,我被重定向到 http://localhost:8080/hi_war_exploded/ 网页只有一个词 - $END$。日志文件报告没有错误。

我希望在 tc9\webapps 中看到我的应用程序的 hw 文件夹,但什么也没找到。

$END$ 是什么意思?我的应用程序在 TomCat 服务器上的什么位置?如何将我的 servlet 放到 TomCat 服务器上?

小服务程序:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class
public class hw extends HttpServlet {

private String message;

public void init() throws ServletException {
// Do required initialization
message = "Hello World";
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// Set response content type
response.setContentType("text/html");

// Actual logic goes here.
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}

public void destroy() {
// do nothing.
}
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">


<servlet>
<servlet-name>hw</servlet-name>
<servlet-class>hw</servlet-class>
</servlet>

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

</web-app>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>Hello</groupId>
<artifactId>hw</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>

最佳答案

IntelliJ IDEA 不会将 webapp 文件复制到 TOMCAT\webapps

modifies Tomcat configurationCATALINA_BASE 中并直接从其 output directory 部署工件以避免复制可能花费大量额外时间的文件,尤其是对于大型项目。

hi_war_exploded 是 Tomcat Run/Debug 配置中配置的上下文,Deployment tab .

在此上下文的根目录中,您拥有 IntelliJ IDEA 在创建项目时生成的默认 index.jsp 页面。

当你打开http://localhost:8080/hi_war_exploded/ URL,Tomcat 从应用程序的 Web 资源根目录提供 index.jsp

$END$new JSP file template 的一部分.当您在项目中创建新的 JSP 文件时,光标将放置在该位置。

当项目向导生成 Web 应用程序项目并放置模板中的 index.jsp 文件时,它不会扩展 $END$ 宏,因此出现在JSP file .实际上是 a known bug在 IntelliJ IDEA 中。

您的 servlet 位于 http://localhost:8080/hi_war_exploded/hw网址。

使其在 http://localhost:8080/hw 可用URL 而不是您需要将 Application context 更改为 /,如以下屏幕截图所示:

Deployment context

关于java - 使用 IntelliJ IDEA 将 servlet 部署到本地 Tomcat 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54712942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com