lsiva
lsiva

Reputation: 485

servlet extending another servlet

I was going thorugh a recent code which was written in servlet. The servlet hierarchy is like this, There is a servlet A which is extended by servlet B extended by servlet C,D,E,F.

Now in my web.xml I have 5 urls configured for these servlets all are mapped to servlet A. So my mapping looks like

URL- /downloadservlet - Class - Servlet A URL- /readcontent - Class servlet A URL- /getdetails - Class servlet A .. etc etc

Now when I wanted to execte the doPost method in servlet D how can I achieve it? because this request can also be served by C or E or F.

The application works correctly but I could not understand how the request is routed to the correct servlet. Any explaination on this is very much appreciated.

Upvotes: 0

Views: 1328

Answers (1)

Oleg Gryb
Oleg Gryb

Reputation: 5249

Servlet itself has nothing to do with inheritance. It will call exactly the same class as provided in servlet tag of your web.xml file, e.g. if you want to map a class org.company.D to a servlet, you would need to write something like this:

<servlet>
    <servlet-name>servletd</servlet-name>
    <servlet-class>org.company.D</servlet-class>
</servlet>

and then map the 'servletd' to a URL that it should listen on:

<servlet-mapping>
    <servlet-name>servletd</servlet-name>
    <url-pattern>/some-path</url-pattern>
</servlet-mapping>

Upvotes: 2

Related Questions