Java實現簡單的HTTP服務器
Java實現簡單的HTTP服務器
Java實現簡單的HTTP服務器
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.charset.*;
import java.util.concurrent.*;
import java.io.*;
public class SimpleHttpServer {
private int port = 80;
private ServerSocketChannel serverSocketChannel = null;
private ExecutorService executorService;
private static final int POOL_MULTIPLE = 4;
public SimpleHttpServer() throws IOException {
executorService = Executors.newFixedThreadPool(Runtime.getRuntime()
.availableProcessors() * POOL_MULTIPLE);
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().setReuseAddress(true);
serverSocketChannel.socket().bind(new InetSocketAddress(port));
}
public void service() {
while (true) {
SocketChannel socketChannel = null;
try {
socketChannel = serverSocketChannel.accept();
executorService.execute(new Handler(socketChannel));
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
new SimpleHttpServer().service();
}
}
class Handler implements Runnable {
private SocketChannel socketChannel;
public Handler(SocketChannel socketChannel) {
this.socketChannel = socketChannel;
}
@Override
public void run() {
handle(socketChannel);
}
private void handle(SocketChannel socketChannel) {
try {
Socket socket = socketChannel.socket();
System.out
.println(socket.getInetAddress() + ":" + socket.getPort());
ByteBuffer buffer = ByteBuffer.allocate(1024);
socketChannel.read(buffer);
buffer.flip();
String request = decode(buffer);
StringBuffer sb = new StringBuffer("HTTP/1.1 200 OK\r\n");
sb.append("Content-Type:text/html\r\n\r\n");
socketChannel.write(encode(sb.toString()));
FileInputStream in = null;
String firstLineOfRequest = request.substring(0,
request.indexOf("\r\n"));
if (firstLineOfRequest.indexOf("login.htm") != -1)
in = new FileInputStream("login.htm");
else
in = new FileInputStream("hello.htm");
FileChannel fileChannel = in.getChannel();
fileChannel.transferTo(0, fileChannel.size(), socketChannel);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (socketChannel != null)
socketChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private Charset charset = Charset.forName("GBK");
private ByteBuffer encode(String string) {
return ByteBuffer.allocate(string.length() * 2).get(
string.getBytes(charset));
}
private String decode(ByteBuffer buffer) {
byte[] source = new byte[buffer.position() + 1];
buffer.put(source);
return new String(source, charset);
}
}
login.htm
<html> <head> <title>helloapp</title> </head> <form name="loginForm" method="post" action="hello.htm"> <table> <tr><td><div align="right">用戶名:</div></td> <td><input type="text" name="username"></td> </tr> <tr><td><div align="right">密碼:</div></td> <td><input type="password" name="password"></td> </tr> <tr><td></td> <td><input type="submit" name="submit" value="submit"></td> </tr> </table> </form> </body> </html>
本文由用戶 AliBarbour 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!