How you can activate or call one another servlet method?

Questions by sudhakar_bvr   answers by sudhakar_bvr

Showing Answers 1 - 4 of 4 Answers

B.Shyam Sundar

  • Jun 12th, 2006
 

To call the service method of the another servlet from other servlet

1)if both servlets are part of the same web application

user ServletContext application=config.getServletContext();

 RequestDispatcher rd=application.getRequestDispatcher("/anotherServlet");

use

rd.include(request,response);

if you want to get the output generated by service method of both the servlets

or use rd.forward(request,response);

if you want to get output of only the called servlet

2)if both the servlets are part of different application with the name app1 and app2 i.e SOne belongs app1 & STwo belongs to app2.and running in the same container.

then

to invoke the service method of STwo from the SOne

use

ServletContext sone_context=config.getServletContext();

ServletContext stwo_context=sone_context.getContext();

RequestDispatcher rd=stwo_context.getRequestDispatcher("/url/of/stwo");

use rd.include(request,response);

or rd.forward(request,response);

SOne is the calling servlet and STwo is the Called Servlet

3)if both the servlets SOne and STwo are part of different applications app1 and app2 and these app1 and app2 running in different container

in service method of sone use

response.sendRedirect("url of the server");

to invoke the service method of stwo

for e.g if SOne if running in weblogic and STwo in Tomcat

the in SOne service method use response.sendRedirect("http://localhost:8080/app2/url/of/stwo");

anurag saxena

  • Nov 8th, 2006
 

thanks for details view

  Was this answer useful?  Yes

To call the service method of the another servlet from other servlet
1)if both servlets are part of the same web application
user ServletContext application=config.getServletContext();
 RequestDispatcher rd=application.getRequestDispatcher("/anotherServlet");
use  rd.include(request,response);
if you want to get the output generated by service method of both the servlets
or use rd.forward(request,response);
if you want to get output of only the called servlet

2)if both the servlets are part of different application with the name app1 and app2 i.e SOne belongs app1 & STwo belongs to app2.and running in the same container.
then to invoke the service method of STwo from the SOne
use ServletContext sone_context=config.getServletContext();
ServletContext stwo_context=sone_context.getContext();
RequestDispatcher rd=stwo_context.getRequestDispatcher("/url/of/stwo");
use rd.include(request,response);
or rd.forward(request,response);
SOne is the calling servlet and STwo is the Called Servlet

3)if both the servlets SOne and STwo are part of different applications app1 and app2 and these app1 and app2 running in different container
in service method of sone use
response.sendRedirect("url of the server");
to invoke the service method of stwo
for e.g if SOne if running in weblogic and STwo in Tomcat
the in SOne service method use response.sendRedirect("http://localhost:8080/app2/url/of/stwo");

  Was this answer useful?  Yes

Hi,
Good to see the ways of communication between servlets.
My question goes as: The servlet communication when they are running in different servlet container. sOne and sTwo are two servlets, here can we include sTwo's output in response of sOne's?
Thank you.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions