0

I am new in Servlets and web.xml content. I would like to set a condition/dependency on two different servlets. Consider I have below two servlets:

<servlet>
        <servlet-name>servlet1</servlet-name>
        <servlet-class>com.my.app.Servlet1</servlet-class>
        <load-on-startup>1</load-on-startup>
</servlet>

<servlet>
        <servlet-name>servlet2</servlet-name>
        <servlet-class>com.my.app.Servlet2</servlet-class>
        <load-on-startup>2</load-on-startup>
</servlet>

And I would like to invoke servlet2 only in case servlet1 encounters an error or fails. As to be a scenario: Assume servlet1 is my main authenticator and the user login failed due to missing parameter in URL in servlet1. In that case I would like to try the second authenticator servlet2 that contains different set of checks...

Can I achieve this by modifying web.xml only? If not, what would be an alternative solution to accomplish this need?

Note: I do have the source code of servlet1 but not the servlet2 (only built jar)...

Thanks in advance.

Psyduck
  • 84
  • 2
  • 9

1 Answers1

0

You could do this using reuqestDispatcher and forward the request to the other servlet2 like following in Servlet 1s doPost method.

//Here URL is path to servlet 2
RequestDispatcher rd  = request.getRequestDispatcher(“url”);

rd.forward(request,response);
rjoseph
  • 11
  • 2
  • Isn't there an option in web.xml checking a certain parameter in URL and based on that invoking the respective servlet? – Psyduck Sep 12 '18 at 14:38