本文整理匯總了Java中com.sun.net.httpserver.HttpContext.setHandler方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpContext.setHandler方法的具體用法?Java HttpContext.setHandler怎麽用?Java HttpContext.setHandler使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.sun.net.httpserver.HttpContext
的用法示例。
在下文中一共展示了HttpContext.setHandler方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createHttpsServer
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
static HttpServer createHttpsServer() throws IOException, NoSuchAlgorithmException {
HttpsServer server = com.sun.net.httpserver.HttpsServer.create();
HttpContext context = server.createContext(PATH);
context.setHandler(new HttpHandler() {
@Override
public void handle(HttpExchange he) throws IOException {
he.getResponseHeaders().add("encoding", "UTF-8");
he.sendResponseHeaders(200, RESPONSE.length());
he.getResponseBody().write(RESPONSE.getBytes(StandardCharsets.UTF_8));
he.close();
}
});
server.setHttpsConfigurator(new Configurator(SSLContext.getDefault()));
server.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
return server;
}
示例2: startServer
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
public static HttpServer startServer(String path, Supplier<String> request, Consumer<String> response) throws IOException {
HttpServer httpServer = HttpServer.create(new InetSocketAddress(4221), 0);
HttpContext context = httpServer.createContext(path);
context.setHandler((he) -> {
final byte[] bytes = request.get().getBytes();
he.sendResponseHeaders(200, bytes.length);
final OutputStream output = he.getResponseBody();
InputStream input = he.getRequestBody();
response.accept(read(input));
output.write(bytes);
output.flush();
he.close();
});
httpServer.start();
return httpServer;
}
示例3: main
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
public static void main(String[] args) throws IOException {
String host = System.getenv().getOrDefault("HOSTNAME", "localhost");
int port = Integer.valueOf(System.getenv().getOrDefault("PORT", "9090"));
HttpServer server = HttpServer.create(new InetSocketAddress(host,port), 0);
HttpContext context = server.createContext("/");
context.setHandler((he) -> {
he.sendResponseHeaders(200, 0);
final OutputStream output = he.getResponseBody();
String address = he.getRemoteAddress().getAddress().getHostAddress();
output.write(("Hello @ "+ new Date().toString() + " from "+ address).getBytes());
output.flush();
he.close();
});
server.start();
System.out.println("server started");
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
server.stop(0);
}
});
}
示例4: create
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
public static HttpContext create(HttpServer server, Path path) {
HttpHandler handler = instantiate(path);
final String extracted = extractContext(path);
HttpContext context = server.createContext(extracted);
context.setHandler(handler);
System.out.println("Context registered: " + context.getPath());
return context;
}
示例5: setupHealthcheck
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
private void setupHealthcheck(final int code) throws IOException {
setupNOHealthcheck();
HttpContext context = httpServer.createContext("/");
context.setHandler(new HttpHandler() {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
httpExchange.sendResponseHeaders(code, -1);
}
});
}
示例6: setHandler
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
private void setHandler(HttpContext context) {
context.setHandler(new WSHttpHandler(adapter, executor));
}