File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes Application and servlet communication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Application and servlet communication" Watch "Application and servlet communication" New topic
Author

Application and servlet communication

zafar ans
Greenhorn

Joined: Apr 02, 2011
Posts: 4
Hello everyone. This is my first post..
For my current project I had to communicate with a servlet from a swing GUI application. I opened an ObjectOutputStream from the servlet and using the URL class' openStream() method I opened an ObjectInputStream at the client. Then I successfully sent an object from the servlet to the client application.
But I tried to write an object to the servlet and failed. Since there is no method in the URL class to get an OutputStream to the servlet I had to get the URLConnection objectfrom URL and then call its methods in the following order:
Client side:


Servlet: (in doGet())


This code doesn't yield any output.. Infact I found out that the servlet's doGet isn't getting the call at all. There were no exceptions raised during the process either at the client or the server.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4384
    
    8

Hi Zafar. Welcome to The Ranch!

The immediate thing that springs to mind is that doGet is only going to be called in response to an HTTP GET request. But you've just opened a connection and written an object to it - there's nothing that specifies the request.

Edit: sorry, on second thoughts, I think GET is the default. So it's probably not that. Might be worth adding anyway, to make it explicit. If you cast the UrlConnection to an HttpUrlConnection you can call setRequestMethod.

(Your post is easier to read if you UseCodeTags - I've added them this time.)
zafar ans
Greenhorn

Joined: Apr 02, 2011
Posts: 4
Matthew Brown wrote:Hi Zafar. Welcome to The Ranch!

The immediate thing that springs to mind is that doGet is only going to be called in response to an HTTP GET request. But you've just opened a connection and written an object to it - there's nothing that specifies the request.

Edit: sorry, on second thoughts, I think GET is the default. So it's probably not that. Might be worth adding anyway, to make it explicit. If you cast the UrlConnection to an HttpUrlConnection you can call setRequestMethod.

(Your post is easier to read if you UseCodeTags - I've added them this time.)


Thank you, I'll use tags the next time.
After implementing your suggestion the doGet() got called finally!! But I got the following exceptions at the server:
(My servlet's name is TimeServlet)




The line at which the exceptions were raised was
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4384
    
    8

OK, so it looks like the stream header hasn't been written.

It's been a bit since I've done this, so I'm, really not sure. But in the one example I've found where I've done something similar, there seem to have been two differences that might be important:

- a POST request was used (sending a serialized object in a GET request seems a bit strange - don't know if it's actually invalid or not though - whereas a POST request has a request body where the object can be stored)

- the object was written to the stream before calling connect() - that was the last action

If neither of those work, hopefully someone more knowledgeable will be along soon!
zafar ans
Greenhorn

Joined: Apr 02, 2011
Posts: 4
Matthew Brown wrote:OK, so it looks like the stream header hasn't been written.

It's been a bit since I've done this, so I'm, really not sure. But in the one example I've found where I've done something similar, there seem to have been two differences that might be important:

- a POST request was used (sending a serialized object in a GET request seems a bit strange - don't know if it's actually invalid or not though - whereas a POST request has a request body where the object can be stored)

- the object was written to the stream before calling connect() - that was the last action

If neither of those work, hopefully someone more knowledgeable will be along soon!

Thanks for your help, I do appreciate it.
I tried putting my code from doGet to doPost. Also I tried waiting for an arbitrary time at the servlet before reading the object. The same exceptions got raised.
Please help!
zafar ans
Greenhorn

Joined: Apr 02, 2011
Posts: 4
I found the solution.
The package com.oreilly.servlet has a class HttpMessage which has the method sendPostMessage(Object o). Pretty neat!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Application and servlet communication