In my servlet class, I have annotated the class with:
@WebServlet("/OnlinePostListener/testFromAnnotation")
public class OnlinePostListener extends HttpServlet {
...
}
My web.xml contains the following:
<servlet>
<description>
</description>
<display-name>OnlinePostListener</display-name>
<servlet-name>OnlinePostListener</servlet-name>
<servlet-class>com.me.forwardingProxy.OnlinePostListener</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>OnlinePostListener</servlet-name>
<url-pattern>/testFromWebXML</url-pattern>
</servlet-mapping>
My servlet only responds when I access the URL:
http://localhost:8080/forwardingProxy/OnlinePostListener/testFromAnnotation
but not:
http://localhost:8080/forwardingProxy/OnlinePostListener/testFromWebXML
What is the difference between the @WebServlet's annotation and servlet-mapping? Why is the servlet-mapping not working for this URL-pattern?
Because the Servlet specification requires that mappings defined in web.xml override rather than add to those defined in annotations. The reason is that without this, there would be no way to disable a mapping defined in an annotation.