简体   繁体   中英

Jetty servlet url and port

What is the method call to figure out what is the url and port a jetty servlet is running on? So that I can just print it on the screen, and use it in the client to connect:

url = new URL("trying to figure this out");

I am running both the client and the server locally, in eclipse, this is a school project.

Relevant code:

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class ServerMain {

    public static void main(String[] args){
        Server server = new Server();
        ServletContextHandler context = 
                new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("Servlet");

        context.setResourceBase("etc/docroot");

        server.setHandler(context);
        context.addServlet(new ServletHolder(new MyServer()), "/game");
        DefaultServlet staticFileServlet = new DefaultServlet();
        context.addServlet(new ServletHolder(staticFileServlet), "/*");
        System.out.println("context: " +  context.getContextPath());
        //System.out.println("One valid Port = "
                  + context.getServer().getConnectors()[0].getPort());
        try {
            server.start();
            server.join();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Since you are doing this embedded you have a couple of options.

Server server = new Server(8080);

This will start the server on port 8080, which is fine if that is what you want but in unit tests you generally want this to be a random port to avoid issues with things like CI's or parallel tests tripping on each other.

Server server = new Server(0);

This will start the connector on a random port, but how to get that port?

server.getConnectors()[0].getLocalPort();

The port is really coming from the connector, and typically you only are setting up one connector here, so this gets you that port.

Now localhost works for testing well, but if you wanted the name of the host that the connector is on you can use this:

server.getConnectors()[0].getHost();

Chain that stuff up and you get how we do most of our unit testing in jetty itself, spinning up the server itself, wiring up whatever handler or webapps we want and then assert behaviors, requests, responses, etc.

We have a number of embedded examples here that you can look at for different ways to wire up jetty inside code, and the jetty.xml format is just a thin layer of xml over java so you can map the startup of jetty easily through the code by reading the xml file with a java hat on. There is an embedded example in here for bootstrapping jetty based on that xml format as well if you want to keep the config there.

http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/tree/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded

For a good page on embedding jetty, look here: http://www.eclipse.org/jetty/documentation/current/embedding-jetty.html

Most likely the jetty server is running on 8080 as default. If not you can go and check the server.xml file which should tell you what the port configuration is.

I'm not sure what you're asking here but if you had some servlet ie Hello :

public final class Hello extends HttpServlet {

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
      throws IOException, ServletException {

        response.setContentType("text/html");
        PrintWriter writer = response.getWriter();        
        writer.println("URL is " + request.getRequestURL() + " port is " request.getServerPort());
    }
} 

Additionally if this is not info what you're looking for please see this :

http://docs.oracle.com/javaee/1.4/api/javax/servlet/http/HttpServletRequest.html

http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.6/api/javax/servlet/http/HttpServletRequest.html

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