tomcat是乙個web容器 用來接收請求和發出響應
即request和response 以及servlet
http協議包裝著tap協議 所以可以用tcp協議來模擬;
1、在main方法中使用
socket方法來接受請求;獲取輸入流和輸出流,並將請求資料報裝在存在字串內
2、封裝requestserversocket serversocket =
newserversocket
(52011);
//設定埠號
socket accept = serversocket.
accept()
;//開啟接受服務
inputstream inputstream=accept.
getinputstream()
; outputstream outputstream=accept.
getoutputstream()
; bufferedreader bufferedreader =
newbufferedreader
(new
inputstreamreader
(accept.
getinputstream()
));//設定輸入流
printwriter printwriter =
newprintwriter
(accept.
getoutputstream()
);//設定輸出流
stringbuffer sb=
newstringbuffer()
;//使用高效能版字串拼接
while
(true
) sb.
(temp)
; sb.
("\n");
} string requeststr = sb.
tostring()
;
將上述獲取的字串封裝到request中並用map集合儲存起來,並且建立獲取頭資訊和parameter的方法
3、response和servlet只有簡單的方法public
class
binrequset
int in = url.
indexof
("?");
if(in >-1
)}else
}public map
getheaders()
public string getparameter
(string key)
}
4、configutil類和web.configpublic
class
binresponse
}public
class
binservlet
public
void
dopost
(binrequset requset,binresponse response)
}
web.config是指請求路徑對應controller
configutil是載入配置檔案的工具類
後期將configutil做成兩個 乙個讀取軟體部署的路徑,即tomcat內部的config的web.xml,public
class
configutil
configmap.
put(temp.
split
("=")[
0],temp.
split
("=")[
1]);
}}catch
(exception e)
}public
static string getclass
(string key)
}
乙個讀取專案中的web.xml下的servlet路徑配置
乙個讀取專案中的web.xml下的servlet路徑配置public
class
configutil
configmap.
put(temp.
split
("=")[
0],temp.
split
("=")[
1]);
}}catch
(exception e)
get();
}public
static string getconfig
(string key)
public()
}
在主方法中new request就可以接受請求並且獲取類;public
class
webconfigutil
"=")[0]+"----"+ temp.split("=")[1]);
pathtoclassmap.
put(temp.
split
("=")[
0], temp.
split
("=")[
1]);
}}catch
(exception e)
}public
static string getclassname
(string path)
}
以上伺服器端的request,response,servlet 封裝完成
5、開始封裝類載入器
封裝乙個我們的classloader,繼承classloader並且繼承他的findclass方法
在編寫乙個載入類的工具loadutil,public
class
myclassloader
extends
classloader
//findclass是找到對應的包名 ,然後我們需要找到對應的class檔案
@override
protected class<
?>
findclass
(string name)
catch
(exception e)
class clazz
=defineclass
(name, classbytes,
0, classbytes.length)
;return clazz;
}}
6、web服務建立乙個webservicepublic
class
loaderutil
//獲取不帶字尾的檔名
public
static string getfilenamewithoutsuffix
(file file)
//獲取所有的
public
static
void
getclasslist
classnamelist)
else
//新增檔案到}}
}}public
static
void
loaderclass()
}}public
static class getclass
(string classname)
}
7、呼叫主方法public
class
webservice
sb.(temp)
; sb.
("\n");
}//獲取到所有請求資料後需要分析第一行
//將中間部分那出來
//然後將其他的每個字段封裝到binrequest中
string requeststr = sb.
tostring()
; binrequest request =
newbinrequest
(requeststr, inputstream)
;//建立request物件
binresponse response =
newbinresponse
(outputstream)
; string path = request.
getheaders()
.get
("path");
//找個這個path,然後讀取配置檔案,找到這個path對應的類
string classname = webconfigutil.
getclassname
(path)
; class clazz
= loaderutil.
getclass
(classname)
;if(clazz!=null)
else
}}
public
class
main
}
Web容器預設的servlet
1.1 各類web伺服器預設servlet名稱 web容器 預設servlet名稱 tomcat,jetty,jboss,and glassfish default ah default resin resin file weblogic fileservlet websphere fileserv...
Spring容器的介紹即簡易搭建
spring是乙個輕量級開源框架,為了解決企業應用開發的複雜性而建立的,其主要優勢是分層架構,分層架構允許使用者選擇使用哪乙個元件,同時為 j2ee 應用程式開發提供整合的框架。正是因為spring是乙個容器性質的框架,所以裝什麼物件就會有什麼功能,所以可以是一站式的,不僅僅不排斥其他框架,還能幫其...
打造自己的web容器(1)
b color red socket入門 color b 近日,讀到一本好書 how tomcat works 該書詳盡分析了tomcat的實現原理,解釋了它的servlet容器的內部執行機制,讀來非常有收穫,特此撰文將讀書過程中的一些心得付諸文字。color red http協議基礎知識 colo...