M
M
MartyMcAir2020-11-26 22:51:08
Java
MartyMcAir, 2020-11-26 22:51:08

How to run Hello World on jetty-maven-plugin with web.xml config?

I tried various examples of launching and using the jetty-maven-plugin.
Some examples work, some don't.
The mapping example, via web.xml, doesn't work for some reason.
The server starts, but when you go to the page in the browser, the servlet returns 404 along the HelloWorldServlet address
5fc006af56d14847630458.jpeg
path . this request. (When visiting localhost:8080/mySimpleApp/ - everything shows correctly, i.e. what is generated from index.jsp.) (UPD: it turns out this is not quite an error, i.e. Jetty gives it, something like .. that nothing is specified for this address in the server settings.

5fc0090be7d54485565081.jpeg
)

It also gives an error javax.servlet.AsyncContext scanned from multiple locations - and this is strange because servlets need dependency: javax.servlet-api to work, but when you add it, this is the error when starting the server.
5fc006a556a30429871586.jpeg
(UPD: changed to jetty-server dependency, and the javax.servlet.AsyncContext scanned from multiple locations error is gone.)

Project itself:
Project structure:
5fc00682cf7d0403134553.jpeg

HelloWorldServlet.java

public class HelloWorldServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.getWriter().println("Hello  World! From Servlet");
    }
}


web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <display-name>Archetype Created Web Application</display-name>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>HelloWorldServlet</servlet-name>
        <servlet-class>basePkg.servlets.HelloWorldServlet</servlet-class>
    </servlet>
    <!--     привязываем его к адресу -->
    <servlet-mapping>
        <!-- при переходя по адресу /helloWorldServlet
        Jetty - перенаправит запрос на сервлет HelloWorldServlet -->
        <!-- в метод doGet(..) -->
        <servlet-name>HelloWorldServlet</servlet-name>
        <url-pattern>/helloWorldServlet</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>org.example</groupId>
    <artifactId>simpleApp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>simpleApp Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>simpleApp</finalName>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.4.34.v20201102</version>
                <configuration>
                    <jvmArgs>-Ddebug=true</jvmArgs>
                    <httpConnector>
                        <port>8080</port>
                    </httpConnector>
                    <webApp>
                        <contextPath>/mySimpleApp</contextPath>
                    </webApp>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


index.jsp - nothing interesting there, inside html tags

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MartyMcAir, 2020-11-26
@MartyMcAir

Changed addiction

<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>

on the
<dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>9.4.34.v20201102</version>
            <scope>provided</scope>
        </dependency>

Then in the console: mvn clean install
And the error javax.servlet.AsyncContext scanned from multiple locations disappeared.
In general, my guess is that Jetty has some kind of conflict with javax either.
And for this he gave out, they say, multiple locations .. And Jetty has its own dependency for this.
(I didn’t fully understand the reasons, where would I read about it!?
Despite the fact that another project starts perfectly with javax.servlet-api dependencies.
I also found it in the answers on other resources, they say you just need to clean the m2 folder and that’s it.)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question