This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Servlets and the fly likes Invoke servlet from servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Invoke servlet from servlet" Watch "Invoke servlet from servlet" New topic

Invoke servlet from servlet

Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 577


I'd like to know how we can invoke a remote servlet running on different machine from the servlet running on localhost? And I want to maintain the session same.

Thanks in advance,

(OCEEJBD6, SCWCD5, SCDJWS, SCJP1.4 and Oracle SQL 1Z0-051)
Mike Ottinger
Ranch Hand

Joined: Jan 11, 2002
Posts: 125
Hi Naren look into the Jakarta Commons HttpClient project. I use this myself for invoking inter-application servlets. It works pretty well.

SCJP 1.4 SCJD 1.5
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

And I want to maintain the session same.

You're not going to be able to share a session across two different applications.
[ June 24, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Perhaps if you tell us a little more about what it is you are trying to achieve, maybe we can help find a different solution.

Alco-Haul: We move spirits.
Demented Deliberations of a Dilettante
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 577

Ok! I got folks. It's simple. Use to connect to the remote sevlet.

But can someone tell whether we can pass a Request object to other remote servlet? This is what all I want.

Thanks and Regards,
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
A request object belongs to the servlet container/web application it is operating in so you can't "pass" it. You could create a new HttpURLConnection to a remote servlet exactly duplicating the contents of the first request - that will create a request on the remote servlet.
As Mike said - HttpClient is a good toolkit for this sort of thing.
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 577


I'm not understanding that HttpClient. Which project should I to refer? Studying all the projects makes no sense for me. Someone atleast show a light so that I can go through that.

Thanks and Regards,
Narendranath Chivukula
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 577

Though I don't get enough information from this forum for the question which I asked, I write the solution which I got after putting lots of effort.

1. Make a connection with the remote servlet using* API.
2. Open an InputStream and read the whole servlet like

3. Point (2) makes that remote servlet to be run.
4. Maintaining same session:

In order to maintain same session, you need to send your request object to the remote servlet. Normally we do create RequestDispatcher object and forward the request object to our local servlet. I think this technique doesn't work for remote servlets. So, open a OutputStream and send your parameters like this(you can also send directly using URL)


Now, in the remote servlet you can read the parameters passed through OutputStream like this.

String user=request.getParameter("user");
String password=request.getParameter("password");
Note: All the parameters are being read only in String. Convert these strings accordingly to get in other primitive types. For objects, I didn't try. If someone experiments on this and write to this forum, it will be grateful.

Hope I won't miss anything. Let me correct if I'm wrong somewhere.

I agree. Here's the link:
subject: Invoke servlet from servlet
It's not a secret anymore!