• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Invoke servlet from servlet

 
Naren Chivukula
Ranch Hand
Posts: 577
Java Notepad Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Ottinger
Ranch Hand
Posts: 125
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naren look into the Jakarta Commons HttpClient project. I use this myself for invoking inter-application servlets. It works pretty well.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sharad Agarwal
Ranch Hand
Posts: 167
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Naren Chivukula
Ranch Hand
Posts: 577
Java Notepad Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13049
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 577
Java Notepad Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 577
Java Notepad Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic