File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Accessing one servlet from another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Accessing one servlet from another" Watch "Accessing one servlet from another" New topic

Accessing one servlet from another

Harri Elef

Joined: Feb 04, 2008
Posts: 2

i am trying to call one servlet from another servlet. The two servlet belong to different applications but deployed on the same tomcat.

Can someone tell me what is the best way to do that?

Thanks a lot

Harri Elef
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

ServletContext.getContext(String uripath) can get a reference to another servlet context, and from there you can access resources in that context such as Servlets. There are a few things to keep in mind:

1) Depending on the container, you will need to look at the documentation to see if you need configuration settings to allow this. By default it is not allowed.
2) Communication with the other Servlet may be via another Thread, so the the data may not arrive as you expect.
3) cross context communication is typically poorly implemented in containers, and is poorly defined in the spec.
[ February 04, 2008: Message edited by: David O'Meara ]
I agree. Here's the link:
subject: Accessing one servlet from another
It's not a secret anymore!