4
<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>

According to me above code is used to map servlet-class with its url-pattern.
But I have a doubt in my mind that why they (java experts) had done it this way (why have they made the servlet and servlet-mapping two separate tags).
They could have done something as mentioned below:

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

This may make it more simple. What is the need of servlet-name in previous mentioned code?

Please help me to clear my doubt.

Thank you in advance.

Prakash K
  • 11,669
  • 6
  • 51
  • 109
Mitesh
  • 477
  • 1
  • 6
  • 22
  • 1
    The question should show some effort of research. Try using google or other answers that can help , before asking a similar question – Prateek Aug 28 '12 at 06:45
  • 1
    @Sajith My question is something different than your suggested link. I want to ask that 'Why we need servlet-name?' In second code block I wrote the code which serve the mapping between servlet-class and url-pattern. – Mitesh Aug 28 '12 at 06:50
  • 1
    You question might be answered [here](http://stackoverflow.com/a/8198327/468763) – Prakash K Aug 28 '12 at 07:26
  • 2
    Using servlet name, You can map single Servlet to multiple url patterns and it allows to map filters with particular Servlet. See here : http://stackoverflow.com/questions/8198312/servlet-mapping-using-web-xml/8198327#8198327 – Hardik Mishra Aug 28 '12 at 08:59

3 Answers3

0

In web.xml you use servlet-name as unique reference to your servlet. This name indentifies the servlet and can be used with filters and so on. It isn't just a matter of mapping your servlet to a specific URL.

You can look at it as an alias.

FrancescoAzzola
  • 2,666
  • 2
  • 15
  • 22
0

Name always will be the unique identification for classes. Here serlvelt-class may come two times, but whatever we are calling servlet should be unique. That will identified by the servlet name. If you see in struts1 or struts2 or spring will have names offcourse in spring id. but identification should be mandatory whether it is in name or ID.

In servlet also servlet class is triggered by servlet name only not by servlet class.because one class will perform amny different actions. Each action should be identify by the name.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
rama
  • 2,157
  • 1
  • 11
  • 2
  • In the future, do not include unrelated links to your site in your posts. Doing so will get your posts and account marked as Spam. I am removing the link from this post for you. – Andrew Barber Aug 28 '12 at 12:18
0

In web.xml, we first configure the servlet using the < servlet > element providing a unique name i.e in < servlet-name > tag and writes the class name of the servlet in < servlet-class > tag.

Second, we map this servlet to a URL or URL pattern using < servlet-mapping > element. < servlet-name > element is used to specify the name of the servlet, which should be called for an incoming URL matching the pattern specified as the value of the < url-pattern > element.

eboix
  • 5,113
  • 1
  • 27
  • 38
Swapnal
  • 279
  • 2
  • 3