Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlet should have a mapping and cannot resolve Servlet

When I open my project, I get the error:

Servlet should have a mapping

and the other error:

Cannot resolve Servlet

enter image description here

But in the below code you can see every servlet have its mapping:

In my web.xml, I have 8 servlet, and every servlet has its corresponding mapping:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>loginservlet</servlet-name>
    <servlet-class>com.sam.servlet.loginservlet</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>changeservlet</servlet-name>
    <servlet-class>com.sam.servlet.changeservlet</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>Searchservlet</servlet-name>
    <servlet-class>com.sam.servlet.Searchservlet</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>borrowservlet</servlet-name>
    <servlet-class>com.sam.servlet.borrowservlet</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>mealselectservlet</servlet-name>
    <servlet-class>com.sam.servlet.mealselectservlet</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>mealaddservlet</servlet-name>
    <servlet-class>com.sam.servlet.mealaddservlet</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>mealdeletedservlet</servlet-name>
    <servlet-class>com.sam.servlet.mealdeletedservlet</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>mealupdateservlet</servlet-name>
    <servlet-class>com.sam.servlet.mealupdateservlet</servlet-class>
  </servlet>

<!-- mapping -->

  <servlet-mapping>
    <servlet-name>loginservlet</servlet-name>
    <url-pattern>/loginservlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>changeservlet</servlet-name>
    <url-pattern>/changeservlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Searchservlet</servlet-name>
    <url-pattern>/Searchservlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>borrowservlet</servlet-name>
    <url-pattern>/borrowservlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>mealselectservlet</servlet-name>
    <url-pattern>/mealselectservlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>mealaddservlet</servlet-name>
    <url-pattern>/mealaddservlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>mealdeletedservlet</servlet-name>
    <url-pattern>/mealdeletedservlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>mealupdateservlet</servlet-name>
    <url-pattern>/mealupdateservlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
like image 723
user7693832 Avatar asked Apr 01 '17 07:04

user7693832


People also ask

Why do we require mapping of a servlet?

Servlet mapping specifies the web container of which java servlet should be invoked for a url given by client. It maps url patterns to servlets. When there is a request from a client, servlet container decides to which application it should forward to. Then context path of url is matched for mapping servlets.

In which file do we define a servlet mapping?

You define servlets as a part of a Web application in several entries in the J2EE standard Web Application deployment descriptor, web. xml. The web. xml file is located in the WEB-INF directory of your Web application.

What is servlet mapping in spring boot?

Servlet mapping can be achieved either by using ServletRegistrationBean or by using @ServletComponentScan annotation in Spring Boot. ServletRegistrationBean registers Servlet as Spring bean. @ServletComponentScan scans Servlet annotated with @WebServlet .


1 Answers

It's a known issue that is already fixed in the current IntelliJ IDEA version. For the older versions there is a workaround:

please open Project Structure | Modules | Web Facet and add your web.xml as Deployment Descriptor.

like image 173
CrazyCoder Avatar answered Dec 09 '22 03:12

CrazyCoder