aspose file tools*
The moose likes Servlets and the fly likes how do transfer data from one server to another server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how do transfer data from one server to another server" Watch "how do transfer data from one server to another server" New topic
Author

how do transfer data from one server to another server

elay Raja
Greenhorn

Joined: Jun 10, 2008
Posts: 27
i want to send xml from one server to another server(ie.cross domain).How can i send it through servlet.is it possible to send it using URL class by making connection to required servlet.

For example, if there are two WebApplication1 & WebApplication2. I want to send the data from webApplication1 to webapplication2 through servlet.

Please help me to resolve this issue.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Yes, if that's the mechanism you want to use.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
elay Raja
Greenhorn

Joined: Jun 10, 2008
Posts: 27
can you send me some example code? Now the code i'm trying like this

URL urlServlet = new URL("https://111.111.111/TestServlet");
con = (HttpsURLConnection) urlServlet.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setDefaultUseCaches(false);
con.setAllowUserInteraction(true);
con.setRequestMethod("GET");
con.setRequestProperty("Content-Length", "0");
con.setRequestProperty("Content-Type", "text/html;charset=UTF-8");
ObjectOutputStream outstream = new ObjectOutputStream(con.getOutputStream());
outstream.writeObject("hi");
outstream.flush();
outstream.close();
But the above code does not reach the requred servlet in the specified URL.
Is there any problem in the code. Please help me to solve this problem
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
But the above code does not reach the requred servlet in the specified URL.
Is there any problem in the code. Please help me to solve this problem


Exactly what happens?
You do not appear to be reading the response so you are missing possible error codes. Also I dont see any provision for exception handling, there are many places in your code that could be throwing an exception which would be diagnostic.

Bill
elay Raja
Greenhorn

Joined: Jun 10, 2008
Posts: 27
thanks for the reply.I found the error.
Now i want to make connection to servlet using URL class. The above servlet is in j_security_check enabled application.
if i connect using in normal way through URL class,i am getting IOException 405 servlet response code. how to make connection using URL?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
Error 405 is the standard response code for "method not allowed" - you can look these up in the JavaDocs for HttpURLConnection class.

The method your code specifies is "GET" - apparently the responding servlet does not like a GET - which makes sense because you are trying to send data in the body of the request. Normally done with a POST or PUT.

Why are you setting Content-Length to zero?
Why are you trying to send a Java object instead of text like your Content-Type specifies?

Bill
Hendy Setyo Mulyo
Ranch Hand

Joined: Dec 01, 2004
Posts: 219

Hi Elay,

You must use PUT method. Perhaps you can see this link http://kickjava.com/996.htm
There is a brief example of using PUT method for HttpURLConnection.
[ August 20, 2008: Message edited by: Hendy Setyo Mulyo ]

Hendy Setyo Mulyo
SCJP 1.4 (95%), SCWCD 1.4 (94%)
Evgeniy Bulanov
Greenhorn

Joined: Dec 19, 2005
Posts: 23
Hi elay Raja,

U may use POST method too. It's like uploading files to server. Set enctype="multipart/form-data" and parse it by org.apache.commons.fileupload.disk.DiskFileItemFactory, org.apache.commons.fileupload.servlet.ServletFileUpload (google for it).

JK


Computers are like air conditioners - They stop working properly when you open Windows
elay Raja
Greenhorn

Joined: Jun 10, 2008
Posts: 27
hi all,I found the error.Now it works fine.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Did you feel like sharing?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how do transfer data from one server to another server