Calling Servlet from Servlets in Java


About author  I am Java/oracle professional.Working on Java/J2EE technologies and i.e Java,J2ee,Oracle ADF,hibernate,J2ee,PL/sql,Apps for 4+ years.I am passionate about learning new technologies.I am sharing my knowledge. Give your views and suggestion on vinay@techartifact.com http://www.linkedin.com/in/vinaykumar2 Read more from this author


I gathered information from environment and sharing you this information with all.
When Ever you want to call any servlet from another servlet We can use two ways:-

  • A servlet can make an HTTP request of another servlet. Opening a connection to a URL
  • A servlet can call another servlet’s public methods directly, if the two servlets run within the same server.

I will let you know the second way to calling the servlet. To call another servlet’s public methods directly, you must:

  • You Should know the name of servlet that you want to call.
  • Acquire access to that servlet’s Servlet object
  • Calling the servlet’s public method

To get the object of servlet, use the ServletContext object’s getServlet method. Get the ServletContext object from the ServletConfig object stored in the Servlet object. An example should make this clear. When the EmployeeDetail servlet calls the BookDB servlet, the EmployeeDetail servlet obtains the EmployeeDB servlet’s Servlet object like this:

Once you have the servlet object, you can call any of that servlet’s public methods. For example, the EmployeeDetail servlet calls the EmployeeDB servlet’s get getEmployeeDetail method:

You Should take care of the few things.If your servlet is following the singlethreadedModel interface then your call violate that single threaded model. Then you should implement the first way..

public class EmployeeDetail extends HttpServlet {

public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
...
EmployeeDBdatabase = (EmployeeDB)
getServletConfig().getServletContext().getServlet(employeedB);
EmployeeDetail bd = database.getEmployeeDetails(empId);
...
}
}

4 Responses to Calling Servlet from Servlets in Java


  1. MirkoNo Gravatar
    Mar 02, 2011

    Why don’t you use a RequestDispatcher?


  2. Pravin JainNo Gravatar
    Mar 02, 2011

    The method getServlet has long been deprecated for security reasons, and will not give direct access to a Servlet, you may however use the getnamedDispatcher method to get a RequestDispatcher and then forward to it.


    • RickyNo Gravatar
      Aug 16, 2011

      A bit surpisred it seems to simple and yet useful.


  3. MisterzNo Gravatar
    Mar 17, 2011

    As already said, getServlet is deprecated, other than call in HTTP or using RequestDispatcher, that are clean method of invocation, it’s possible to use the same aim of using public method, you can use ServletContext…
    Example.

    interface EmployDAO{

    Employee findById(String id);

    }

    MyServlet implements EmployeeDAO{

    init(){
    getServletConfig()
    .getServletContext()
    .setAttribute(“EMPL::DAO”,this);
    }
    }

    MyOtherServlet{

    public void doGet (HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException
    {

    EmployeeDAO dao = getServletConfig()
    .getServletContext()
    .getAttribute(“EMPL::DAO”);

    Employee empl = dao.findById(request.getParameter(“E_ID”));

    }

    }

    but i think it’s not so clean…

    BYE :-D

Leave a Reply