`
louisling
  • 浏览: 144450 次
  • 性别: Icon_minigender_1
  • 来自: ZhuHai
社区版块
存档分类
最新评论

Sun HttpServer

    博客分类:
  • Web
阅读更多
/**<pre>
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.

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.

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 HttpsConfigurator object, 
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);

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.

    }
});
</pre> */

public class HttpServerDemo {
    public static void main(String[] args) throws IOException {
        startHttpServer();
    }

    static void startHttpServer() throws IOException {
        int port = 8888;
        InetSocketAddress addr = new InetSocketAddress(port);
        HttpServer server = HttpServer.create(addr, 0);

        server.createContext("/myApp", new MyHandler());

        //server.setExecutor(null); // creates a default executor
        server.setExecutor(Executors.newCachedThreadPool());
        server.start();
        System.out.println("Server is listening on port " + port + "...");
    }
}

class MyHandler implements HttpHandler {
    public void handle(HttpExchange exchange) throws IOException {
        String requestMethod = exchange.getRequestMethod();
        System.out.println("RequestMethod: " + requestMethod);

        if (requestMethod.equalsIgnoreCase("GET")) {
            Headers responseHeaders = exchange.getResponseHeaders();
            responseHeaders.set("Content-Type", "text/html"); //text/plain
            exchange.sendResponseHeaders(200, 0);

            OutputStream responseBody = exchange.getResponseBody();
            Headers requestHeaders = exchange.getRequestHeaders();
            Set<String> keySet = requestHeaders.keySet();
            Iterator<String> iter = keySet.iterator();
            while (iter.hasNext()) {
                String key = iter.next();
                List<String> values = requestHeaders.get(key);
                String s = "<b>" + key + "</b> = " + values.toString() + "<br>";
                responseBody.write(s.getBytes());
            }
            responseBody.close();
        }
    }
}
分享到:
评论

相关推荐

    com.sun.net.httpserver.http.jar

    com.sun.net.httpserver

    JAVA HTTP 发送 接收 com.sun.net.httpserver包 demo

    在Java中,我们可以使用多种库来实现HTTP通信,其中包括`com.sun.net.httpserver`包,这是一个内置的轻量级HTTP服务器,适用于测试、原型设计以及简单的应用。本示例将详细介绍如何使用`com.sun.net.httpserver`包...

    java HttpServer构建http服务器

    Java中的HttpServer是Java SE平台的一部分,位于`com.sun.net.httpserver`包中,它提供了一个简单的HTTP服务器实现,主要用于开发、测试和演示用途。这个轻量级的服务器可以帮助我们理解HTTP协议的工作原理,并且...

    httpserver.rar

    Java HttpServer是Java SDK的一部分,位于com.sun.net.httpserver包下。它是一个轻量级的HTTP服务器,适合于开发和测试用途。通过使用HttpServer,开发者可以直接在Java代码中创建服务器,处理HTTP请求,并返回定制...

    基于 httpserver 类实现的简易 http 服务器

    利用 Java 的 com.sun.net 包下的 http 类,实现的简易 http 服务器。全部代码已上传至 GitHub,链接在文末。 主要功能为: 1、只处理 GET 方式和 POST 方式的请求,其他的返回 501 Not Implemented 2、GET:1)...

    com.sun.net.httpserver安卓HTTP服务器jar包最新版2.2.1

    使用com.sun.net.httpserver安卓HTTP服务器jar包最新版2.2.1,可以在android系统中创建一个web服务器

    CDSpace 测试工具

    CDSpace是基于Apache Http Client和Sun Http Server两个库,以JavaFX为GUI框架开发的一款HTTP接口测试工具,主要功能分为两部分:1、模拟HttpClient发送自定义请求给服务端,接受响应并将响应的消息体和头域显示在...

    HTTP接口测试工具-CDSpace

    CDSpace是基于Apache Http Client和Sun Http Server两个库,以JavaFX为GUI框架开发的一款HTTP接口测试工具,主要功能分为两部分:1、模拟HttpClient发送自定义请求给服务端,接受响应并将响应的消息体和头域显示在...

    简易HttpServer

    创建一个简单的Java HttpServer,我们需要导入`com.sun.net.httpserver.HttpServer`类,并通过以下步骤实现: 1. 初始化HttpServer实例:`HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);...

    java使用webserver发布服务端和客户端

    import com.sun.net.httpserver.HttpServer; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpExchange; public class Server { public static void main(String[] args) throws ...

    java http post client server

    在Java中,我们可以使用内置的`HttpServer`类(来自`com.sun.net.httpserver`包)来创建一个简单的HTTP服务器。服务器的主要职责是接收客户端的POST请求,处理请求体中的数据,并返回响应。 服务器端实现的关键步骤...

    817-5602-10.pdf-Oracle Solaris 9 - Sun ONE Application Server Pl

    Sun ONE Application Server 7, Update 3 产品支持多种 Web 服务器,包括 Apache HTTP Server、Microsoft IIS 和 Sun ONE Web Server 等。这些 Web 服务器能够与 Sun ONE Application Server 7, Update 3 产品进行...

    Sun Java System Application Server Enterprise Edition 8.2 管理指南.pdf

    ### Sun Java System Application Server Enterprise Edition 8.2 管理指南 #### 一、概述 Sun Java System Application Server Enterprise Edition 8.2 (以下简称 Sun Java Application Server EE 8.2) 是一款...

    Oracle Solaris 9 - Sun ONE Application Server 7 Platform Summary

    * Apache HTTP Server 2.0.40 目录服务器 Sun ONE 应用服务器 7 支持以下目录服务器: * Sun Java System Directory Server 5.2 * Microsoft Active Directory 浏览器 Sun ONE 应用服务器 7 支持以下浏览器: ...

    ProtoJ:我对Java的com.sun.net.HttpServer和相关类的包装

    原型Java的com.sun.net.HttpServer和相关类的包装ProtoJ是一个原型服务器,旨在使编写Java简单服务器变得快速而轻松。 目前尚不适合生产,并且不适合生产,但如果将来会适用,那就太好了。 ProtoJ应该针对Java 8和...

    http-game:用于发布使用 com.sun.net.httpserver.HttpServer 检索一些游戏分数的 HTTP 示例

    本篇文章将详细讲解如何利用Java内置的`com.sun.net.httpserver.HttpServer`类来创建一个简单的HTTP服务器,用于发布和检索游戏分数。这个功能对于小型应用或教学场景十分实用,无需依赖大型的Web框架。 `...

    Oracle Solaris 9 - Introduction to Sun ONE Application Server 7-

    Oracle Solaris 9 - Introduction to Sun ONE Application Server 7 本文档介绍了 Sun ONE Application Server 7 的基本概念和特点,该服务器提供了一个高性能的 J2EE 平台,适合广泛部署应用服务和 Web 服务。它...

    Sun Java System Web Server 7.0 管理員指南.pdf

    ### Sun Java System Web Server 7.0 知识点概览 #### 产品与文档版权信息 Sun Java System Web Server 7.0是一款由Sun Microsystems, Inc.开发的高性能网络服务器,适用于各种企业级应用环境。该产品及其文档受到...

    HttpServer:Java支持的Httpserver

    Java中的HttpServer通常指的是`com.sun.net.httpserver.HttpServer` 类,它是Java的标准库(Java SE)的一部分,位于`jdk.jdk.net.http` 包下。这个类提供了一个简单的API,允许程序员创建自己的HTTP服务器来处理...

Global site tag (gtag.js) - Google Analytics