Link to home
Start Free TrialLog in
Avatar of jussara
jussara

asked on

HOWTO invoke a method on another Servlet from my Servlet?

Hello!

How can my Servlet invoke a public method on another Servlet?

Thanks.
Avatar of dviji
dviji

By using " InterServlet Communication" we will achive it.

Example:-

ServletContext sc = getServletConfig().getServletContext();
Enumeration  servletNames = sc.getServletNames(); //will return the available servlet names
MyServlet myServlet = (MyServlet)sc.getServlet("servletName "); //get the MyServlet instance from ServletContext
String name = myServlet.getNames(); //execute the public method.


...dviji





Hmmm, interesting.  I was reading that the method 'getServletNames()' is deprecated.  It wont be along much longer.  So, I would guess in such a situation the use of reflection would work.  But it is an interesting problem.  

Tom
ASKER CERTIFIED SOLUTION
Avatar of Jim Cakalic
Jim Cakalic
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
interservlet communication was deprecated and will be (has been?) removed in later versions because of security reasons.

The best way to achieve it is by having the servlets call one another over HTTP, and having the service method throw back the response based on the parameters passed (passing in the function to be called as a parameter, and parameters to it as other parameters for example).
jussara:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
Recommendation: points to jim_cakalic
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for jim_cakalic

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer