PackageCom.day5;ImportJava.io.BufferedWriter;Importjava.io.IOException;ImportJava.io.OutputStream;ImportJava.io.OutputStreamWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket;Importjava.util.Date; Public classServer {/**Liu Shihua Response response *@paramargs *@throwsException*/ Public Static voidMain (string[] args)throwsException {//Set line breakString CRLF = "\ r \ n"; //register for 8080 socketsServerSocket server=NewServerSocket (8080); System.out.println ("Start accepting requests ..."); //Accept client RequestsSocket socket =server.accept (); //get client Object output stream objectOutputStream out =Socket.getoutputstream (); //convert byte output stream to character output flowOutputStreamWriter writer =NewOutputStreamWriter (out); //convert character output to buffer character streamBufferedWriter BW =NewBufferedWriter (writer); StringBuilder Response=NewStringBuilder (); //Response message body contentString html= "; //Setting the response header informationResponse.append ("http/1.1-OK"). Append (CRLF); Response.append ("Server:apache/2.4.10 (WIN32) php/5.3.28"). Append (CRLF); Response.append ("Date:"). Append (NewDate ()). append (CRLF); Response.append ("Content-type:text/html"). Append (CRLF); Response.append ("Content-length:" +html.getbytes (). Length). append (CRLF); Response.append (CRLF); Response.append (HTML); //sending data to the clientBw.write (response.tostring ()); //Close Resource FlowBw.close (); Socket.close (); Server.close (); SYSTEM.OUT.PRINTLN (response); }}
Response message Format
http/1.1 okserver:apache/2.4.10 (Win32) php/5.3.28Jul 2018 14:52:32 gmtcontent-type : text/htmlcontent-length:17
02-response Simple Response message