T
T
Tushkantchik2018-03-07 21:41:23
Java
Tushkantchik, 2018-03-07 21:41:23

How to properly compile and run a servlet (Tomcat)?

An error occurs when trying to start the servlet.
Project structure.
5aa0314dc5b26893094720.png
web.xml

web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
<welcome-file-list>
    <welcome-file>
        myPage.html
    </welcome-file>
</welcome-file-list>



    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>D:\MySite\out\production\MySite\ua\user\project\web\MyServlet.class</servlet-class>

    </servlet>


    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>


</web-app>

Servlet source
package ua.user.project.web;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter wr = response.getWriter();
        wr.write("Hallo!!");
    }
}

This is how I compiled the servlet
5aa032397a766390236852.png
. When I enter IP / test in the browser line, this error is:
5aa033eb8a77b049676883.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2018-03-07
@Tushkantchik

Because the servlet-class tag should contain not the absolute path to the class file, but the full class name - ua.user.project.web.MyServlet.

S
sviato_slav, 2018-03-14
@sviato_slav

Error in line in web.xml:
D:\MySite\out\production\MySite\ua\user\project\web\MyServlet.class Needed
:
ua.user.project.web.MyServlet

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question