Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ public static void main(String[] args) throws Exception {
System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StrErrLog");
System.setProperty("org.eclipse.jetty.LEVEL", "INFO");

// Create a basic Jetty server object that will listen on port 8080.
// Create a basic Jetty server object that will listen on port defined by
// the PORT environment variable when present, otherwise on 8080.
// Note: If you set this to port 0, a randomly available port will be
// assigned. You can find the assigned port in the logs or programmatically
// obtain it.
Server server = new Server(8080);
int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
Server server = new Server(port);

// The WebAppContext is the interface to provide configuration for a web
// application. In this example, the context path is being set to "/" so
Expand Down
6 changes: 4 additions & 2 deletions appengine-java11/custom-entrypoint/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
public class Main {

public static void main(String[] args) throws IOException {
// Create an instance of HttpServer bound to port 8080.
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
// Create an instance of HttpServer bound to port defined by the
// PORT environment variable when present, otherwise on 8080.
int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);

// Set root URI path.
server.createContext("/", (var t) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
public class Main {

public static void main(String[] args) throws IOException {
// Create an instance of HttpServer with port 8080.
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
// Create an instance of HttpServer bound to port defined by the
// PORT environment variable when present, otherwise on 8080.
int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);

// Set root URI path.
server.createContext("/", (var t) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
public class Main {

public static void main(String[] args) {
// Starts the webapp on localhost:8080.
Spark.port(8080);
// Starts the webapp on localhost and the port defined by the PORT
// environment variable when present, otherwise on 8080.
int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
Spark.port(port);
Spark.get("/", (req, res) -> "Hello World!");
}
}