Java, Go web server

yang_net · · 3136 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

 

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

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

3136 次点击  
加入收藏 微博
下一篇:DNS协议分析
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传