My First Web Server
Httpserver.java
1 PackageCom.xiejiaohui.ch1;2 3 ImportJava.io.File;4 Importjava.io.IOException;5 ImportJava.io.InputStream;6 ImportJava.io.OutputStream;7 Importjava.net.InetAddress;8 ImportJava.net.ServerSocket;9 ImportJava.net.Socket;Ten One Public classHttpserver { A - Public Static FinalString web_root = System.getproperty ("User.dir") + File.separator + "Webroot"; - Private Static FinalString Shutdown_command = "/shutdown"; the - Private Booleanshutdown =false; - - Public Static voidMain (string[] args) { +Httpserver Server =Newhttpserver (); - server.await (); + } A at Public voidawait () { -ServerSocket ServerSocket =NULL; - intPort = 8080; - Try { -ServerSocket =NewServerSocket (port, 1, Inetaddress.getbyname ("127.0.0.1")); -}Catch(IOException e) { in e.printstacktrace (); -System.exit (1); to } + while(!shutdown) { -Socket socket =NULL; theInputStream input =NULL; *OutputStream output =NULL; $ Try {Panax NotoginsengSocket =serversocket.accept (); -input =Socket.getinputstream (); theOutput =Socket.getoutputstream (); +Request Request =NewRequest (input); A request.parse (); the +Response Response =NewResponse (output); - response.setrequest (request); $ Response.sendstaticresource (); $ - socket.close (); - theshutdown =Request.geturi (). Equals (Shutdown_command); -}Catch(Exception e) {Wuyi e.printstacktrace (); the Continue; - } Wu } - About } $ -}
Request.java
1 PackageCom.xiejiaohui.ch1;2 3 Importjava.io.IOException;4 ImportJava.io.InputStream;5 6 Public classRequest {7 PrivateInputStream input;8 PrivateString URI;9 Ten PublicRequest (InputStream input) { One This. Input =input; A } - - Public voidParse () { theStringBuffer request =NewStringBuffer (2048); - inti; - byte[] buffer =New byte[2048]; - Try { +i =input.read (buffer); -}Catch(IOException e) { + e.printstacktrace (); Ai =-1; at } - for(intj = 0; J < I; J + +) { -Request.append ((Char) buffer[j]); - } - System.out.print (request.tostring ()); -URI =Parseuri (request.tostring ()); in } - to Privatestring Parseuri (String requeststring) { + intindex1, Index2; -Index1 = Requeststring.indexof ("); the if(Index1! =-1) { *Index2 = Requeststring.indexof (", Index1 + 1); $ if(Index2 >index1) {Panax Notoginseng returnRequeststring.substring (index1 + 1, index2); - } the } + return NULL; A } the + PublicString GetURI () { - returnURI; $ } $ -}
Response.java
1 PackageCom.xiejiaohui.ch1;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 Importjava.io.IOException;6 ImportJava.io.OutputStream;7 8 Public classResponse {9 Ten Private Static Final intBuffer_size = 1024; One PrivateRequest request; A Privateoutputstream output; - - PublicResponse (OutputStream output) { the This. Output =output; - } - - Public voidsetrequest (Request request) { + This. Request =request; - } + A Public voidSendstaticresource ()throwsIOException { at byte[] bytes =New byte[buffer_size]; -FileInputStream FIS =NULL; - Try { -File File =NewFile (Httpserver.web_root, Request.geturi ()); - if(File.exists ()) { -FIS =Newfileinputstream (file); in intch = fis.read (Bytes, 0, buffer_size); - while(ch! =-1) { toOutput.write (Bytes, 0, ch); +ch = fis.read (Bytes, 0, buffer_size); - } the}Else { *String errormessage = "http/1.1 404 File Not found\r\n" + $"Content-type:text/html\r\n" +Panax Notoginseng"Content-length:23\r\n" + -"\ r \ n" + the"; + Output.write (Errormessage.getbytes ()); A } the}Catch(Exception e) { + e.printstacktrace (); - System.out.println (e.tostring ()); $}finally { $ if(FIS! =NULL) { - fis.close (); - } the } - }Wuyi the -}
My First Web Server