File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes 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 "Servlet calling another servlet..." Watch "Servlet calling another servlet..." New topic

Servlet calling another servlet...

Jason Kim

Joined: Apr 13, 2003
Posts: 17
Can a servlet load another servlet in different J2EE application ?
If so, could you show me how ?
Code example would be much appreicated.
Ashik Sherfudeen

Joined: May 09, 2003
Posts: 2

I think v can use Request Despatcher Interface for this. Use forward method to load the server which is in another j2ee application.
If this answer is wrong could u explain why it is wrong.

Every thing is possible.
Amol Desai
Ranch Hand

Joined: Jan 29, 2003
Posts: 82
You cannot use the forward method of RequestDispatcher interface.
A forward method allows you to redirect only to a resource in the same Web application.
The sendRedirect method, on the other hand, allows you to redirect to any URL.
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
Moving to the Servlets forum...
Jason Kim

Joined: Apr 13, 2003
Posts: 17
Servlet in different applications within same virtual host can call each other.
ServletContext context = getServletContext('context-root-name');
If applications are in different host, one has to use URL connection or sendRedirect ?
Kaustubh Patil
Ranch Hand

Joined: Aug 13, 2001
Posts: 164
I think using a sendRedirect() would be a solution.
forward() may have problems because redirecting to the different resource takes place on the server side in this case where as in the case of sendRedirect() a browser is made to generate a new request to the resource (which could be anything - maybe on a different server).
Pl. correct me if i am wrong or have missed anything.

Kaustubh. Mumbai, India.
I agree. Here's the link:
subject: Servlet calling another servlet...
It's not a secret anymore!