Java Web å¼åï¼è¯·è¿ç¦»åç§æ¡æ¶ï¼ä» Servlet å¼å
ä¸å¤§æ³¢èªå¸¦å¤æç大æ¡å¸å·²ç»å°æ¥ï¼èµ°ç
OSCHINA 软件åºæä¸ä¸ªåç±»ââWebæ¡æ¶ï¼è¯¥åç±»ä¸å
å«å¤ç§ç¼ç¨è¯è¨çå°è¿500个项ç®ã
Webæ¡æ¶æ¯å¼åè
å¨ä½¿ç¨æç§è¯è¨ç¼åWebåºç¨æå¡ç«¯æ¶å
³äºæ¶æçæä½³å®è·µãå¾å¤Webæ¡æ¶æ¯ä»å®é
çWeb项ç®æ½ååºæ¥çï¼ä»
åWebç请æ±åååºå¤çæå
³ï¼å½¢æä¸ä¸ªåºç¡ï¼å¨å¼åå«çåºç¨é¡¹ç®çæ¶ååå¯ä»¥ä»è¿ä¸ªå¥ç¦»åºæ¥çåºç¡åèµ·ï¼è®©å¼åè
æ´å
³æ³¨æ´å
·ä½çä¸å¡é®é¢ï¼èä¸æ¯Webç请æ±åååºçæ§å¶ã
æ¡æ¶å¾å¤ï¼ä½å¥è·¯åºæ¬ç±»ä¼¼ï¼å¸®ä½ éèå¾å¤å
³äº HTTP åè®®ç»èå
容ï¼ä¸æ³¨åè½å¼åã
ä½å¯¹ä¸ä¸ªåå¦è
æ¥è¯´ï¼è¿æ©çæ¥è§¦æ¡æ¶å¾å¾æ¯äºåååï¼åæ ·ä¸ä¸ªé®é¢ï¼æ¢ä¸ç§æ¡æ¶ä½ å¯è½éè¦ä»å¤´å¼å§ç 究ã
ä¸é¢æ¯é对åå¦ Java å¼å Web è¿ç¨ä¸äºä¸ªäººè§è§£åæè·¯ï¼é«æå¯ç¥è¿ã
1. åºæ¬è¦æ±ï¼Java ç¼ç¨åºç¡
æè¯å¥½ç Java è¯è¨ç¼ç¨åºç¡ï¼è¿æ¯å¿
é¡»çï¼å¨è®¨è®º Web å¼åææ¯æ¶æäºä¸ä¸ª Java ç¼ç¨åºç¡çé®é¢ä¼è¢«éè§çã
2. ç¯å¢åå¤ (Eclipse + Tomcat)
éæ©ä¸ä¸ªä½ åç±çServlet容å¨ï¼æè
说大ä¸ç¹å°±æ¯åºç¨æå¡å¨ï¼æ¨è Tomcat ãResin æè
Jetty è¿äºè½»é级ç产åãè¿ä¸ä¸ªäº§åä¸è½½ zip å
解ååå°±å¯ä»¥ç¨äºãå¦æä½ ä¸çæ Tomcat çè¯è¯·ä¸è¦ä½¿ç¨ exe çæ¬ç Tomcatï¼é£ä¼å¾å¢å¾å¤ç¦æ¼ãä¹ä¸å»ºè®®å¨ Eclipse çä¸äºå¼åç¯å¢ä¸éæ Tomcat çåæ³ï¼ä¹ä¼å¾å¢ç¦æ¼ã
æåºç¨æå¡å¨å¯å¨èµ·æ¥å¹¶è½è®¿é®å°å
¶é»è®¤ç页é¢ä¸ºåã
å
³äºå¼åå·¥å
·
ä¸æ¨èä½¿ç¨ MyEclipse å Eclipse ç JEE çæ¬ï¼å¾å¢ç¦æ¼ãè¿è¡ç¼æ
¢èä¸è¿è®©ä½ æ æ³äºè§£ Web 项ç®çç»æãæ®éç Eclipse æè
ä½ å欢çå¼åå·¥å
·å°±è¶³å¤äºï¼è½æ¯ææ®é Java 项ç®å¼åå³å¯ã
为äºæ¹ä¾¿ï¼æåäºä¸ä¸ªæåºæ¬çJava é¡¹ç® ââ ServletDemo.zip ï¼ä½ å¯å°å®å¯¼å
¥å° Eclipse éå°±æ¯ä¸ä¸ªå®æ´çãæç®åç Web 项ç®ã
ç¶åå°ä¸é¢ XML å
容æ¿æ¢ Tomcat ä¸ç conf/server.xml æ件
å
¶ä¸ D:\WORKDIR\ServletDemo æ¿æ¢ä¸ºä½ 导å
¥ç项ç®è·¯å¾ï¼å次å¯å¨ Tomcat åå¨æµè§å¨æå¼
http://localhost:8080/hello 便å¯çå° Hello World çè¾åºä¿¡æ¯ã
3. äºè§£ Servlet å Filter
好äºï¼æå·²ç»æç¯å¢æèµ·æ¥äºï¼æ¥ä¸æ¥è¯¥å¹²åå¢ï¼
åé¢çæ¥éª¤ä¸ºçæ¯æ建ä¸ä¸ªæµè¯çç¯å¢ï¼ç¶åè®©ä½ äºè§£ä¸ä¸ªæåºæ¬ç Java Web 项ç®çç»æã
ä¸ä¸ªæåºæ¬ç Java Web 项ç®æéç jar å
åªéè¦ä¸ä¸ª servlet-api.jar ï¼è¿ä¸ª jar å
ä¸ç类大é¨åé½æ¯æ¥å£ï¼è¿æä¸äºå·¥å
·ç±»ï¼å
±æ 2 个å
ï¼åå«æ¯ javax.servlet å javax.servlet.httpãææè¿ä¸ªjarå
æ¾å°äº webapp ç®å½å¤çä¸ä¸ªç¬ç« packages æ件夹éï¼è¿æ¯å 为ææç Servlet 容å¨é½å¸¦æè¿ä¸ªå
ï¼ä½ æ éåæ¾å°Web项ç®éï¼æ们æ¾å°è¿éåªä¸è¿æ¯ç¼è¯çéè¦ï¼è¿è¡æ¯ä¸éè¦çãå¦æä½ ç¡¬æ¯æ servlet-api.jar æ¾å° webapp/WEB-INF/lib ç®å½ä¸ï¼é£ä¹ Tomcat å¯å¨æ¶è¿ä¼æ¥ä¸ä¸ªè¦åä¿¡æ¯ã
Java Web 项ç®è¿éè¦ä¸ä¸ªé常éè¦çé
ç½®æ件 web.xml ï¼å¨è¿ä¸ªé¡¹ç®ä¸å·²ç»è¢«ææå°åäºï¼åªä¿çæç¨çä¿¡æ¯ï¼
追é®æ»æ»æ»æ»æ»ï¼å«ä»¥ä¸ºä½ 头è¡æ¯é«çº§çï¼æå°±ä¸æ¢åä½ äºã
追çä½ æ³ç¥éä»ä¹ï¼å¥ä»¬ï¼è¯´è¯ææç¹ã
ä¸çä½ å°±æ²¡æå
»ï¼è¦å¦ä¸è¥¿å¿
须谦èã
追é®ææä¹å°±ä¸ææäºï¼
æ»ä¸ææï¼
é£è¯·ä½ 以ä¸ç§å润çæ¹å¼ï¼æ»åºå»ã
çï¼è¿æ¯ç¨å°äºæ»ã
追çæ¯çåè´§
è¿æ¥ç©ç¥é
追é®æ¯ï¼ä½ ä¼ç©ã
å¤å¶ç²è´´çå¤å¶ä¸æ®µå¥½åå¾ä¸ä¸çåçï¼ç¶ååçç¾åº¦éä½ ä¸ºæ¨èçæ¡ï¼ç¶ååå°é«çº§äºã
ä½ åçå©ç¨è§åæ¯ä½ çèªç±ï¼æå«å¤å¶ç²è´´çæ»ä¹æ¯æçæå©ã
ä½ æ¯ä¸åªä¼ç©çææå
»çå¤å¶ç²è´´çã