aspose file tools*
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "Invoke servlet from servlet" Watch "Invoke servlet from servlet" New topic
Author

Invoke servlet from servlet

Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 577

Hi,

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,
Narendranath.


Cheers,
Naren
(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
Sheriff

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 java.net.URLConnection 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,
Narendranath
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
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.
Bill
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 577

Hi,

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 java.net.* API.
2. Open an InputStream and read the whole servlet like

while(inputStream.readLine()!=null){}
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)

outputStream.write("user=XYZ&password=PQWOSL123&....");
outputStream.close();

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.

Regards,
Narendranath
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Invoke servlet from servlet