自己動手寫乙個伺服器,不能說水平會有多大的提公升,但是讓我知道servlet是如何與伺服器進行互動的。現在將簡單的模擬實現記錄下來。(此處記錄的是伺服器模擬的簡易版本,以後還會寫乙個利用註解實現的伺服器版本)
分析圖:
} //判定引數
if(paremter==null)
//將引數放入map集合中
parseparemter(paremter);
} /**
* 解析引數,將引數放入集合中
* @param paremter
*/private void parseparemter(string paremter) else
} }
public string geturl()
public string getmethod()
public listgetparamters(string name)
return null;
} public string getparamter(string name)
return null;} }
response**:
public class response
public response(outputstream os)
/* * 拼裝響應頭
*/private void createhandler(int code)
}/* * 要顯示到瀏覽器上的內容
*/public response print(string respcontext)
/*** 通過流將封裝好的響應內容寫出到瀏覽器
* @param code
* @throws ioexception
*/public void pushtoclient(int code) throws ioexception
}
servlet**:
public abstract class servlet
public abstract void doget(request req,response resp);
public abstract void dopost(request req,response resp);
}
private mapservlet;
servlet=new hashmap<>();
}} }
public mapgetservlet()
public void setservlet(mapservlet) }
servletcontext**:
public class servletcontext
public servlet getservlet(string url) throws instantiationexception, illegalacces***ception, classnotfoundexception
}
server**:
public class server }
}
dispacher**:
public class dispatcher implements runnable catch (exception e)
} @override
public void run() catch (instantiationexception e) catch (illegalacces***ception e) catch (classnotfoundexception e)
} }}
loginservlet**:
public class loginservlet extends servlet catch (ioexception e)
} @override
public void dopost(request req, response resp)
}
**的組織結構:
前端測試:
web伺服器模擬實現
本文源於51cto。直接上 山寨 web伺服器 1 支援多個 瀏覽器訪問 多執行緒 2 如何提供服務 socket 3 如何返回響應 io author phenix public class tomcatserver 服務啟動方法 建立socket伺服器 public void start int...
JAVA 模擬TomCat伺服器
一 模擬tomcat伺服器 二 模擬客戶端瀏覽器 http 協議版本 狀態碼 服務的提供 server apache coyote 1.1 支援的範圍 accept ranges bytes 標記 token etag w 304 1516331676000 最後的修改時間 last modifie...
Udp 模擬實現客戶端與伺服器通訊
客戶端模組 include include include std string include close介面 include atoi介面 include 位址結構定義 include 位元組序轉換介面 include 套接字介面 class udpsocket 1.建立套接字 bool soc...