Explanation for servlet and servlet mapping in web.xml

<servlet> <servlet-name>PerformReg</servlet-name> <servlet-class>com.PerformRegistartion</servlet-class> </servlet> <servlet-mapping> <servlet-name>PerformReg</servlet-name> <url-pattern>/PerformReg</url-pattern> </servlet-mapping> 

In my opinion, the code is used to display the servlet-class with its url-pattern .
But I have doubts about why they (java experts) did it this way (why they made two servlet and servlet-mapping tags).
They could do something as follows:

 <servlet> <servlet-class>com.PerformRegistartion</servlet-class> <url-pattern>/PerformReg</url-pattern> </servlet> 

This can make it simpler. What is the need for servlet-name in the previous code?

Please help me clear my doubts.

Thanks in advance.

+4
source share
3 answers

In web.xml, you use the name of the servlet as a unique reference to your servlet. This name defines the servlet and can be used with filters, etc. This is not just a matter of mapping a servlet to a specific URL.

You can look at it as an alias.

0
source

The name will always be a unique identifier for classes. Here the serlvelt class may appear twice, but everything that we call a servlet must be unique. This will be determined by the name of the servlet. If you see in struts1 or struts2 or spring, then the names will be indicated in the spring id. but identification must be required, be it name or identifier.

In the servlet, the servlet class is also launched by the name of the servlet, not only by the servlet class. Because one class will perform amny different actions. Each action must be identified by name.

0
source

In web.xml we first configure the servlet using the < servlet > element, which provides the unique name ie in the < servlet-name > and writes the name of the servlet class to the < servlet-class > .

Secondly, we map this servlet to a URL or a URL using the < servlet-mapping > element. The < servlet-name > element is used to specify the name of the servlet to be invoked for the incoming URL that matches the pattern specified as the value of the < url-pattern > element.

0
source

All Articles