M
M
Max_Tr2019-04-06 17:18:40
Java
Max_Tr, 2019-04-06 17:18:40

Why does the "Class servlets.MainServlet is not a Servlet" error occur?

Why can't I reach the servlet?
Prescribed everything seems to be correct, but I can’t reach the servlet with
an error:
type Exception report

message Class servlets.MainServlet is not a Servlet

description The server encountered an internal error that prevented it from fulfilling this request.

exception javax.servlet.ServletException

: Class servlets.MainServlet is not a Servlet ) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) org.apache.coyote.AbstractProtocol
$AbstractConnectionHandler.process(AbstractProtocol.java:603)
run(JIoEndpoint.java:312)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
java.base/java.lang.Thread.run(Thread.java:834)


pom.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 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_3_1.xsd"
         version="3.1"
         metadata-complete="true">

  <display-name>Tomcat Documentation</display-name>
  <description>
    Tomcat Documentation.
  </description>


  <servlet>
    <servlet-name>testServlet</servlet-name>
    <servlet-class>servlets.MainServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>testServlet</servlet-name>
    <url-pattern></url-pattern>
  </servlet-mapping>
</web-app>

MainServlet.java
package servlets;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("")
public class MainServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("WEB-INF/views/main.jsp").forward(req,resp);

    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kolman, 2019-04-07
@Evgeny_13

In you need to specify how this servlet will be mapped, if there is no special mapping, then you need to put "/". (/)
The annotation name is specified in several ways:
1. WebServlet("/YourName")
2. WebServlet(name = "YourName")
In your case, you simply do not specify anything in this annotation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question