【问题标题】:Deploy servlet with IntelliJ IDEA to local Tomcat server使用 IntelliJ IDEA 将 servlet 部署到本地 Tomcat 服务器
【发布时间】:2019-07-09 19:26:52
【问题描述】:

尝试将简单的 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>

【问题讨论】:

    标签: java tomcat servlets intellij-idea


    【解决方案1】:

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

    它在CATALINA_BASEmodifies Tomcat configuration 并直接从其output directory 部署工件以避免复制可能需要大量额外时间的文件,尤其是对于大型项目。

    hi_war_exploded 是在 Tomcat 运行/调试配置中配置的上下文,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 中。它实际上是 IntelliJ IDEA 中的 a known bug

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

    要使其在 http://localhost:8080/hw URL 上可用,您需要将 Application context 更改为 /,如下图所示:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-07
      相关资源
      最近更新 更多