Java, Go web server

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

 

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

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.