Listing 2.2. HttpServer1 类的 await 方法public void await() {ServerSocket serverSocket = null;int port = 8080;try {serverSocket = new ServerSocket(port, 1,InetAddress.getByName("127.0.0.1")); }catch (IOException e) {e.printStackTrace();System.exit(1); }// 循环,等待一个请求while (!shutdown) {Socket socket = null;InputStream input = null;OutputStream output = null;try {socket = serverSocket.accept();input = socket.getInputStream();output = socket.getOutputStream();// 创建请求对象并解析Request request = new Request(input);request.parse();// 创建回应对象Response response = new Response(output);response.setRequest(request);//检测是否是 servlet 或静态资源的请求//servlet 请求以 "/servlet/" 开始 if (request.getUri().startsWith("/servlet/")) {ServletProcessor1 processor = new ServletProcessor1();processor.process(request, response); }else {StaticResourceProcessor processor =new StaticResourceProcessor();processor.process(request, response); }// 关闭socketsocket.close();//检测是否前面的 URI 是一个 shutdown 命令shutdown = request.getUri().equals(SHUTDOWN_COMMAND); }catch (Exception e) {e.printStackTrace();System.exit(1); } }}
public Object getAttribute(String attribute) {return null; }public Enumeration getAttributeNames() {return null; }public String getRealPath(String path) {
新闻热点
疑难解答