Java, Go web server

 

1. 使用java, go自带web server

 

java语言版本参考http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/package-summary.html 的 A minimal Http server example

 

package net;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

/**
 * jdk6新特性——http server
 *
 * @author yangwm Jan 17, 2010 5:16:41 PM
 */
public class MiniHttpd {
    public static void main(String[] args) {
        try {
            HttpServer hs = HttpServer.create(new InetSocketAddress(8080),0);// 设置HttpServer的端口为8088
            hs.createContext("/yangwm", new MiniHttpHandler());// 用MiniHttpHandler类内处理到/yangwm的请求
            hs.setExecutor(null); // creates a default executor
            hs.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class MiniHttpHandler implements HttpHandler {
    public void handle(HttpExchange t) throws IOException {
        String response = "welcome yangwm's home/n";
        t.sendResponseHeaders(200, response.length());
        OutputStream os = t.getResponseBody();
        os.write(response.getBytes());
        os.close();
    }
}

 

go语言版本参考http://golang.org/pkg/http/ 的 func ListenAndServe

 

package main

import (
   "http"
    "io"
    "log"
)

func MiniHttpHandler(c *http.Conn, req *http.Request) {
    io.WriteString(c, "welcome<font color='#ff0000'> yangwm</font>'s home/n");
}

func main() {
    http.Handle("/yangwm", http.HandlerFunc(HelloServer));
    err := http.ListenAndServe(":8080", nil);
    if err != nil {
        log.Exit("ListenAndServe: ", err.String())
    }
}

 

2. 测试java, go自带web server

 

java web server测试:

C:/Documents and Settings/yangwm>curl http://127.0.0.1:8080/yangwm
welcome yangwm's home

 

go web server测试:

[root@localhost ~]# curl http://127.0.0.1:8080/yangwm
welcome yangwm's home

 

 

3. java, go自带web server分析

 

HttpServer.java源码,

 

package com.sun.net.httpserver;

 

import com.sun.net.httpserver.spi.HttpServerProvider;

 

public abstract class HttpServer {

    public static HttpServer create(InetSocketAddress addr, int backlog)
            throws IOException {
        HttpServerProvider provider = HttpServerProvider.provider();
        return provider.createHttpServer(addr, backlog);
    }

}

 

 

HttpServerProvider.java源码,

package com.sun.net.httpserver.spi;

import com.sun.net.httpserver.*;

public abstract class HttpServerProvider {

    public static HttpServerProvider provider() {
        synchronized (lock) {
            if (provider != null)
                return provider;
            return (HttpServerProvider) AccessController
                    .doPrivileged(new PrivilegedAction<Object>() {
                        public Object run() {
                            if (loadProviderFromProperty())
                                return provider;
                            if (loadProviderAsService())
                                return provider;
                            provider = new sun.net.httpserver.DefaultHttpServerProvider();
                            return provider;
                        }
                    });
        }
    }

}

 

DefaultHttpServerProvider .java源码,

package sun.net.httpserver;

import java.net.*;
import java.io.*;
import com.sun.net.httpserver.*;
import com.sun.net.httpserver.spi.*;

public class DefaultHttpServerProvider extends HttpServerProvider {
    public HttpServer createHttpServer(InetSocketAddress addr,
            int backlog) throws IOException {
        return new HttpServerImpl(addr, backlog);
    }

    public HttpsServer createHttpsServer(InetSocketAddress addr,
            int backlog) throws IOException {
        return new HttpsServerImpl(addr, backlog);
    }
}

HttpServerImpl.java源码, 可参看http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Modules-sun/net/sun/net/httpserver/HttpServerImpl.java.htm

 

 

 

server.go源码, 可参看http://golang.org/src/pkg/http/server.go

 

 

本文来自:CSDN博客

感谢作者:yang_net

查看原文:Java, Go web server

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。