http://docs.oracle.com/javase/8/docs/jre/api/net/httpserver/spec/overview-summary.html

1.Package com.sun.net.httpserver Description

Provides a simple high-level Http server API, which can be used to build embedded HTTP servers.
Provides a simple high-level Http server API, which can be used to build embedded HTTP servers. Both "http" and "https" are supported. The API provides a partial implementation of RFC 2616 (HTTP 1.1) and RFC 2818 (HTTP over TLS). Any HTTP functionality not provided by this API can be implemented by application code using the API.

Programmers must implement the HttpHandler interface. This interface provides a callback which is invoked to handle incoming requests from clients. A HTTP request and its response is known as an exchange. HTTP exchanges are represented by the HttpExchange class. The HttpServer class is used to listen for incoming TCP connections and it dispatches requests on these connections to handlers which have been registered with the server.

A minimal Http server example is shown below:

   class MyHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
InputStream is = t.getRequestBody();
read(is); // .. read the request body
String response = "This is the response";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
... HttpServer server = HttpServer.create(new InetSocketAddress(8000));
server.createContext("/applications/myapp", new MyHandler());
server.setExecutor(null); // creates a default executor
server.start();

The example above creates a simple HttpServer which uses the calling application thread to invoke the handle() method for incoming http requests directed to port 8000, and to the path /applications/myapp/.

The HttpExchange class encapsulates everything an application needs to process incoming requests and to generate appropriate responses.

Registering a handler with a HttpServer creates a HttpContext object and Filter objects can be added to the returned context. Filters are used to perform automatic pre- and post-processing of exchanges before they are passed to the exchange handler.

For sensitive information, a HttpsServer can be used to process "https" requests secured by the SSL or TLS protocols. A HttpsServer must be provided with a HttpsConfiguratorobject, which contains an initialized SSLContext. HttpsConfigurator can be used to configure the cipher suites and other SSL operating parameters. A simple example SSLContext could be created as follows:

   char[] passphrase = "passphrase".toCharArray();
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream("testkeys"), passphrase); KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, passphrase); TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks); SSLContext ssl = SSLContext.getInstance("TLS");
ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

In the example above, a keystore file called "testkeys", created with the keytool utility is used as a certificate store for client and server certificates. The following code shows how the SSLContext is then used in a HttpsConfigurator and how the SSLContext and HttpsConfigurator are linked to the HttpsServer.

    server.setHttpsConfigurator (new HttpsConfigurator(sslContext) {
public void configure (HttpsParameters params) { // get the remote address if needed
InetSocketAddress remote = params.getClientAddress(); SSLContext c = getSSLContext(); // get the default parameters
SSLParameters sslparams = c.getDefaultSSLParameters();
if (remote.equals (...) ) {
// modify the default set for client x
} params.setSSLParameters(sslparams);
// statement above could throw IAE if any params invalid.
// eg. if app has a UI and parameters supplied by a user. }
});
Since:
1.6

2. Package com.sun.net.httpserver.spi Description

Provides a pluggable service provider interface, which allows the HTTP server implementation to be replaced with other implementations.
 
 
 
 

HttpServer的使用的更多相关文章

  1. NodeJS 最快速搭建一个HttpServer

    最快速搭建一个HttpServer 在目录里放一个index.html cd D:\Web\InternalWeb start http-server -i -p 8081

  2. 基于Netty4的HttpServer和HttpClient的简单实现

    Netty的主页:http://netty.io/index.html 使用的Netty的版本:netty-4.0.23.Final.tar.bz2 ‐ 15-Aug-2014 (Stable, Re ...

  3. libevent源码分析:http-server例子

    http-server例子是libevent提供的一个简单web服务器,实现了对静态网页的处理功能. /* * gcc -g -o http-server http-server.c -levent ...

  4. httpserver

    改了下 # -*- coding:utf-8 -*- from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler HOST = &quo ...

  5. 一个简单的零配置命令行HTTP服务器 - http-server (nodeJs)

    http-server 是一个简单的零配置命令行HTTP服务器, 基于 nodeJs. 如果你不想重复的写 nodeJs 的 web-server.js, 则可以使用这个. 安装 (全局安装加 -g) ...

  6. nodejs搭建http-server

      很多时候我们都需要搭建一个简单的服务器,部署在IIS,阿帕奇,或者用nodejs,网上很多关于nodejs搭建server的文章,但都是要创建server.js,很麻烦, 在这里我分享一个创建ht ...

  7. 一、HTTPServer,RequestHandler,ServerHandler,Handler

    1.      HTTPServer,RequestHandler,ServerHandler,Handler 1.1       基本概念 HTTPServer主要是对传输控制层HTTP,TCP,S ...

  8. nodejs:本地文件夹http服务器http-server

    一.已经安装nodejs的电脑,有一个方便通过http访问本地文件夹.文件夹服务器 static files over HTTP,并不是我们平常说的node那个web服务器哦 二.好处 可以方便实现跨 ...

  9. PHP使用libevent实现高性能httpServer

    今天发现php也有一个libevent的扩展,安装后尝试下了一个webserver(httpserver), 发现性能还不错,逻辑很简单,每秒响应速度1800~4000次/s 代码如下 <?ph ...

  10. netty httpserver

    netty也可以作为一个小巧的http服务器使用. package com.ming.netty.http.httpserver; import java.net.InetSocketAddress; ...

随机推荐

  1. 一些不错的英文歌曲MV,留个存档!

    Lambada [[http://www.yinyuetai.com/video/265213]]Trouble Is A Friend [[http://www.yinyuetai.com/vide ...

  2. PhoneGap,Cordova[3.5.0-0.2.6]:生成Android项目时出现错误(An error occurred while listing Android targets)

    我在升级到Cordova最新版本(3.5.0-0.2.6)后,在生成Android项目(cordova platform add android)时出现错误: Error: An error occu ...

  3. vmware ubuntu14.04虚拟机不能正常拷贝文件到windows且不能自适应虚拟机屏幕窗口自动变化的解决办法

    纠结于这个问题了半天.一直重复安装不同版本的vmare-tools, 一直没有任何效果.进入到/usr/bin/ 目录使用ll vm* 查看,发现和别的不同的是没有vmware-toolbox-cmd ...

  4. EasyMock(官方资料整理)

    1.要求 EasyMock要求java1.5.0及以上版本. Objenesis (2.0+)必须在classpath中来执行class mocking. 2.使用Maven 在Maven中心库中可以 ...

  5. windows下安装和配置Weka

    Weka是一款免费的,非商业化的,基于java环境下的开源的机器学习以及数据挖掘软件.Weka里含有各种数据挖掘工具:数据预处理,分类与回归,聚类,关联规则和可视化工具. 一.安装weka 我们首先需 ...

  6. Makefile 知识点

    $@ $@ is the name of the target. $? The $? macro stores the list of dependents more recent than the ...

  7. UVALive 7324 ASCII Addition (模拟)

    ASCII Addition 题目链接: http://acm.hust.edu.cn/vjudge/contest/127407#problem/A Description Nowadays, th ...

  8. rop框架中@ServiceMethod注解属性

    @ServiceMethod 属性 method :代码服务方法名version :表 示 版 本 号 group:服务分组名.服务的分组没有特殊的意义,您可以为服务定义一个分组,以便在事件监听器.服 ...

  9. 转载robots.txt的学习

    转载原地址: http://www.monring.com/seo/aspdotseo-robot.html 在国内,robots.txt文件,对于用户来说他是个可有可无的东西,也不会有人去看.但对于 ...

  10. IIS6的SSL配置,如何配置SSL到登陆页,如何将SSL证书设置成受信任的证书

    一. 申请证书1. 到受信任的机构申请 略 2. 到自建的证书服务器申请 a. 安装证书服务 通过控制面板中的“添加/删除程序”,选择“添加/删除Windows组件”.在Windows组件向导中找到“ ...