Skip to content

Commit 3bef9b6

Browse files
author
Jon Mountjoy
committed
getting started
0 parents  commit 3bef9b6

7 files changed

Lines changed: 144 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Joe Kutner
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: java $JAVA_OPTS -cp target/classes:target/dependency/* Main

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
heroku-java-sample
2+
==================

pom.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.example</groupId>
7+
<version>1.0-SNAPSHOT</version>
8+
<artifactId>helloworld</artifactId>
9+
<dependencies>
10+
<dependency>
11+
<groupId>org.eclipse.jetty</groupId>
12+
<artifactId>jetty-servlet</artifactId>
13+
<version>7.6.0.v20120127</version>
14+
</dependency>
15+
<dependency>
16+
<groupId>javax.servlet</groupId>
17+
<artifactId>servlet-api</artifactId>
18+
<version>2.5</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>postgresql</groupId>
22+
<artifactId>postgresql</artifactId>
23+
<version>9.0-801.jdbc4</version>
24+
</dependency>
25+
</dependencies>
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<artifactId>maven-compiler-plugin</artifactId>
30+
<version>2.3.2</version>
31+
<configuration>
32+
<source>1.7</source>
33+
<target>1.7</target>
34+
</configuration>
35+
</plugin>
36+
<plugin>
37+
<groupId>org.apache.maven.plugins</groupId>
38+
<artifactId>maven-dependency-plugin</artifactId>
39+
<version>2.4</version>
40+
<executions>
41+
<execution>
42+
<id>copy-dependencies</id>
43+
<phase>package</phase>
44+
<goals><goal>copy-dependencies</goal></goals>
45+
</execution>
46+
</executions>
47+
</plugin>
48+
</plugins>
49+
</build>
50+
</project>

src/main/java/Main.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import java.io.IOException;
2+
import javax.servlet.ServletException;
3+
import javax.servlet.http.*;
4+
import org.eclipse.jetty.server.Server;
5+
import org.eclipse.jetty.servlet.*;
6+
import java.net.URI;
7+
import java.net.URISyntaxException;
8+
import java.sql.*;
9+
10+
public class Main extends HttpServlet {
11+
@Override
12+
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
13+
throws ServletException, IOException {
14+
15+
if (req.getRequestURI().endsWith("/db")) {
16+
showDatabase(req,resp);
17+
} else {
18+
showHome(req,resp);
19+
}
20+
}
21+
22+
private void showHome(HttpServletRequest req, HttpServletResponse resp)
23+
throws ServletException, IOException {
24+
resp.getWriter().print("Hello from Java!");
25+
}
26+
27+
private void showDatabase(HttpServletRequest req, HttpServletResponse resp)
28+
throws ServletException, IOException {
29+
try {
30+
Connection connection = getConnection();
31+
32+
Statement stmt = connection.createStatement();
33+
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)");
34+
stmt.executeUpdate("INSERT INTO ticks VALUES (now())");
35+
ResultSet rs = stmt.executeQuery("SELECT tick FROM ticks");
36+
37+
String out = "Hello!\n";
38+
while (rs.next()) {
39+
out += "Read from DB: " + rs.getTimestamp("tick") + "\n";
40+
}
41+
42+
resp.getWriter().print(out);
43+
} catch (Exception e) {
44+
resp.getWriter().print("There was an error: " + e.getMessage());
45+
}
46+
}
47+
48+
private Connection getConnection() throws URISyntaxException, SQLException {
49+
URI dbUri = new URI(System.getenv("DATABASE_URL"));
50+
51+
String username = dbUri.getUserInfo().split(":")[0];
52+
String password = dbUri.getUserInfo().split(":")[1];
53+
String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + dbUri.getPath();
54+
55+
return DriverManager.getConnection(dbUrl, username, password);
56+
}
57+
58+
public static void main(String[] args) throws Exception{
59+
Server server = new Server(Integer.valueOf(System.getenv("PORT")));
60+
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
61+
context.setContextPath("/");
62+
server.setHandler(context);
63+
context.addServlet(new ServletHolder(new Main()),"/*");
64+
server.start();
65+
server.join();
66+
}
67+
}

system.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
java.runtime.version=1.7
2+

0 commit comments

Comments
 (0)