當前位置: 首頁>>代碼示例>>Java>>正文


Java HttpContext.setHandler方法代碼示例

本文整理匯總了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;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:18,代碼來源:ProxyTest.java

示例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;
}
 
開發者ID:AdamBien,項目名稱:loadr,代碼行數:17,代碼來源:HttpService.java

示例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);
        }
    });

   
}
 
開發者ID:abhirockzz,項目名稱:accs-psm-cli-docker,代碼行數:30,代碼來源:Bootstrap.java

示例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;
}
 
開發者ID:AdamBien,項目名稱:nano,代碼行數:9,代碼來源:Contexts.java

示例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);
        }
    });
}
 
開發者ID:workshare,項目名稱:ms-nos,代碼行數:11,代碼來源:HealthcheckerTest.java

示例6: setHandler

import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
private void setHandler(HttpContext context) {
    context.setHandler(new WSHttpHandler(adapter, executor));
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:4,代碼來源:HttpEndpoint.java


注:本文中的com.sun.net.httpserver.HttpContext.setHandler方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。