最代码官方
2014-11-28 11:43:05
证
分享java最简单web server代码,只支持一个http get请求
package com.zuidaima.simpleserver; 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; /* * a simple static http server * @author www.zuidaima.com */ public class SimpleHttpServer { public static void main(String[] args) throws Exception { HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0); server.createContext("/zuidaima", new MyHandler()); server.setExecutor(null); // creates a default executor server.start(); } static class MyHandler implements HttpHandler { public void handle(HttpExchange t) throws IOException { String response = "www.zuidaima.com"; t.sendResponseHeaders(200, response.length()); OutputStream os = t.getResponseBody(); os.write(response.getBytes()); os.close(); } } }
访问:http://localhost:8000/zuidaima
猜你喜欢
- 小型java购物车,实现购物车的增删改查简单功能
- 根据java数组源码写的自己的数组的简单的功能的实现
- java读写服务器web项目中的配置文件
- Java实现最简单的网盘系统
- java控制台实现的简单小系统,支持计算器功能,日历、乘法表算法
- java简单Socket服务端与客户端通讯demo
- java通过jquery实现顶和踩的web实例
- 最基础的通过myeclipse搭建框架开发java web工程的步骤详解
- JAVAWEB新手学堂代码HelloWeb
- aop aspectj简单实例,一看就懂
- java编写一个迷你版的Tomcat服务器,适合初学者
- Alipay之web简单的支付功能沙箱java代码demo
请下载代码后再发表评论



3038169233 LV1
2024年6月29日
柳咪华沙 LV7
2024年6月28日
tartaglia LV2
2024年4月16日
annazhang LV29
2023年10月3日
2036495585 LV9
2023年9月26日
卢本伟不开挂 LV4
2023年9月17日
cheung524071 LV8
2023年8月23日
sdhfkh LV4
2023年6月12日
张三asdcsv
2023年6月6日
暂无贡献等级
微信网友_6411724627349504 LV3
2023年4月3日