Java Servlet(浜?锛歴ervlet閰嶇疆鍙婄敓鍛藉懆鏈熺浉鍏?jdk7+tomcat7+eclipse)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Servlet(浜?锛歴ervlet閰嶇疆鍙婄敓鍛藉懆鏈熺浉鍏?jdk7+tomcat7+eclipse)相关的知识,希望对你有一定的参考价值。
鏍囩锛?/p>
璇ョ瘒鏂囩珷璁板綍浜哠ervlet閰嶇疆鐩稿叧鐢ㄦ硶鍙奡ervlet鍦⊿ervlet瀹瑰櫒涓敓鍛藉懆鏈熸柟娉曘€?/p>
- Tomcat鏄竴涓猄ervlet瀹瑰櫒锛?/strong>
1銆丼ervlet瀹瑰櫒绠$悊浜哠ervlet鐨勬暣涓敓鍛藉懆鏈燂紝骞惰皟鐢╯ervlet鐨勭敓鍛藉懆鏈熺殑鏂规硶銆?/p>
2銆丼ervlet瀹瑰櫒涓嶅彧鏄疭ervlet鐨勭敓鍛藉懆鏈燂紝鑰屼笖杩樻槸Jsp,Filter,Listener,Tag绛夌敓鍛藉懆鏈熺鐞嗗鍣ㄣ€?/p>
- Servlet鐨勬敞鍐屼笌杩愯
1銆丼ervlet绋嬪簭蹇呴』閫氳繃Servlet瀹瑰櫒鏉ュ惎鍔ㄨ繍琛岋紝骞朵笖瀛樺偍鐩綍鏈夌壒娈婅姹傦紝閫氬父servlet缂栬瘧濂界殑.class鏂囦欢瑕佸瓨鍌ㄥ湪<WEB搴旂敤绋嬪簭鐩綍>\WEB-INF\classes\鐩綍涓€?/p>
2銆丼ervlet绋嬪簭蹇呴』鍦╓EB搴旂敤绋嬪簭鐨剋eb.xml鏂囦欢涓繘琛屾敞鍐屽拰鏄犲皠鍏惰闂矾寰勶紝鎵嶅彲浠ヨServlet寮曟搸鍔犺浇琚鐣岃闂€?/p>
3銆佷竴涓?lt;servlet>鍏冪礌鐢ㄦ埛娉ㄥ唽涓€涓猄ervlet锛屽畠鍖呭惈鏈変袱涓富瑕佺殑瀛愬厓绱狅細<servlet-name>鍜?lt;servlet-class>,鍒嗗埆鐢ㄦ埛璁剧疆Servlet鐨勬敞鍐屽悕绉板拰Servlet鐨勫畬鏁寸被鍚嶃€?/p>
4銆佷竴涓?lt;servlet-mapping>鍏冪礌鐢ㄤ簬鏄犲皠涓€涓凡娉ㄥ唽鐨凷ervlet鐨勪竴涓澶栬闂矾寰勶紝瀹冨寘鍚湁涓や釜瀛愬厓绱狅細<servlet-name>鍜?lt;url-pattern>锛屽垎鍒敤鎴锋寚瀹歋ervlet鐨勬敞鍐屽悕绉板拰Servlet鐨勫澶栬闂矾寰勩€?/p>
- Servlet瀹瑰櫒鐩稿簲瀹㈡埛璇锋眰鐨勮繃绋嬶細
1銆乻ervlet寮曟搸妫€鏌ユ槸鍚﹀凡缁忚杞借鍒涘缓璇ervlet鐨勫疄渚嬪璞°€傚鏋滃凡缁忓垱寤猴紝鍒欑洿鎺ユ墽琛岀4姝ワ紱鍚﹀垯锛屾墽琛岀2姝ャ€?/p>
2銆佽杞藉苟鍒涘缓璇ervlet鐨勪竴涓疄渚嬪璞★細璋冪敤璇ervlet鐨勬瀯閫犲嚱鏁般€?/p>
3銆佽皟鐢╯ervlet瀹炰緥瀵硅薄鐨刬nit鏂规硶銆?/p>
4銆佸垱寤轰竴涓敤浜庡皝瑁呰姹傜殑ServletRequest瀵硅薄鍜屼竴涓唬琛ㄧ浉搴旀秷鎭殑ServletResponse瀵硅薄锛岀劧鍚庤皟鐢⊿ervlet鐨剆ervice鏂规硶骞跺皢璇锋眰鍜岀浉搴斿璞′綔涓哄弬鏁颁紶閫掕繘鍘汇€?/p>
5銆亀eb搴旂敤绋嬪簭琚仠姝㈡垨鑰呴噸鍚箣鍓嶏紝servlet寮曟搸灏嗗嵏杞絪ervlet锛屽苟鍦ㄥ嵏杞戒箣鍓嶈皟鐢╯ervlet鐨刣estory鏂规硶銆?/p>
- Servlet鐢熷懡鍛ㄦ湡鐨勬柟娉曪紙浠ヤ笅鏂规硶閮芥槸鏈塖ervlet瀹瑰櫒璐熻矗璋冪敤锛夛細
1銆侀鍏堟槸鍒涘缓瀹炰緥鏃惰皟鐢ㄦ瀯閫犲櫒锛氬彧琚皟鐢ㄤ竴娆★紝鍙湁绗竴娆¤姹係ervlet鏃讹紝鍒涘缓Servlet鐨勫疄渚嬶紝璋冪敤鏋勯€犲櫒銆?br />2銆乮nit鏂规硶锛氬彧琚皟鐢ㄤ竴娆★紝鍦ㄥ垱寤哄ソServlet瀹炰緥鍚庣珛鍗宠皟鐢╥nit鍑芥暟锛岀敤鎴峰垵濮嬪寲褰撳墠Servlet瀹炰緥
3銆乻ervice鏂规硶:琚娆¤皟鐢紝姣忔璇锋眰閮戒細琚皟鐢紝鐢ㄤ簬鐩稿簲璇锋眰銆?/p>
4銆乨estory鏂规硶锛氬彧琚皟鐢ㄤ竴娆★紝鍦ㄥ綋鍓峴ervlet鎵€鍦ㄧ殑web搴旂敤琚嵏杞藉墠璋冪敤锛岀敤浜庨噴鏀緎ervlet鎵€鍗犵敤鐨勮祫婧愩€?/p>
- Servlet閰嶇疆闇€瑕佹敞鎰忎簨椤癸細
servlet鑺傜偣涓嬬殑load-on-startup閰嶇疆锛?/p>
1銆佸彲浠ユ寚瀹歴ervlet琚垱寤虹殑鏃舵満锛?/p>
2銆侀粯璁よ鍙傛暟涓鸿礋鏁帮紝鍦ㄥ姞杞絯eb搴旂敤鏃讹紝涓嶅姞杞絪ervlet瀹炰緥锛岃€屽湪绗竴娆¤姹傛椂琚垱寤猴紱
3銆佽嫢涓?鎴栬€呮鏁帮紝鍒欏湪褰撳墠WEB搴旂敤琚玸ervlet鍔犺浇鏃跺垱寤哄瀹炰緥锛屼笖鍊艰秺灏忚秺鏃╄鍒涘缓銆?/p>
閰嶇疆绀轰緥锛?/p>
1 <!-- 閰嶇疆鍜屾槧灏?servlet --> 2 <servlet> 3 <!-- Servlet娉ㄥ唽鐨勫悕瀛?--> 4 <servlet-name>helloServlet</servlet-name> 5 <!-- Servlet鍏ㄧ被鍚?--> 6 <servlet-class>com.dx.hello.HelloServlet</servlet-class> 7 <load-on-startup>1</load-on-startup> 8 </servlet> 9 <servlet-mapping> 10 <!-- 瀵瑰簲servlet鑺傜偣涓嬬殑servlet-name鐨勬敞鍐屽悕瀛椾竴鑷?--> 11 <servlet-name>helloServlet</servlet-name> 12 <!-- 鏄犲皠鍏蜂綋鐨勮闂矾寰勶紝鍏朵腑/浠h〃褰撳墠web鐨勬牴鐩綍 --> 13 <url-pattern>/hello</url-pattern> 14 </servlet-mapping>
- Servlet鏄犲皠鐨勭粏鑺傦細
1銆佸悓涓€涓猄ervlet鍙互琚槧灏勫埌澶氫釜URL涓婏紝鍗冲涓?lt;servlet-mapping>鍏冪礌鐨?lt;servlet-name>瀛愬厓绱犵殑璁剧疆鍊煎彲浠ユ槸鍚屼竴涓猄ervlet鐨勬敞鍐屽悕銆?/p>
姣斿锛?/p>
1 <!-- 閰嶇疆鍜屾槧灏?servlet --> 2 <servlet> 3 <!-- Servlet娉ㄥ唽鐨勫悕瀛?--> 4 <servlet-name>helloServlet</servlet-name> 5 <!-- Servlet鍏ㄧ被鍚?--> 6 <servlet-class>com.dx.hello.HelloServlet</servlet-class> 7 <load-on-startup>1</load-on-startup> 8 </servlet> 9 <servlet-mapping> 10 <!-- 瀵瑰簲servlet鑺傜偣涓嬬殑servlet-name鐨勬敞鍐屽悕瀛椾竴鑷?--> 11 <servlet-name>helloServlet</servlet-name> 12 <!-- 鏄犲皠鍏蜂綋鐨勮闂矾寰勶紝鍏朵腑/浠h〃褰撳墠web鐨勬牴鐩綍 --> 13 <url-pattern>/hello</url-pattern> 14 </servlet-mapping> 15 <servlet-mapping> 16 <!-- 瀵瑰簲servlet鑺傜偣涓嬬殑servlet-name鐨勬敞鍐屽悕瀛椾竴鑷?--> 17 <servlet-name>helloServlet</servlet-name> 18 <!-- 鏄犲皠鍏蜂綋鐨勮闂矾寰勶紝鍏朵腑/浠h〃褰撳墠web鐨勬牴鐩綍 --> 19 <url-pattern>/hello2</url-pattern> 20 </servlet-mapping>
杩欐牱瀛愬氨鍙互閫氳繃http://localhost:8080/MyServlet001/hello鍜宧ttp://localhost:8080/MyServlet001/hello2璁块棶鍚屼竴涓猻ervlet銆?/p>
2銆佸湪Servlet鏄犲皠鍒癠RL涓篃鍙互浣跨敤*閫氶厤绗︼紝浣嗘槸鍙兘鏈変袱绉嶅浐瀹氱殑鏍煎紡锛氫竴绉嶆槸“*.鎵╁睍鍚?rdquo;锛屽彟澶栦竴绉嶆槸浠ユ鏂滄潬“/”寮€澶村苟浠?ldquo;/”缁撳熬銆?/p>
姣斿锛?/p>
1 <servlet-mapping> 2 <!-- 瀵瑰簲servlet鑺傜偣涓嬬殑servlet-name鐨勬敞鍐屽悕瀛椾竴鑷?--> 3 <servlet-name>helloServlet2</servlet-name> 4 <!-- 鏄犲皠鍏蜂綋鐨勮闂矾寰勶紝鍏朵腑/浠h〃褰撳墠web鐨勬牴鐩綍 --> 5 <url-pattern>*.do</url-pattern> 6 <!-- <url-pattern>*.html</url-pattern> --> 7 </servlet-mapping> 8 <servlet-mapping> 9 <!-- 瀵瑰簲servlet鑺傜偣涓嬬殑servlet-name鐨勬敞鍐屽悕瀛椾竴鑷?--> 10 <servlet-name>helloServlet1</servlet-name> 11 <!-- 鏄犲皠鍏蜂綋鐨勮闂矾寰勶紝鍏朵腑/浠h〃褰撳墠web鐨勬牴鐩綍 --> 12 <url-pattern>/hello2/*</url-pattern> 13 <!-- <url-pattern>/*</url-pattern> --> 14 </servlet-mapping>
浣嗛渶瑕佹敞鎰忎簨椤癸細
1銆?.html杩欐牱鍐欐槸娌℃湁闂鐨勶紝濡傛灉閰嶇疆涓?*.html鎴栬€?*.action銆?*.do绛夋牸寮忕殑锛屾槸閿欒鐨勯厤缃紝鍔犺浇鏃朵細璺戝嚭寮傚父淇℃伅銆?/p>
2銆佽嫢鏋滈厤缃负*娌℃湁鍚庣紑鍚嶏紝鍒欏繀椤绘祬琛ㄥ甫/锛屾牸寮忎负锛?*鎴栬€?a/*绛夌瓑銆?/p>
以上是关于Java Servlet(浜?锛歴ervlet閰嶇疆鍙婄敓鍛藉懆鏈熺浉鍏?jdk7+tomcat7+eclipse)的主要内容,如果未能解决你的问题,请参考以下文章