Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

31 lines (22 sloc) 1.3 KB

Embedded web server examples

The Java Buildpack can run applications which provide their own web server or servlet container, provided as JAR files.

Example

This example uses Jetty as an embedded web server, but should be applicable for other technologies.

public static void main(String[] args) {
  int port = Integer.parseInt(System.getenv("PORT"));
  Server server = new Server(port);
  server.setHandler(new AbstractHandler() {
      @Override
      public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
          response.setContentType("text/html;charset=utf-8");
          response.setStatus(HttpServletResponse.SC_OK);
          baseRequest.setHandled(true);
          response.getWriter().println("<h1>Hello, world</h1>");
      }
  });
  server.start();
  server.join();
}

The important takeaway is to note that the port comes from the environment variable port. Other variables are detailed in the Cloud Foundry developer guide. When the application is built as an executable JAR file, it will be treated as a Java Main application.

You can’t perform that action at this time.