2

I am using jetty server (from org.mortbay.jetty.Server)

Server server = new Server(8080);

How do i get my server address so i can send messages to it for testing? Thanks.

Michael A
  • 5,770
  • 16
  • 75
  • 127
  • Is your server multihome? Do you use vhosts? The answer depends on these factors – fge Dec 30 '11 at 11:49

2 Answers2

7

Use java.net.InetAddress:

InetAddress addr = InetAddress.getLocalHost();

// Get IP Address
byte[] ipAddr = addr.getAddress();

// Get hostname
String hostname = addr.getHostName();
Lauri Piispanen
  • 2,067
  • 12
  • 20
1

I'm a bit confused about your question. When you start and embedded Jetty server, the address will always be "localhost" (your machine's loopback interface). The port will be whatever you chose it to be when you create the Server instance. So for your example, your embedded Jetty server will be at

http://localhost:8080

At any rate, you can also get Jetty's current (local) port number like this:

int jettyPort = server.getConnectors()[0].getLocalPort();

This could be useful for integration testing, where you have a test tool (like a JUnit rule or something) that starts embedded Jetty on a random port, and then later on, when using this Jetty instance in a test, you need to know on what port it was started.

Shivan Dragon
  • 15,004
  • 9
  • 62
  • 103
  • 1
    If you're using Jetty 9, this doesn't work. You'll need to cast the connector to a ServerConnector. – Steve N Jan 24 '14 at 12:25