天天看點

Servlet多線程單例模式

??瑷?锛?Servlet/JSP??????ASP??PHP绛??告??锛??變??跺?绾跨?杩?琛????鋒??寰?楂????ц????????變?Servlet/JSP榛?璁ゆ??浠ュ?绾跨?妯″??ц???锛???浠ワ??ㄧ???浠g???堕??瑕???甯哥??村?拌????澶?绾跨???瀹??ㄦ?ч??棰???

?

JSP??涓?瀛??ㄧ??澶?绾跨???棰?锛?

褰?瀹㈡?風??绗?涓?娆¤?鋒???涓?涓?JSP??浠舵?訛????$????璇?SP缂?璇???涓?涓?CLASS??浠訛?骞跺??寤轟?涓?璇ョ被??瀹?渚?锛??跺????寤轟?涓?绾跨?澶???CLIENT绔???璇鋒???濡?????澶?涓?瀹㈡?風?????惰?鋒?璇?SP??浠訛??????$??浼???寤哄?涓?绾跨???姣?涓?瀹㈡?風??璇鋒?瀵瑰?涓?涓?绾跨???浠ュ?绾跨??瑰??ц???澶уぇ??浣?瀵圭郴缁???璧?婧???姹?,??楂?绯葷???骞跺????????搴??堕??

瀵?SP涓????界?ㄧ????????璇存??濡?涓?:

瀹?渚?????: 瀹?渚????????ㄥ??涓???????,骞惰?灞?浜?璇ュ??渚???????绾跨??變韓锛???浠ヤ???绾跨?瀹??ㄧ??.

JSP绯葷???渚???8涓?绫誨????

JSP涓??ㄥ?扮??OUT,REQUEST,RESPONSE,SESSION,CONFIG,PAGE,PAGECONXT??绾跨?瀹??ㄧ??(??涓烘??涓?绾跨?瀵瑰???request锛?respone瀵矽薄?芥??涓?涓??風??锛?涓?瀛??ㄥ?變韓??棰?), APPLICATION?ㄦ?翠釜绯葷???琚?浣跨????浠ヤ???绾跨?瀹??ㄧ??.

灞??ㄥ????: 灞??ㄥ?????ㄥ????涓?????,??涓烘??涓?绾跨??芥??瀹???宸辯??????绌洪????浠ユ??绾跨?瀹??ㄧ??.

????绫? ????绫諱??ㄨ?瀹?渚???,灏卞???存?ヤ嬌??涔?涓???绾跨?瀹??ㄧ??.

澶??ㄨ?婧?: ?ㄧ?搴?涓????戒???澶?涓?绾跨???杩?绋????舵??浣???涓?涓?璧?婧?(濡?:澶?涓?绾跨???杩?绋????跺?逛?涓???浠惰?琛?????浣?).姝ゆ?朵?瑕?娉ㄦ????姝ラ??棰?.

浣垮??浠ュ??绾跨??瑰??ц?,杩??訛?浠??跺????涓?涓?瀹?渚?锛?????瀹㈡?風????璇鋒?浠ヤ覆琛??瑰??ц???杩??蜂???浣?绯葷????ц??

????

??棰?

??棰?涓?. 璇存????ervlet瀹瑰?ㄥ?浣????ㄥ??瀹?渚?澶?绾跨????瑰??ュ???璇鋒?

??棰?浜?. 濡?浣??ㄥ???涓?淇?璇?servlet????瀹?渚?澶?绾跨????瑰??ュ伐浣?(涔?灏辨??璇村?浣?寮???绾跨?瀹??ㄧ??servelt)??

????

涓?. Servlet瀹瑰?ㄥ?浣????舵?ュ???澶?涓?璇鋒? ???

Java????瀛?妯″??JMM锛?Java Memory Model锛?

JMM涓昏???涓轟?瑙?瀹?浜?绾跨?????瀛?涔??寸??涓?浜??崇郴???規??JMM??璁捐?★?绯葷?瀛??ㄤ?涓?涓誨??瀛?(Main Memory)锛?Java涓?????瀹?渚??????藉?ㄥ???ㄤ富瀛?涓?锛?瀵逛?????绾跨??芥???變韓????姣??$嚎绋??芥????宸辯??宸ヤ???瀛?(Working Memory)锛?宸ヤ???瀛??辯?瀛???????涓ら?ㄥ??缁???锛?缂?瀛?涓?淇?瀛?????涓誨??涓????????瘋?锛?缂?瀛????藉苟涓??誨??涓誨????姝ワ?涔?灏辨??缂?瀛?涓???????淇??瑰???芥病??绔??誨???頒富瀛?涓?锛?????涓?淇?瀛?????绾跨???灞??ㄥ????锛?绾跨?涔??存??娉??鎬??存?ヨ?塊??????涓??????????規??JMM锛???浠???浠ュ?璁烘??涓???璁ㄨ?虹??Servlet瀹?渚?????瀛?妯″???借薄涓哄?炬??绀虹??妯″????

Servlet多線程單例模式

???????? 宸ヤ???绾跨?Work Thread:?ц?浠g????涓?缁?绾跨???

???????? 璋?搴?嚎绋?Dispatcher Thread锛?姣?涓?绾跨??藉?鋒??????缁?瀹???绾跨?浼???绾?绾跨????規??浼???绾ц?搴??ц?????

???????

??????? Servlet???ㄥ?绾跨??ュ???澶?涓?璇鋒????惰?塊????servlet渚?璧?浜?涓?涓?绾跨?姹??ユ???¤?鋒???绾跨?姹?瀹???涓???涓?绯誨????宸ヤ???绾跨???????Servlet浣跨?ㄤ?涓?璋?搴?嚎绋??ョ?$??宸ヤ???绾跨???

??????

??????? 褰?瀹瑰?ㄦ?跺?頒?涓?Servlet璇鋒?锛?璋?搴?嚎绋?浠?绾跨?姹?涓????轟?涓?宸ヤ???绾跨?,灏?璇鋒?浼???缁?璇ュ伐浣???绾跨?锛??跺???辮?ョ嚎绋??ユ?ц?Servlet??service?規???褰?杩?涓?绾跨?姝e?ㄦ?ц????跺??,瀹瑰?ㄦ?跺?闆???涓?涓?璇鋒?,璋?搴?嚎绋????蜂?绾跨?姹?涓????哄???涓?宸ヤ???绾跨??ユ???℃?扮??璇鋒?,瀹瑰?ㄥ苟涓??沖?杩?涓?璇鋒??????塊????????涓?涓?Servlet.褰?瀹瑰?ㄥ???舵?跺?闆?瑰??涓?涓?Servlet??澶?涓?璇鋒????跺??锛??d?杩?涓?Servlet??service()?規?灏??ㄥ?绾跨?涓?骞跺???ц???

???????? Servlet瀹瑰?ㄩ?璁ら???ㄥ??瀹?渚?澶?绾跨????瑰??ュ???璇鋒?锛?杩??峰??灏?浜х??Servlet瀹?渚???寮???锛?????浜?瀵矽?鋒?????搴??堕?達?瀵逛?Tomcat??浠ュ??erver.xml涓???杩?<Connector>??绱?璁劇疆绾跨?姹?涓?绾跨????扮????

??????

??????? 灏卞???版?ヨ?達?

??????? 璋?搴???绾跨?绫繪????璐???璐d換濡??跺??瀛?锛?璇ョ被??璐d換??璋?搴?嚎绋?锛?????瑕??╃?ㄨ??宸辯??灞??у??????宸辯??璐d換????浠ヨ?ョ被???挎??浜?璐d換??锛?骞朵?璇ョ被??璐d換????涓??闆??涓?????浣?瀵矽薄涓??????朵?瀵矽薄??渚?璧?浜?璇ョ?瑰??瀵矽薄???挎????璐d換锛???浠?灏遍??瑕?寰??拌?ョ?瑰??瀵矽薄???h?ョ被灏辨??涓?涓???渚?妯″???瀹??頒????

???

娉ㄦ??锛????″?ㄥ??浠ヤ嬌?ㄥ?涓?瀹?渚??ュ???璇鋒?锛?浠f?垮??涓?瀹?渚???璇鋒?????甯??ョ????????棰??????″?ㄥ??寤轟?涓?Servlet绫葷??澶?涓?Servlet瀹?渚?缁?????瀹?渚?姹?锛?瀵逛?姣?涓?璇鋒?????Servlet瀹?渚?杩?琛???搴?澶???锛?涔????懼???闆??渚?姹?涓?绛?寰?涓?姝よ?鋒???杩??峰氨????骞跺??璁塊??????棰???

姝ゆ??灞??ㄥ????(瀛?娈?涔???瀹??ㄧ??锛?浣?瀵逛??ㄥ????????變韓?版????涓?瀹??ㄧ??锛???瑕?杩?琛???姝ュ???????瀵逛?杩??峰?瀹?渚???????ingleThreadModel?ュ?e苟涓??借В?沖苟??璁塊????棰??? SingleThreadModel?ュ?e??ervlet瑙???涓?宸茬?琚?搴?寮?浜???

浜? 濡?浣?寮???绾跨?瀹??ㄧ??Servlet

?

????1??瀹???SingleThreadModel ?ュ??

????璇ユ?ュ?f??瀹?浜?绯葷?濡?浣?澶???瀵瑰??涓?涓?Servlet??璋??ㄣ??濡???涓?涓?Servlet琚?杩?涓??ュ?f??瀹?,?d??ㄨ?涓?Servlet涓???service?規?灏?涓?浼???涓や釜绾跨?琚????舵?ц?锛?褰??朵?灏變?瀛??ㄧ嚎绋?瀹??ㄧ????棰???杩?绉??規???瑕?灏????㈢??Concurrent Test绫葷??绫誨ご瀹?涔??存?逛負锛?

Public class Concurrent Test extends HttpServlet implements SingleThreadModel {

????????

}?

????2????姝ュ?瑰?變韓?版??????浣?

????浣跨??ynchronized ?抽??瀛??戒?璇?涓?娆″????涓?涓?绾跨???浠ヨ?塊??琚?淇??ょ???烘?碉??ㄦ??璁烘??涓???Servlet??浠ラ??杩???姝ュ????浣??ヤ?璇?绾跨???瀹??ㄣ????姝ュ????浠g??濡?涓?锛?

????????

Public class Concurrent Test extends HttpServlet { ????????

Username = request.getParameter ("username");

Synchronized (this){

Output = response.getWriter ();

Try {

Thread. Sleep (5000);

} Catch (Interrupted Exception e){}

output.println("?ㄦ?峰??:"+Username+"<BR>");

}

}

}

????3???垮??浣跨?ㄥ??渚?????

??????瀹?渚?涓???绾跨?瀹??ㄩ??棰????卞??渚???????????锛???瑕???ervlet???㈢??浠諱??規????㈤?戒?浣跨?ㄥ??渚?????锛??d?璇?ervlet灏辨??绾跨?瀹??ㄧ????

????淇?姝d??㈢??Servlet浠g??锛?灏?瀹?渚??????逛負灞??ㄥ????瀹??闆???風?????斤?浠g??濡?涓?锛?

????

Public class Concurrent Test extends HttpServlet {public void service (HttpServletRequest request, HttpServletResponse

Response) throws ServletException, IOException {

Print Writer output;

String username;

Response.setContentType ("text/html; charset=gb2312");

????

}

}

??**??瀵逛??㈢??涓?绉??規?杩?琛?娴?璇?锛???浠ヨ〃???ㄥ??浠??借?借?捐?″?虹嚎绋?瀹??ㄧ??Servlet绋?搴???浣???锛?濡???涓?涓?Servlet瀹??頒?SingleThreadModel?ュ?o?Servlet寮???灏?涓烘??涓??扮??璇鋒???寤轟?涓???????Servlet瀹?渚?锛?杩?灏?寮?璧峰ぇ????绯葷?寮?????SingleThreadModel??ervlet2.4涓?宸蹭??????′嬌??????峰????ㄧ?搴?涓?浣跨?ㄥ??姝ユ?ヤ??よ?浣跨?ㄧ???變韓???版??锛?涔?浼?浣跨郴缁????ц?藉ぇ澶т?????杩?????涓鴻???姝ョ??浠g?????ㄥ??涓??跺?誨???芥??涓?涓?绾跨??ц?瀹?锛?浣垮??跺???跺???瀹㈡?瘋?鋒???????????浣?锛???涓?寰?澶?瀹㈡?峰?浜??誨??舵???????涓轟?璇?涓誨????瀹瑰??绾跨???宸ヤ???瀛?涓????版????涓??存?э?瑕?棰?绻??闆?鋒?扮?瀛?,杩?涔?浼?澶уぇ?闆獎??绯葷????ц?姐????浠ュ?ㄥ??????寮???涓?涔?搴??垮??????灏??? Servlet 涓?????姝ヤ唬??锛???erlet涓??垮??浣跨?ㄥ??渚???????淇?璇?Servlet绾跨?瀹??ㄧ????浣抽???┿??浠?Java ??瀛?妯″??涔???浠ョ?ラ??锛??規?涓???涓存?跺???????ㄦ??涓?????绌洪?達???涓?姣?涓?绾跨??芥????宸辯???????绌洪?達???浠ュ??浠?涓?浼?褰卞??绾跨???瀹??ㄣ??

?

?村??璇????璇存??锛?

?? 1锛???????绾跨?瀹????杩???????????瀛?娈靛???變韓?版??(濡?琛ㄥ?????闆????

?????? a锛?灏? ???闆???? ???闆????澶?绾跨?骞朵??變韓灞??ㄥ????.??浠ユ??浠?瑕?灏藉???界????ervlet涓?浣跨?ㄥ??ㄥ??????

????????? 渚?濡?锛?String user = "";

????????????? user = request.getParameter("user");

????? b锛?浣跨?ㄥ??姝ュ??Synchronized锛??叉?㈠???藉?姝ヨ??ㄧ??浠g??????杩????崇??绾跨???瑕?????澶??????ㄤ嬌?ㄥ???垮?????跺??瑕?灏藉???界??缂╁???姝ヤ唬???????達?涓?瑕??存?ュ??evice?規?????搴??規?涓?浣跨?ㄥ??姝ワ?杩??蜂?涓ラ??褰卞???ц?姐??

?????? 2,灞??х??绾跨?瀹????ServletContext锛?HttpSession锛?ServletRequest瀵矽薄涓?灞??с??

???????? ServletContext锛?锛?绾跨???涓?瀹??ㄧ??锛?

???????? ServletContext????浠ュ?绾跨????惰????灞??х??锛?绾跨???涓?瀹??ㄧ????瑕?瀵瑰??х??璇誨??杩?琛???姝ュ???????杩?琛?娣卞害Clone()????浠ュ??ervlet涓?涓???涓?灏藉???藉???淇?瀛?浼?琚?淇??癸???锛????版??锛???浠ラ?????朵??瑰??ㄥ?涓?Servlet涓??變韓锛?姣??規??浠???浠ヤ嬌?ㄥ??渚?妯″??ュ????變韓?版????

???????? HttpSession锛?锛?绾跨???涓?瀹??ㄧ??锛?

???????? HttpSession瀵矽薄?ㄧ?ㄦ?蜂?璇????村????????藉?ㄥ???灞?浜???涓?涓?Session??璇鋒???绾跨?涓?琚?璁塊??锛???姝?ession瀵矽薄??灞??ц?塊????璁轟???绾跨?瀹??ㄧ????

???????? 褰??ㄦ?鋒??寮?澶?涓???灞?浜?涓?涓?杩?绋???娴?瑙??ㄧ???o??ㄨ?浜?绐??g??璁塊??灞?浜???涓?涓?Session锛?浼??虹?闆?娆¤?鋒?锛???瑕?澶?涓?宸ヤ?绾跨??ュ???璇鋒?锛????介???????跺?绾跨?璇誨??灞??с??杩??舵??浠???瑕?瀵瑰??х??璇誨??杩?琛???姝ュ???锛?浣跨?ㄥ??姝ュ??Synchronized??浣跨?ㄨ?????ㄦ?ヨВ?熾??

??????? ServletRequest锛?锛?绾跨???瀹??ㄧ??锛?

??????? 瀵逛?姣?涓?涓?璇鋒?锛??變?涓?宸ヤ?绾跨??ユ?ц?锛??戒???寤烘??涓?涓??扮??ServletRequest瀵矽薄锛???浠?ervletRequest瀵矽薄???藉?ㄤ?涓?绾跨?涓?琚?璁塊????ServletRequest??绾跨?瀹??ㄧ????娉ㄦ??锛?ServletRequest瀵矽薄??ervice?規??????村??????????锛?涓?瑕?璇??懼??ervice?規?缁?????浠??朵?瀛?璇鋒?瀵矽薄??寮??ㄣ??

3锛?浣跨?ㄥ??姝ョ??????绫夥?

? 浣跨??ector浠f??rrayList锛?浣跨??ashtable浠f??ashMap??

4锛?涓?瑕???ervlet涓???寤鴻??宸辯??绾跨??ュ??????涓????姐??

? Servlet??韬?灏辨??澶?绾跨???锛???ervlet涓?????寤虹嚎绋?锛?灏?瀵艱?存?ц????靛?????锛??虹?闆?绾跨?瀹??ㄩ??棰???

5锛??ㄥ?涓?servlet涓?瀵瑰??ㄥ?矽薄(姣??規??浠?杩?琛?淇??規??浣?涓?瀹?瑕?????锛????頒??ョ??璁塊????

??

6锛?javax.servlet.SingleThreadModel?ュ?f??涓?涓???璇??ュ?o?濡???涓?涓?Servlet瀹??頒?杩?涓??ュ?o???ervlet瀹瑰?ㄥ?淇?璇??ㄤ?涓??跺?諱???涓?涓?绾跨???浠ュ?ㄧ?瀹???servlet瀹?渚???service?規?涓??ц???灏??朵?????璇鋒?杩?琛???????

PS锛?

Servlet骞堕????????渚???. 褰?container寮?濮?????????瀹㈡?風?????鴻?鋒????℃??Container浼????у?瑰?ㄧ????缃?璐?璐e??杞藉??瀹?渚???涓?涓?Servlet锛?涔???浠ラ??缃?涓哄?涓?锛?涓?杩?涓???涓?杩?涔?骞詫?.涓?杩?涓????ヨ?翠?涓?servlet??浼???涓?涓?瀹?渚???

?

?

1) Struts2??Action??????锛?????瀹?渚???锛?浼?瀵規??涓?涓?璇鋒?,浜х??涓?涓?Action??瀹?渚??ュ?????

2) Struts1??Action,Spring??Ioc瀹瑰?ㄧ?$????bean 榛?璁ゆ????瀹?渚???.

Struts1 Action????瀹?渚???锛?spring mvc??controller涔???濡?姝ゃ????姝ゅ????惰?姹?蹇?椤繪??绾跨?瀹??ㄧ??锛???涓轟???Action??涓?涓?瀹?渚??ュ?????????璇鋒?????渚?绛??ラ???朵?Struts1 Action?戒???浜?锛?骞朵?瑕??ㄥ????剁?瑰??灏?蹇???Action璧?婧?蹇?椤繪??绾跨?瀹??ㄧ??????姝ョ????

?

Spring??Ioc瀹瑰?ㄧ?$????bean 榛?璁ゆ????瀹?渚?????

Struts2 Action瀵矽薄涓烘??涓?涓?璇鋒?浜х??涓?涓?瀹?渚?锛???姝ゆ病??绾跨?瀹??ㄩ??棰???锛?瀹???涓?锛?servlet瀹瑰?ㄧ?姣?涓?璇鋒?浜х??璁稿???涓㈠???瀵矽薄锛?骞朵?涓?浼?瀵艱?存?ц?藉?????懼???堕??棰?锛???

褰?Spring绠$??Struts2??Action?訛?bean榛?璁ゆ????瀹?渚???锛???浠ラ??杩???缃????闆??惰?劇疆涓哄??????(scope="prototype 锛?

?

繼續閱讀