Simplehttpserver blocking mode

Source: Internet
Author: User

Simplehttpserver.java

ImportJava.io.FileInputStream;Importjava.io.IOException;Importjava.net.InetSocketAddress;ImportJava.net.Socket;ImportJava.nio.ByteBuffer;ImportJava.nio.CharBuffer;ImportJava.nio.channels.FileChannel;ImportJava.nio.channels.ServerSocketChannel;ImportJava.nio.channels.SocketChannel;ImportJava.nio.charset.Charset;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors; Public classSimplehttpserver {Private intPort = 8088; PrivateServersocketchannel Serversocketchannel =NULL; PrivateExecutorservice Executorservice; Private Static Final intPool_multiple = 4;  PublicSimplehttpserver ()throwsIOException {executorservice= Executors.newfixedthreadpool (Runtime.getruntime (). Availableprocessors () *pool_multiple); Serversocketchannel=Serversocketchannel.open (); Serversocketchannel.socket (). Setreuseaddress (true); Serversocketchannel.socket (). Bind (Newinetsocketaddress (port)); System.out.println ("Server starting ..."); }             Public voidService () { while(true) {Socketchannel Socketchannel=NULL; Try{Socketchannel=serversocketchannel.accept (); Executorservice.execute (NewHandler (Socketchannel)); }            Catch(IOException e) {e.printstacktrace (); }        }    }             Public Static voidMain (string[] args)throwsIOException {Newsimplehttpserver (). Service (); }            classHandlerImplementsRunnable {PrivateSocketchannel Socketchannel;  PublicHandler (Socketchannel socketchannel) { This. Socketchannel =Socketchannel; }                 Public voidrun () {handle (Socketchannel); }                 Public voidhandle (Socketchannel socketchannel) {FileInputStream in=NULL; Try{Socket Socket=Socketchannel.socket (); System.out.println ("Connecting from:" + socket.getinetaddress () + ":" +Socket.getport ()); Bytebuffer Buffer= Bytebuffer.allocate (1024);                Socketchannel.read (buffer);                Buffer.flip (); String Request=decode (buffer);                                System.out.print (Request); StringBuffer SB=NewStringBuffer ("http/1.1 ok\r\n"); Sb.append ("Content-type:text/html\r\n\r\n");                                Socketchannel.write (Encode (sb.tostring ())); String firstlineofrequest= request.substring (0, Request.indexof ("\ r \ n")); if(Firstlineofrequest.indexof ("login.htm")! =-1) in=NewFileInputStream ("login.htm"); Elseinch=NewFileInputStream ("Hello.htm"); FileChannel FileChannel=In.getchannel (); Filechannel.transferto (0, Filechannel.size (), Socketchannel); }            Catch(Exception e) {//e.printstacktrace ();            }            finally {                Try {                    if(Socketchannel! =NULL) Socketchannel.close (); if(In! =NULL) In.close (); }                Catch(IOException e) {e.printstacktrace (); }            }        }                        PrivateCharset Charset =Charset.defaultcharset ();  PublicString decode (bytebuffer buffer) {Charbuffer Charbuff=charset.decode (buffer); returncharbuff.tostring (); }                 Publicbytebuffer encode (String str) {returnCharset.encode (str); }    }}

Login.htm

<HTMLLang= "ZH-CN"><Head>    <title>Login.htm</title>    <MetaCharSet= "Utf-8"> </Head><Body>    <formname= "LoginForm"Method= "POST"Action= "Hello.htm">/* <form name= "loginform" method= "Get" action= "hello.htm" > * /        <Table>            <TR><TD><DivAlign= "Right">User name:</Div></TD>                <TD><inputtype= "text"name= "username"></TD>            </TR>            <TR><TD><DivAlign= "Right">Password:</Div></TD>                <TD><inputtype= "Password"name= "Password"></TD>            </TR>            <TR><TD></TD>                <TD><inputtype= "Submit"name= "Submit"value= "Submit"></TD>            </TR>        </Table>    </form></Body></HTML>

Hello.htm

<HTMLLang= "ZH-CN"><Head>    <title>Hello.htm</title>    <MetaCharSet= "Utf-8"> </Head><Body>    <P>It works!</P></Body></HTML>

// GET Request (default)  /login. htm http/1.1*/*accept-language:zh-cnuser-agent:mozilla/ 4.0 (compatible; MSIE 8.0; Windows NT 6.1; trident/4.0; Qdesk 2.5.1277.202; SLCC2;. NET CLR 2.0.50727;. NET CLR 3.5.30729;. NET CLR 3.0.30729; Media Center PC 6.0) accept-encoding:gzip, Deflatehost:127.0.0.1:8088connection:keep-alive

 //  POST request  post /hello.htm http/accept:image /jpeg, Application/x-ms-application, Image/gif, Application/xaml+xml, Image/pjpeg, APPLICATION/X-MS-XBAP, *< Span style= "color: #008000;" >/*  referer:   http://127.0.0.1  :8088/login.htmaccept-language:zh-cnuser-agent:mozilla/ 4.0 (compatible; MSIE 8.0; Windows NT 6.1; trident/4.0; Qdesk 2.5.1277.202; SLCC2;. NET CLR 2.0.50727;. NET CLR 3.5.30729;. NET CLR 3.0.30729; Media Center PC 6.0) Content-type:application/x-www-form-urlencodedaccept-encoding:gzip, Deflatehost: 127.0.0.1:8088content-length:49connection:keep-alivecache-control:no-cacheusername=username&password= Password&submit=submit  

// Get request with variables  /hello.htm?username=username&password=password&submit=submit http/1.1Accept:image /jpeg, Application/x-ms-application, Image/gif, Application/xaml+xml, Image/pjpeg, APPLICATION/X-MS-XBAP, *  /*http://127.0.0.1: 8088/login.htmaccept-language:zh-cnuser-agent:mozilla/4.0 ( Compatible MSIE 8.0; Windows NT 6.1; trident/4.0; Qdesk 2.5.1277.202; SLCC2;. NET CLR 2.0.50727;. NET CLR 3.5.30729;. NET CLR 3.0.30729; Media Center PC 6.0) accept-encoding:gzip, Deflatehost:127.0.0.1:8088connection:keep-alive

Simplehttpserver blocking mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.