Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploy servlet with IntelliJ IDEA to local Tomcat server

Trying to deploy simple servlet to Tomcat server. After select Run Tomcat... I'm redirected to http://localhost:8080/hi_war_exploded/ with webpage with one word - $END$. Logfiles reports no error.

I was expecting to see hw folder with my application in tc9\webapps, but found nothing.

What does the $END$ means? Where is my application on TomCat server? How to put my servlet to TomCat server?

Servlet:

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>
like image 740
vico Avatar asked Feb 15 '19 16:02

vico


People also ask

Can I use Tomcat with IntelliJ?

If you're not creating a new project from scratch and instead have an existing project that runs on Tomcat, you can configure IntelliJ IDEA Ultimate to connect to your existing Tomcat installation. Let's work with this application from GitHub: Clone the Project in IntelliJ IDEA and then go to Run > Edit Configurations.

Can we use servlet in IntelliJ?

Define the servlet elementUltimate The Servlet element also contains definitions of initialization attributes. IntelliJ IDEA provides the facilities to configure, remove, and edit Servlet elements using the IntelliJ IDEA editor.


1 Answers

IntelliJ IDEA doesn't copy the webapp files into TOMCAT\webapps.

It modifies Tomcat configuration in CATALINA_BASE and deploys the artifact directly from its output directory to avoid copying the files which can take a lot of extra time, especially for the large projects.

hi_war_exploded is the context configured in Tomcat Run/Debug configuration, Deployment tab.

In the root of this context you have the default index.jsp page generated by IntelliJ IDEA on project creation.

When you open http://localhost:8080/hi_war_exploded/ URL, Tomcat serves index.jsp from the Web Resource root of your application.

$END$ is a part of the the new JSP file template. When you create a new JSP file in a project, cursor is placed at this location.

When the project wizard generates the Web Application project and places index.jsp file from the template, it doesn't expand the $END$ macro, so it appears in the JSP file. It's actually a known bug in IntelliJ IDEA.

Your servlet is available at http://localhost:8080/hi_war_exploded/hw URL.

To make it available at http://localhost:8080/hw URL instead you need to change the Application context to / as shown on this screenshot:

Deployment context

like image 59
CrazyCoder Avatar answered Sep 27 '22 19:09

CrazyCoder