When I open my project, I get the error:
Servlet should have a mapping
and the other error:
Cannot resolve Servlet
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>
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.
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.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With