• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Invoke servlet from servlet

 
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic