How do I call one servlet from another servlet?
[ Short answer: there are several ways to do this, including a) use a RequestDispatcher b) use a URLConnection or HTTPClient c) send a redirect d) call getServletContext().getServlet(name) (deprecated, doesn’t work in 2.1+) – Alex ] It depends on what you mean by “call” and what it is you seek to do and why you seek to do it. If the end result needed is to invoke the methods then the simplest mechanism would be to treat the servlet like any java object , create an instance and call the mehods. If the idea is to call the service method from the service method of another servlet, AKA forwarding the request, you could use the RequestDispatcher object. If, however, you want to gain access to the instance of the servlet that has been loaded into memory by the servlet engine, you have to know the alias of the servlet. (How it is defined depends on the engine.
[ Short answer: there are several ways to do this, including • use a RequestDispatcher • use a URLConnection or HTTPClient • send a redirect • call getServletContext().getServlet(name) (deprecated, doesn’t work in 2.1+) – Alex ] It depends on what you mean by “call” and what it is you seek to do and why you seek to do it. If the end result needed is to invoke the methods then the simplest mechanism would be to treat the servlet like any java object , create an instance and call the mehods. If the idea is to call the service method from the service method of another servlet, AKA forwarding the request, you could use the RequestDispatcher object. If, however, you want to gain access to the instance of the servlet that has been loaded into memory by the servlet engine, you have to know the alias of the servlet. (How it is defined depends on the engine.) For example, to invoke a servlet in JSDK a servlet can be named by the property myname.code=com.sameer.servlets.MyServlet The code below