Java – Why use in web.xml(Deployment Descriptor) in jsp/servlet

javajspservlets

<servlet>
    <servlet-name>Hello</servlet-name>
    <servlet-class>HelloWorld</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>/HelloWorld.do</url-pattern>
</servlet-mapping>

Why do we use url-pattern inside servlet-mapping tag. Why not in servlet tag itself.

It's seems just an extra tag to write.

Is it just because servlet/jsp spec writers decided to do so or has it some logical reason behind its existance ?

Best Answer

This is more likely due to the fact that servlets were intended to support multiple protocols, and not just HTTP. URL patterns are specific to HTTP alone, and therefore the mapping of the servlet to HTTP URL patterns is done in a servlet-mapping tag, instead of the servlet tag which is used for declaring the more generic properties of the servlet.