0

I have deployed two tomcats on my host on different ports. one is listening on port A and one on port B. There are webapps hosted on both the tomcats.

I need to make sure that the client which calls APIS on those webapps on both tomcats doesn't need to know both the ports. The client (angularJS) should be able to call just one tomcat and internally that tomcat should be able to redirect the request for proper service to another tomcat and pass the response back to the client.

That way I will be able to handle same origin policy also as the angularjs webapp is also deployed on the tomcat where I want to send the requests and cookies can also be used with this as there will be only one server where the cookies will be coming from.

Is there any way to achieve that? Is there any configuration that I can do on the first tomcat itself to redirect some request based on the URI to the second tomcat? or any other tool that I can deploy to support that? I heard somewhere of port redirection using iptables. Can that be used here?

user3565529
  • 1,317
  • 2
  • 14
  • 24
  • Questions on professional server- or networking-related infrastructure administration are off-topic for Stack Overflow unless they directly involve programming or programming tools. You may be able to get help on Server Fault. – JFPicard Jan 21 '16 at 20:00
  • Not sure I understand your question but you might want to look at a "reverse proxy" (like Apache). – Gaël J Jan 21 '16 at 20:06
  • Put an Apache HTTPD in front and configure it as a reverse proxy. Off topic. Try serverfault.com. – user207421 Jan 21 '16 at 21:05
  • I dont want to add another server, just wanted to check if there is something available like reverse proxy for tomcat – user3565529 Jan 21 '16 at 22:58

1 Answers1

0

The client does a request to port A, and inside the java code does a http request to http://localhost:B

To do a http request just search "java http request" and chose one of hundreds of examples. One possible way from here: How do I do a HTTP GET in Java?

   public static String getHTML(String urlToRead) throws Exception {
      StringBuilder result = new StringBuilder();
      URL url = new URL(urlToRead);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("GET");
      BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String line;
      while ((line = rd.readLine()) != null) {
         result.append(line);
      }
      rd.close();
      return result.toString();
   }
Community
  • 1
  • 1
Gavriel
  • 18,880
  • 12
  • 68
  • 105