280 likes | 489 Views
Java 網路程式設計. 第 10 章 認識 Web 架構下的 Java 類別. 三種知名的分散式檔案系統. NFS(Network File System), Sun Microsystems, Inc 。 AFS(Andrew File System), Transarc Corporation 。 DFS(Distributed File System), OSF DCE 的一部分。. 分散式檔案系統. NFS 應用的例子. AFS 應用的例子. DCE 的結構. 思索網路應用的開發. 主從式架構 分散式系統 (Distributed System).
E N D
Java網路程式設計 第10章 認識Web架構下的Java類別
三種知名的分散式檔案系統 • NFS(Network File System), Sun Microsystems, Inc。 • AFS(Andrew File System), Transarc Corporation。 • DFS(Distributed File System), OSF DCE的一部分。
思索網路應用的開發 • 主從式架構 • 分散式系統(Distributed System)
主從架構的分類 • 從主端(或伺服端)的特性來分類 • 從主從之間的關係來分類
各種主從架構 • 同時性連線導向(Concurrent, Connection-Oriented) • 同時性非連線導向(Concurrent, Connectionless) • 複迴性連線導向(Iterative, Connection-Oriented) • 複迴性非連線導向(Iterative, Connectionless)
主從架構系統開發的基本觀念 • 需求分析 • 環境的規劃與建立 • 應用系統的設計 • 資料庫設計 • 應用系統的分割 • 效能調整與系統測試
從Web架構下的Java類別出發 • HTTP協定 • HTTPRequest • URL網址
MyHTTPServer.java import java.net.*; class MyHTTPServer { public static void main(String[] args) { ServerSocket server; Socket client; HTTPRequest request; try { server = new ServerSocket(1250); while (true) { client = server.accept(); request = new HTTPRequest(client); request.process(); } } catch (Exception e) { System.err.println("無法啟動HTTP Server : " + e.getMessage()); e.printStackTrace(); } } }
HTTPRequest.java import java.net.*; import java.io.*; import java.util.*; class HTTPRequest { private Socket client; public HTTPRequest(Socket client) { this.client = client; } public void process() { // 取得client端的輸入與輸出串流 try { PrintStream outputStream = new PrintStream(client.getOutputStream()); BufferedReader bufr =new BufferedReader(new InputStreamReader(client.getInputStream())); // 讀取HTTP請求 String request = bufr.readLine().trim(); // 處理請求的內容 StringTokenizer stringToken = new StringTokenizer(request); // 讀取method String header_method = stringToken.nextToken(); // 檢查是否支援method if (!header_method.equals("GET")) { outputStream.print("HTTP協定不支援的method\r\n"); outputStream.flush(); return; } // 讀取URI String header_uri = stringToken.nextToken(); // 讀取header StringBuffer returnDocument = new StringBuffer(); // 建立回應 returnDocument.append("注意URL中所指定的檔案為 : " + header_uri + "\r\n"); returnDocument.append("檔案儲存的位置是相對於document root \n"+ " 跟一般的HTTP server的規矩一樣,client端需提供以下的header資訊 :\r\n"); String line = null; // 讀取header的其餘部分, 直到遇到空白行 while ((line = bufr.readLine().trim()) != null && line.length() > 0) { returnDocument.append(line + "\r\n"); } returnDocument.append("在這樣的運作下才能打開檔案傳資訊給client\r\n"); // 傳回response outputStream.print("HTTP/1.0 200 OK\r\n"); outputStream.print("Content-length: " + returnDocument.length() + "\r\n"); outputStream.print("Content-type: text/plain\r\n\r\n"); outputStream.print(returnDocument); outputStream.flush(); } catch (Exception e) { System.err.println("無法處理HTTPRequest: "+ e.getMessage()); e.printStackTrace(); } finally { try { client.close(); } catch (IOException e) { // 忽略不處理 } } } }