{"id":4629,"date":"2010-04-03T17:00:33","date_gmt":"2010-04-03T08:00:33","guid":{"rendered":"http:\/\/www.sssg.org\/blogs\/hiro345\/?p=4629"},"modified":"2010-04-04T15:15:07","modified_gmt":"2010-04-04T06:15:07","slug":"com-sun-net-httpserver-httpserver","status":"publish","type":"post","link":"https:\/\/www.hiro345.net\/blogs\/hiro345\/archives\/4629.html","title":{"rendered":"com.sun.net.httpserver.HttpServer"},"content":{"rendered":"<p>Java SE 6 \u304b\u3089\u3001com.sun.net.httpserver.HttpServer \u3068\u3044\u3046\u7c21\u6613Web\u30b5\u30fc\u30d0\u304c\u5185\u8535\u3055\u308c\u3066\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u306a\u306e\u3067\u3001\u305d\u308c\u3092\u4f7f\u3063\u3066\u307f\u308b\u7c21\u5358\u306a\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u6210\u3057\u3066\u307f\u307e\u3057\u305f\u3002Jetty\u3092\u4f7f\u3046\u307e\u3067\u3082\u306a\u3044\u3068\u304d\u306b\u91cd\u5b9d\u3057\u305d\u3046\u3067\u3059\u3002<\/p>\n<div class=\"code\" style=\"overflow: scroll; width: 490px; white-space: nowrap;background-color:black;color:white;padding:4px;\">import&nbsp;java.io.IOException&#x3b;<br \/>\nimport&nbsp;java.io.OutputStream&#x3b;<br \/>\nimport&nbsp;java.net.InetSocketAddress&#x3b;<\/p>\n<p>import&nbsp;com.sun.net.httpserver.HttpExchange&#x3b;<br \/>\nimport&nbsp;com.sun.net.httpserver.HttpHandler&#x3b;<br \/>\nimport&nbsp;com.sun.net.httpserver.HttpServer&#x3b;<\/p>\n<p>public&nbsp;class&nbsp;Httpd&nbsp;{<br \/>\n&nbsp;&nbsp;final&nbsp;static&nbsp;private&nbsp;int&nbsp;PORT&nbsp;=&nbsp;8081&#x3b;<br \/>\n&nbsp;&nbsp;private&nbsp;String&nbsp;message&nbsp;=&nbsp;&quot;Hello&nbsp;com.sun.net.httpserver.HttpServer.&quot;&#x3b;<br \/>\n&nbsp;&nbsp;private&nbsp;HttpServer&nbsp;server&#x3b;<br \/>\n&nbsp;&nbsp;private&nbsp;HttpHandler&nbsp;handler&#x3b;<br \/>\n&nbsp;&nbsp;public&nbsp;Httpd()&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;handler&nbsp;=&nbsp;new&nbsp;HttpHandler()&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;handle(HttpExchange&nbsp;he)&nbsp;throws&nbsp;IOException&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(new&nbsp;java.util.Date())&#x3b;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OutputStream&nbsp;os&nbsp;=&nbsp;null&#x3b;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;os&nbsp;=&nbsp;he.getResponseBody()&#x3b;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;he.sendResponseHeaders(200,&nbsp;0)&#x3b;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;os.write(message.getBytes())&#x3b;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;finally&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(os&nbsp;&#x21;=&nbsp;null)&nbsp;os.close()&#x3b;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;&#x5c;t&quot;&nbsp;+&nbsp;message)&#x3b;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}&#x3b;<br \/>\n&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;public&nbsp;void&nbsp;run()&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;=&nbsp;HttpServer.create(new&nbsp;InetSocketAddress(PORT),&nbsp;0)&#x3b;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(IOException&nbsp;e)&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace()&#x3b;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;server.createContext(&quot;&#x2f;&quot;,&nbsp;handler)&#x3b;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;server.start()&#x3b;<br \/>\n&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Httpd&nbsp;httpd&nbsp;=&nbsp;new&nbsp;Httpd()&#x3b;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;httpd.run()&#x3b;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(Exception&nbsp;e)&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace()&#x3b;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;}<br \/>\n}<\/p><\/div>\n<p>\u30d6\u30e9\u30a6\u30b6\u3067 http:\/\/localhost:8081\/ \u3078\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306a\u5b9f\u884c\u7d50\u679c\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<div class=\"code\" style=\"overflow: scroll; width: 490px; white-space: nowrap;background-color:black;color:white;padding:4px;\">&gt;java&nbsp;Httpd<br \/>\nSat&nbsp;Apr&nbsp;03&nbsp;15&#x3a;03&#x3a;09&nbsp;JST&nbsp;2010&nbsp;&nbsp;&nbsp;&nbsp;Hello&nbsp;com.sun.net.httpserver.HttpServer.<\/div>\n<p>\u30d6\u30e9\u30a6\u30b6\u306b\u306f\u4e0b\u8a18\u30e1\u30c3\u30bb\u30fc\u30b8\u304c\u8868\u793a\u3055\u308c\u307e\u3059\u3002<br \/>\nHello&nbsp;com.sun.net.httpserver.HttpServer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java SE 6 \u304b\u3089\u3001com.sun.net.httpserver.HttpServer \u3068\u3044\u3046\u7c21\u6613Web\u30b5\u30fc\u30d0\u304c\u5185\u8535\u3055\u308c\u3066\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u306a\u306e\u3067\u3001\u305d\u308c\u3092\u4f7f\u3063\u3066\u307f\u308b\u7c21\u5358\u306a\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u6210\u3057\u3066\u307f\u307e\u3057\u305f\u3002Jetty\u3092\u4f7f\u3046\u307e &hellip; <a href=\"https:\/\/www.hiro345.net\/blogs\/hiro345\/archives\/4629.html\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,15],"tags":[297],"class_list":["post-4629","post","type-post","status-publish","format-standard","hentry","category-java","category-program","tag-com-sun-net-httpserver-httpserver"],"_links":{"self":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/4629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/comments?post=4629"}],"version-history":[{"count":4,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/4629\/revisions"}],"predecessor-version":[{"id":4633,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/4629\/revisions\/4633"}],"wp:attachment":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/media?parent=4629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/categories?post=4629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/tags?post=4629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}