实现Processor
java">package Webserver.src.processor;import java.io.IOException;import Webserver.src.connector.Request;
import Webserver.src.connector.Response;public class StaticProcessor {public void process(Request request, Response response) {try {response.sendStaticResource();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
}
- 访问静态资源
- 简单的调用response中的sendStaticResource即可
实现Connector
java">package Webserver.src.connector;import java.io.Closeable;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;import Webserver.src.processor.StaticProcessor;public class Connector implements Runnable{private static final int DEFAULT_PORT = 8888;private ServerSocket server;private int port;//绑定端口public Connector() {this(DEFAULT_PORT);}public Connector(int port) {this.port = port;}public void start() {//创建线程并启动该线程,会自动调用线程中的run方法Thread thread = new Thread(this);thread.start();}@Overridepublic void run() {try {//创建服务器socketserver = new ServerSocket(port);System.out.println("启动服务器,监听端口:" + port);//BIO式建立网络间通讯while (true) {//server.accept会阻塞直到链接成功建立Socket socket = server.accept();InputStream input = socket.getInputStream();OutputStream output = socket.getOutputStream();//创建Request并解析HTTP请求头中的uriRequest request = new Request(input);request.parse();//创建Response并绑定request以获取uriResponse response = new Response(output);response.setRequest(request);//调用process读取文件内容并输出到输出流中StaticProcessor processor = new StaticProcessor();processor.process(request, response);//及时关闭socket节省服务器资源close(socket);}} catch (Exception e) {// TODO: handle exceptione.printStackTrace();} finally {//最后确保关闭服务器,节省资源close(server);}}private void close(Closeable closeable) {if (closeable != null) {try {closeable.close(); } catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}}
}
- 建立了个BIO模型的网络间通讯服务器
- 调用process将请求的文件内容输出到输出流中
实现客户端TestClient以及服务器端Bootstrap
TestClient
java">package Webserver.src.test;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;public class TestClient {public static void main(String[] args) throws UnknownHostException, IOException {//建立socket监听主机:"localhost" 端口:8888Socket socket = new Socket("localhost", 8888);OutputStream output = socket.getOutputStream();//发送HTTP请求并及时关闭输出流,节省资源output.write("GET /index.html HTTP/1.1".getBytes());socket.shutdownOutput();//读入输入中的内容并输出InputStream input = socket.getInputStream();byte [] buffer = new byte[2048];int length = input.read(buffer);StringBuilder response = new StringBuilder();for (int i = 0; i < length; i ++) {response.append((char)buffer[i]);}System.out.println(response.toString());socket.shutdownInput();//关闭socket,关闭客户端socket.close();}
}
- 建立客户端
- 接受客户端传入的消息并打印
Bootstrap
java">package Webserver.src;import Webserver.src.connector.Connector;public class Bootstrap {public static void main(String[] args) {Connector connector = new Connector();connector.start();}
}
- 启动服务器端
- 等待客户端连接和解析其发送的请求
测试结果
先后启动服务器端以及客户端
https://i-blog.csdnimg.cn/direct/6f760c5c98f241c08f5f4800d7875b76.png" alt="在这里插入图片描述" />
- 可见成功返回200 OK的HTTP响应头和请求的文件内容
启动服务器端并在web浏览器上发送请求
- 发送http://localhost:8888/index.html请求
https://i-blog.csdnimg.cn/direct/966581ad7e9342809d13826d209edc3c.png" alt="在这里插入图片描述" />
成功返回对应的网页内容
- 发送不合法的请求
https://i-blog.csdnimg.cn/direct/fd822d0d94b94738b1f443e5f08d4e89.png" alt="在这里插入图片描述" />
成功返回File not found的网页内容