Created
November 8, 2013 06:40
-
-
Save taywils/7367126 to your computer and use it in GitHub Desktop.
spark_demo_step_4 HelloSpark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static spark.Spark.*; | |
import spark.*; | |
import java.util.ArrayList; | |
public class HelloSpark { | |
// Just store POST data within a ArrayList for now | |
public static ArrayList<String> things = new ArrayList<String>(); | |
public static void main(String[] args) { | |
get(new Route("/list") { | |
@Override | |
public Object handle(Request request, Response response) { | |
StringBuilder html = new StringBuilder(); | |
if (HelloSpark.things.isEmpty()) { | |
html.append("<b>Try adding some things to your list</b>"); | |
} else { | |
html.append("<ul>"); | |
for (String thing : HelloSpark.things) { | |
html.append("<li>").append(thing).append("</p>"); | |
} | |
html.append("</ul>"); | |
} | |
return html.toString(); | |
} | |
}); | |
post(new Route("/add/:item") { | |
@Override | |
public Object handle(Request request, Response response) { | |
HelloSpark.things.add(request.params(":item")); | |
response.status(200); | |
return response; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment