It's not a secret anymore!
The moose likes Servlets and the fly likes one servlet calling another servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "one servlet calling another servlet" Watch "one servlet calling another servlet" New topic

one servlet calling another servlet

jak lodhi

Joined: May 16, 2006
Posts: 6
how one servlet calls another servlet programatically ?
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Can you explain your question in a little more detail?
Servlets don't call one another.

What is it you're trying to do?

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Charles Lyons
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
I would guess the RequestDispatcher is what you're looking for - it's the closest thing towards having one servlet "invoke" another (i.e. pass request handling to another). There's no way (indeed there is no need) to invoke a servlet instance directly. Please clarify for further advice.

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Sandeep Sundaram

Joined: Mar 22, 2005
Posts: 9
javax.servlet.http.HttpServlet servlet1 = (javax.servlet.http.HttpServlet)getServletContext().getServlet("servlet2");
servlet1.doGet(req, res);

where servlet2 is the name given in web.xml file
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
According to the javadocs, the getServlet(String) method has been returning "null" ever since Servlet API 2.1. Go with the RequestDispatcher approach.
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
Please bear in mind, though, that getting one servlet to call another is like going out into the street and knocking on your own front door just to ask someone to pass the salt.

As a reality check, ask yourself what the servlet you are calling actually does, and if you can make that behaviour available to both servlets by something like s simple method call.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
I agree. Here's the link:
subject: one servlet calling another servlet
It's not a secret anymore!