简体   繁体   中英

Getting my server address - Jetty web server

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.

Use java.net.InetAddress :

InetAddress addr = InetAddress.getLocalHost();

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

// Get hostname
String hostname = addr.getHostName();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM