這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
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