Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

one servlet calling another servlet

 
jak lodhi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how one servlet calls another servlet programatically ?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain your question in a little more detail?
Servlets don't call one another.

What is it you're trying to do?
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sandeep Sundaram
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic