Skip to content

Instantly share code, notes, and snippets.

Created December 31, 2015 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4240bdf7b5e80e07da45 to your computer and use it in GitHub Desktop.
Save anonymous/4240bdf7b5e80e07da45 to your computer and use it in GitHub Desktop.
package lol;
import java.io.IOException;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.*;
public class DouWs {
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/plus/", new MyDouHandler());
server.setExecutor(null);
server.start();
}
static class MyDouHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
String uriPath = t.getRequestURI().toString().substring("/plus/".length());
int sum = 0;
for (String s: uriPath.split("/")){
try {
sum += Integer.parseInt(s);
} catch (NumberFormatException e){
String response = String.format("cannot parse number %s in the path %s: %s", s, uriPath, e.getMessage() );
t.sendResponseHeaders(400,response.length());
t.getResponseBody().write(response.getBytes());
t.getResponseBody().close();
return ;
}
}
String response = String.format("%d", sum );
t.sendResponseHeaders(299,response.length());
t.getResponseBody().write(response.getBytes());
t.getResponseBody().close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment