3

Since Endpoint.publish creates / uses a lightweight http server (as described here), is there a way to get access to said web server and, say, deploy a single html document at what would normally be the document root?

So if I did

Endpoint.publish("http://0.0.0.0:1234/webService");

Could I get the web server object and tell it to reply with index.html when somebody browses to http:/my.ip.add.res:1234/?

Community
  • 1
  • 1
captainroxors
  • 718
  • 7
  • 18

2 Answers2

3

If you want an embedded http server you can try this http://docs.oracle.com/javase/6/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/package-summary.html

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • Ooooo that looks promising. I'll give that a go once I'm back at work (on Monday). I'm actually already using the HttpExchange object at one point in my code. Silly me for not inspecting the rest of the package. – captainroxors Mar 29 '14 at 07:42
  • Actually upon reading closer, I'm certain this will work. Thanks. – captainroxors Mar 29 '14 at 07:45
  • Please give an example in your post in addition to linking to a resource, in case the link ever dies – Robin Kanters Jun 30 '16 at 10:08
3

Thanks to Evgeniy's direction, I just needed to take a closer look at the com.sun.net.httpserver package.

No, you can't get the Endpoint's webserver, but you can first make one and then publish the Endpoint on that server. Something to this effect:

HttpServer server = HttpServer.create(new InetSocketAddress(1234), 0);
HttpContext c = server.createContext("/webService");
Endpoint e = Endpoint.create(new WebService());
e.publish(c);

// Anything else you want to do with your server.

server.setExecutor(null);
server.start();

And presto. You have a web server with a web service published on it.

captainroxors
  • 718
  • 7
  • 18