1

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();
        }
    }
}
Mat
  • 202,337
  • 40
  • 393
  • 406
Trup
  • 1,635
  • 13
  • 27
  • 40

3 Answers3

9

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

jesse mcconnell
  • 7,102
  • 1
  • 22
  • 33
  • Can you take a look at this question http://stackoverflow.com/questions/32869334/eof-invalid-function-argument-errno4022-with-jetty-9 I stuck with this for long time. – vels4j Nov 26 '15 at 06:38
1

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.

Orn Kristjansson
  • 3,435
  • 4
  • 26
  • 40
  • Cool, and how about the whole url, can it be url = new URL("http://localhost:8180/reversi"); Does it seem right? – Trup Aug 17 '12 at 23:07
0

I'm not sure what you're asking here but if you had some servlet i.e 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

ant
  • 22,634
  • 36
  • 132
  • 182
  • Well I can't do that because right now I am just setting things up. The server is running, but in order to get the client up, I have to figure out how to connect it to the server, and for this I need the port and the url to connect. – Trup Aug 17 '12 at 23:08
  • how did you setup server in the first place? In order to connect server you ought to know which server/port it's running on. Please take a look at my previous answer how to start jetty server with example http://stackoverflow.com/questions/10567699/getting-http-500-when-running-servlet-in-jetty/10609507#10609507 – ant Aug 17 '12 at 23:19
  • Please see the relevant code above about how I start up the servlet. – Trup Aug 17 '12 at 23:26