Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need a servlet name?

I have been readin Head First JSP and Servlet, I see that the web.xml has

  <!-- To name the servlet -->
  <servlet>                                    
    <servlet-name>ServletName</servlet-name>
    <servlet-class>packy.FirstServlet</servlet-class>
  </servlet>

  <!-- For URL's to map to the correct servlet -->
  <servlet-mapping>
    <servlet-name>ServletName</servlet-name>
    <url-pattern>/ServletURL</url-pattern>
  </servlet-mapping>

Why hide the original servlet's location ? I can simply see that it is for security reason and some more such kinda advantages, but why have a name for each servlet ? Why can't the web.xml be simple like

  <servlet>                                    
    <url-pattern>ServletURL</url-pattern>
    <servlet-class>packy.FirstServlet</servlet-class>
  </servlet>
like image 561
vikkyhacks Avatar asked Nov 19 '13 14:11

vikkyhacks


People also ask

What is the significance of servlet-name?

The servlet-name element declares a name for this particular servlet instance. Each servlet instance in a context must have a unique name. However, the name is only used to associate URL mappings with this instance, and need not correspond to the name of the servlet class or the URL of the servlet.

What should the servlet-name be?

The name for each servlet must be unique across the deployment descriptor. The <servlet-mapping> element specifies a URL pattern and the name of a declared servlet to use for requests whose URL matches the pattern.

What is servlet and why do we need it?

A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers.


1 Answers

It allows you to have multiple servlet mappings on a single servlet instance (even spread over multiple web.xml/web-fragment.xml files) without the unnecessary need to create a separate instance per mapping:

<servlet>
    <servlet-name>someServlet</servlet-name>
    <servlet-class>com.example.SomeServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>someServlet</servlet-name>
    <url-pattern>/enroll</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>someServlet</servlet-name>
    <url-pattern>/pay</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>someServlet</servlet-name>
    <url-pattern>/bill</url-pattern>
</servlet-mapping>

(note: yes, you can have multiple URL patterns per mapping, but that wouldn't cover them being split over multiple web.xml/web-fragment.xml files)

It allows you to map filters on the particular servlet without worrying about what URL patterns the servlet is/would be using:

<filter-mapping>
    <filter-name>someFilter</filter-name>
    <servlet-name>someServlet</servlet-name>
</filter-mapping>

Your proposal would support neither of them.

Note that since Servlet 3.0, which is out for almost 4 years already (December 2009; please make sure that you learn the matters by up to date resources ... anything older than 1~3 years should be carefully reviewed), you can easily use the @WebServlet annotation to minimze web.xml boilerplate:

@WebServlet("/servletURL")
public class SomeServlet extends HttpServlet {}

Just solely this annotation already maps it on an URL pattern of /servletURL without any web.xml entry.

like image 113
BalusC Avatar answered Nov 02 '22 23:11

BalusC