Skip to content

Commit 173e798

Browse files
author
Christopher Frost
committed
Merge pull request cloudfoundry#167 from robb1e/master
Docs - Embedded Jetty example.
2 parents c0a6a1f + 72f4580 commit 173e798

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ cf push <APP-NAME> -p <ARTIFACT> -b https://github.com/cloudfoundry/java-buildpa
1616
## Examples
1717
The following are _very_ simple examples for deploying the artifact types that we support.
1818

19+
* [Embedded web server](docs/example-embedded-web-server.md)
1920
* [Grails](docs/example-grails.md)
2021
* [Groovy](docs/example-groovy.md)
2122
* [Java Main](docs/example-java_main.md)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Embedded web server examples
2+
3+
The Java Buildpack can run applications which provide their own web server or servlet container, provided as JAR files.
4+
5+
## Example
6+
7+
This example uses Jetty as an embedded web server, but should be applicable for other technologies.
8+
9+
```java
10+
11+
public static void main(String[] args) {
12+
int port = Integer.parseInt(System.getenv("PORT"));
13+
Server server = new Server(port);
14+
server.setHandler(new AbstractHandler() {
15+
@Override
16+
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
17+
response.setContentType("text/html;charset=utf-8");
18+
response.setStatus(HttpServletResponse.SC_OK);
19+
baseRequest.setHandled(true);
20+
response.getWriter().println("<h1>Hello, world</h1>");
21+
}
22+
});
23+
server.start();
24+
server.join();
25+
}
26+
```
27+
28+
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](http://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html). When the application is built as an executable JAR file, it will be treated as a [Java Main](https://github.com/cloudfoundry/java-buildpack/blob/master/docs/example-java_main.md) application.
29+
30+

0 commit comments

Comments
 (0)