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

vic*_*ico 5 java tomcat servlets intellij-idea

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

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

是什么$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.
    }
}
Run Code Online (Sandbox Code Playgroud)

网页.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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Cra*_*der 6

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

会修改Tomcat配置CATALINA_BASE,直接从它的部署神器输出目录,以避免复制这可能需要很多额外的时间,特别是对于大型项目的文件。

hi_war_exploded是在 Tomcat 运行/调试配置,部署选项卡中配置的上下文。

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

当您打开http://localhost:8080/hi_war_exploded/ URL 时,Tomcatindex.jsp从应用程序的 Web 资源根目录提供服务。

$END$新 JSP 文件模板的一部分。在项目中创建新的 JSP 文件时,光标将放置在此位置。

当项目向导生成 Web 应用程序项目并index.jsp从模板放置文件时,它不会展开$END$宏,因此它出现在JSP 文件中。这实际上IntelliJ IDEA 中的一个已知错误

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

为了使它可以在HTTP://本地主机:8080 / HW网址,而不是你需要更改应用程序上下文/作为显示在这个截图:

部署上下文