-2

I'm trying to run my simple servlet "Hello".
I have installed tomcat 9.0.6
Than i create a new JavaEE web project in Idea (called test)... after i create new package in src (called servlet), after new servlet file "myServlet".
Project Structure
Here is code:

public class myServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("text/html");

    PrintWriter out = response.getWriter();

    out.println("<html><body>");

    out.println("<h2>Hello my first Servlet</h2>");
    out.println("<br/>");
    out.println("Time on the server is: " + new java.util.Date());

    out.println("</body></html>");
}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
}

Than i open web.xml and add next lines for servlet and servlet-mapping:

<?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>myServlet</servlet-name>
    <servlet-class>servlet.myServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>myServlet</servlet-name>
    <url-pattern>/myServlet</url-pattern>
</servlet-mapping>
</web-app>

After successfully running tomcat ("Artifact is deployed successfully") i try to visit url: http://localhost:8080/test/myServlet
Result was page 404...
But if i change url to: http://localhost:8080/myServlet
result was correct.
Result
What's wrong with url: http://localhost:8080/test/myServlet ???
Need some libraries or what ???
Where is my mistake ???

Also i try with @WebServlet("/myServlet") annotation without servlet and servlet-mapping lines in web.xml - result the same.

layjel
  • 121
  • 1
  • 12

1 Answers1

0

You need to define the context path in context.xml - please see this link http://tomcat.apache.org/tomcat-9.0-doc/config/context.html#Defining_a_context

Alok Sinha
  • 171
  • 1
  • 3