Created
December 31, 2015 17:19
-
-
Save anonymous/4240bdf7b5e80e07da45 to your computer and use it in GitHub Desktop.
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
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