Gaurav Shukla
Gaurav Shukla

Reputation: 61

How to send data from one webApplication to another

I have two web applications in two different server.I want send some data in header or request to other web application.How can I do that, please help me.

Upvotes: 3

Views: 3960

Answers (3)

Thorn
Thorn

Reputation: 4057

One web application is functioning as the client of the other. You can use the org.apache.http library to create your HTTP client code in Java. How you will do this depends on a couple of things:

  1. Are you using http or https?
  2. Does the application you are sending data to have a REST API?
  3. Do you have a SOAP based web service?

If you have a SOAP based web service, then creating a Java client for it is very easy. If not, you could do something like this and test the code in a regular Java client before trying to run it in the web application.

 import org.apache.http.client.utils.*;
 import org.apache.http.*; 
 import org.apache.http.impl.client.*;

  HttpClient httpclient = new DefaultHttpClient();
  try {
     URIBuilder builder = new URIBuilder();
     builder.setHost("yoursite.com").setPath(/appath/rsc/);
     builder.addParameter("user", username);
     builder.addParameter("param1", "SomeData-sentAsParameter");
     URI uri = builder.build();
     HttpGet httpget = new HttpGet(uri);
     HttpResponse response = httpclient.execute(httpget);
     System.out.println(response.getStatusLine().toString());
     if (response.getStatusLine().getStatusCode() == 200) {
        String responseText = EntityUtils.toString(response.getEntity());
        httpclient.getConnectionManager().shutdown();
     } else {
        log(Level.SEVERE, "Server returned HTTP code "
                + response.getStatusLine().getStatusCode());
     }
  } catch (java.net.URISyntaxException bad) {
     System.out.println("URI construction error: " + bad.toString());
  }

Upvotes: 0

user1697575
user1697575

Reputation: 2848

You can pass data by many means:

  1. by making http request from your app:

    URLConnection conn = new URL("your other web app servlet url").openConnection();
    // pass data using conn. Then on other side you can have a servlet that will receive these calls.
    
  2. By using JMS for asynchronous communication.

  3. By using webservice (SOAP or REST)

  4. By using RMI

  5. By sharing database between the apps. So one writes to a table and the other reads from that table

  6. By sharing file system file(s)...one writes to a file the other reads from a file.

  7. You can use socket connection.

Upvotes: 5

Vitaly
Vitaly

Reputation: 2800

HttpClient can help

http://hc.apache.org/index.html

Apache HttpComponents

The Apache HttpComponents™ project is responsible for creating and maintaining a toolset of low level Java components focused on HTTP and associated protocols.

Upvotes: 0

Related Questions