Анхны
бичил вэб үйлчлэгчийг бага зэрэг сайжруулан боловсронгуй болгосон хувилбарыг дор харуулав. Энд үйлчлүүлэгчээс ирэх хүсэлтийг уншихдаа InputStream обьектыг бус, харин мэдээллийг мөр, мөрөөр нь унших чадвартай BufferedReader обьектыг ашигласан байна. Мөн мөрийн задаргаа хийх жишээ болгон GET хүсэлт доторхи URL хэсгийг ялган авч харуулав. Ингэснээр URL-ээр өгөгдсөн файлыг үйлчлүүлэгч рүү хариу болгон илгээх зам нээгдэнэ.
package web.server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.logging.Logger;
/**
*
* @author Erdenebat Byambaa, USI company
* very simple, very ugly implementation of web server
*/
public class SimpleServer {
static Logger log = Logger.getLogger("SimpleServer");
static final int PORT = 1010;
static final String NEWLINE = "\r\n";
static final String OK = "HTTP/1.1 200 OK" + NEWLINE;
static String WelcomeMessage = "<html>" +
"<head><title>Welcome</title></head>" +
"<body><h1>" +
"Welcome, Sir/Madame" +
"</h1></body>"+
"</html>";
static long length = WelcomeMessage.length();
public static void main(String[] args) throws IOException {
int port = PORT;
if (args.length > 0)
port = Integer.parseInt(args[0]);
System.out.println();
log.info("SimpleServer is listening on " + port);
System.out.println();
// creating server socket
ServerSocket server = new ServerSocket(port);
// main loop
while (true) {
// accepting a new client
Socket socket = server.accept();
int remotePort = socket.getPort();
log.info("accepting new client on " + remotePort);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
String line = br.readLine();
String[] tokens = line.split(" ");
String rawUrl = tokens[1];
log.info("requested resource is " + rawUrl);
System.out.println();
while ( (line = br.readLine()) != null && !line.equals("")) {
for (byte data: line.getBytes()) {
String hex = Integer.toHexString(data);
hex = (hex.length()==1? "0"+hex: hex);
System.out.print(hex + " ");
}
System.out.println("0d 0a");
System.out.println(line);
System.out.println();
}
if (line.equals("")) {
System.out.println("0d 0a");
}
// so, here we have completed receiving data from client
System.out.println();
System.out.println("sending response ...");
// sending OK
out.write(OK.getBytes());
// starting http header sending
out.write("Content-Type: text/html".getBytes());
out.write(NEWLINE.getBytes());
out.write("Content-Length: ".getBytes());
out.write(Long.toString(length).getBytes());
out.write(NEWLINE.getBytes());
out.write("Date: ".getBytes());
out.write(new Date().toString().getBytes());
out.write(NEWLINE.getBytes());
out.write("Server: Simplest Web Server".getBytes());
out.write(NEWLINE.getBytes());
// end of header sending
out.write(NEWLINE.getBytes());
// sending body part
out.write(WelcomeMessage.getBytes());
in.close();
out.close();
socket.close();
System.out.println();
log.info("disconnected --- remote port: " + remotePort);
System.out.println();
} // main loop
} // main method
} // SimpleServer
хөтөлбөрийн гарц:Nov 15, 2006 4:18:25 PM web.server.SimpleServer main
INFO: SimpleServer is listening on 1010
Nov 15, 2006 4:18:52 PM web.server.SimpleServer main
INFO: accepting new client on 1056
Nov 15, 2006 4:18:52 PM web.server.SimpleServer main
INFO: requested resource is /
48 6f 73 74 3a 20 6c 6f 63 61 6c 68 6f 73 74 3a 31 30 31 30 0d 0a
Host: localhost:1010
55 73 65 72 2d 41 67 65 6e 74 3a 20 4d 6f 7a 69 6c 6c 61 2f 35 2e 30 20 28 57 69 6e 64 6f 77 73 3b 20 55 3b 20 57 69 6e 64 6f 77 73 20 4e 54 20 35 2e 31 3b 20 65 6e 2d 55 53 3b 20 72 76 3a 31 2e 38 2e 30 2e 38 29 20 47 65 63 6b 6f 2f 32 30 30 36 31 30 32 35 20 46 69 72 65 66 6f 78 2f 31 2e 35 2e 30 2e 38 0d 0a
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.8) Gecko/20061025 Firefox/1.5.0.8
41 63 63 65 70 74 3a 20 74 65 78 74 2f 78 6d 6c 2c 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 6d 6c 2c 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 68 74 6d 6c 2b 78 6d 6c 2c 74 65 78 74 2f 68 74 6d 6c 3b 71 3d 30 2e 39 2c 74 65 78 74 2f 70 6c 61 69 6e 3b 71 3d 30 2e 38 2c 69 6d 61 67 65 2f 70 6e 67 2c 2a 2f 2a 3b 71 3d 30 2e 35 0d 0a
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
41 63 63 65 70 74 2d 4c 61 6e 67 75 61 67 65 3a 20 65 6e 2d 75 73 2c 65 6e 3b 71 3d 30 2e 35 0d 0a
Accept-Language: en-us,en;q=0.5
41 63 63 65 70 74 2d 45 6e 63 6f 64 69 6e 67 3a 20 67 7a 69 70 2c 64 65 66 6c 61 74 65 0d 0a
Accept-Encoding: gzip,deflate
41 63 63 65 70 74 2d 43 68 61 72 73 65 74 3a 20 49 53 4f 2d 38 38 35 39 2d 31 2c 75 74 66 2d 38 3b 71 3d 30 2e 37 2c 2a 3b 71 3d 30 2e 37 0d 0a
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
4b 65 65 70 2d 41 6c 69 76 65 3a 20 33 30 30 0d 0a
Keep-Alive: 300
43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 6b 65 65 70 2d 61 6c 69 76 65 0d 0a
Connection: keep-alive
0d 0a
sending response ...
Nov 15, 2006 4:18:52 PM web.server.SimpleServer main
INFO: disconnected --- remote port: 1056