4

I saw another question like this on StackOverflow, but the answers he got were pretty, rediculous. "Connect to the localhost." -- Like, okay.

Anyway, the problem is I finally got my embedded Jetty server to compile and run, the problem was since I'm using it in an API i needed the sources, instead of just the dependency.

I'm running the most basic hello-world code right now, and the server is starting, but none of my browsers can form a connection.

Here's the code:

try {
    httpServer = new Server(8080);
    httpServer.setHandler(new JettyPage());
    httpServer.start();
} catch(Exception e) {
    e.printStackTrace();
}

Naturally, here's JettyPage.java:

public class JettyPage extends AbstractHandler {

    @Override
    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        baseRequest.setHandled(true);
        response.getWriter().println("<h1>Hello world.</h1>");
    }

}

The output in the console:

2014-11-07 07:01:05.155:INFO::main: Logging initialized @599ms
2014-11-07 07:01:05.190:INFO:oejs.Server:main: jetty-9.3.0.M1
2014-11-07 07:01:05.215:INFO:oejs.ServerConnector:main: Started ServerConnector@6e5e91e4{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2014-11-07 07:01:05.215:INFO:oejs.Server:main: Started @660ms

Firefox:

The connection was reset

Opera:

No data received

Chrome:

No data received

--- Yes, I am connecting to localhost:8080 I've also tried: 0.0.0.0:8080 and 127.0.0.1:8080

Note: If I disable my apache server that's running on port 80, and bind Jetty to port 80, I still can't connect.

Hobbyist
  • 15,888
  • 9
  • 46
  • 98
  • Maybe you're using some kind of VPN, like Cisco AnyConnect? https://stackoverflow.com/questions/32103885/not-able-to-access-local-server-running-after-vpn-connection – user11153 Mar 19 '21 at 15:23

2 Answers2

3

I had exactly the same problem...very simple code, couldn't get browser to connect to Jetty even though it compiled and ran from both command-line and IDE. Finally traced it down to mismatched servlet and Jetty jars (at least I think that caused the problem). The ones that worked for me were javax-servlet-api-3.1.0.jar and jetty-all-9.2.8.v20150217.jar. Once I started using those I could connect if the code was compiled at command-line or from NetBeans IDE.

For raw noobs like me, most of the online examples for command-line compile use Linux/Unix path separators (":"). It took me several minutes to figure out on a Windows machine I needed a different one (";"):

javac -cp .;javax.servlet-api-3.1.0.jar;jetty-all-9.2.8.v20150217.jar HelloWorld.java

Hope that helps.

TimF
  • 31
  • 2
1

If you followed the HelloWorld example exactly, you would have a response.

There is still something wrong with your runtime environment.

Is this windows you are running on? If so, try setting the port to something other than 8080 (try 38080), as you might have something already on that port, and Windows will not throw an error or warning in this situation. (on other OS's this would result in a bind exception)

Do you have any logging output that indicates an error or warning?

There should be plenty of logging output to the console, even when there is an error in starting up the server, or serving some content.

If you want to enable VERY verbose logging, you can add -Dorg.eclipse.jetty.LEVEL=DEBUG to your java execution.

Windows Version

> java -Dorg.eclipse.jetty.LEVEL=DEBUG -cp .;servlet-api.jar;jetty-all.jar HelloWorld

Linux / OSX / Unix Version

$ java -Dorg.eclipse.jetty.LEVEL=DEBUG -cp .:servlet-api.jar:jetty-all.jar HelloWorld
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136