- 浏览: 51615 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
yekai584341028:
楼主太搞笑了,还和服务器扯皮呵呵。
捕获异常要捕获根源异常。
j2me 联网技术分析总结 -
wcjhaoa:
那个一次排序时的交换情况我想半天没明白你为什么要这样写的... ...
快速排序java实现 -
lei511:
android 连接 有问题
[2010-05-04 10 ...
eclipse真机调试J2me程序 -
xmmarmy007:
java.io.IOException: Couldn't r ...
j2me 联网技术分析总结
servletè¿æ»¤å¨
Servletè¿æ»¤å¨æ¯Servletçä¸ç§ç¹æ®ç¨æ³ï¼ä¸»è¦ç¨æ¥å®æä¸äºéç¨çæä½ãæ¯å¦ç¼ç çè¿æ»¤ï¼å¤æç¨æ·çç»éç¶æççãServletè¿æ»¤å¨çéç¨åºåï¼
Aï¼è®¤è¯è¿æ»¤
Bï¼ç»å½åå®¡æ ¸è¿æ»¤
Cï¼å¾å转æ¢è¿æ»¤
Dï¼æ°æ®å缩è¿æ»¤
Eï¼å å¯è¿æ»¤
Fï¼ä»¤çè¿æ»¤
Gï¼èµæºè®¿é®è§¦åäºä»¶è¿æ»¤
Servletè¿æ»¤å¨æ¥å£çææï¼
ææçServletè¿æ»¤å¨ç±»é½å¿ é¡»å®ç°javax.servlet.Filteræ¥å£ãè¿ä¸ªæ¥å£å«æ3个è¿æ»¤å¨ç±»å¿ é¡»å®ç°çæ¹æ³ï¼
æ¹æ³ 说æ
init(FilterConfig cfg) è¿æ¯Servletè¿æ»¤å¨çåå§åæ¹æ³ï¼æ§è´¨çåä¸servletçinitæ¹æ³ã
doFilter(ServletRequest,ServletResponse,FilterChain) å®æå®é çè¿æ»¤æä½ï¼å½è¯·æ±è®¿é®è¿æ»¤å¨å ³èçURLæ¶ï¼Servlet容å¨å°å è°ç¨è¿æ»¤å¨çdoFilteræ¹æ³ãFilterChainåæ°ç¨äºè®¿é®åç»è¿æ»¤å¨
destroy() Servlet容å¨å¨éæ¯è¿æ»¤å¨å®ä¾åè°ç¨è¯¥æ¹æ³ï¼è¿ä¸ªæ¹æ³ä¸å¯ä»¥éæ¾Servletè¿æ»¤å¨å ç¨çèµæºãæ§è´¨çåä¸servletçdestory()æ¹æ³ã
Servletè¿æ»¤å¨çå建æ¥éª¤ï¼
Aï¼å®ç°javax.servlet.Filteræ¥å£çservletç±»
Bï¼å®ç°initæ¹æ³ï¼è¯»åè¿æ»¤å¨çåå§åå½æ°
Cï¼å®ç°doFilteræ¹æ³ï¼å®æ对请æ±æè¿æ»¤çååº
Dï¼è°ç¨FilterChainæ¥å£å¯¹è±¡çdoFilteræ¹æ³ï¼ååç»çè¿æ»¤å¨ä¼ é请æ±æååº
F.å¨web.xmlä¸é ç½®Filter
2.使ç¨è¿æ»¤å¨å¤çä¸æé®é¢
å½ç¨ç¨æ·ç»é页é¢è¾å ¥å¸å·æ¶ï¼å¦æè¾å ¥æ¯ä¸æï¼åå°servletå次è¾åºè¿ä¸ªå 容æ¶ï¼å¯è½å°±ä¼æ¯ä¹±ç ï¼è¿æ¯å 为serlvetä¸é»è®¤æ¯ä»¥ISO-8859-1æ ¼å¼ç¼ç çï¼å¦æåå°æå¤ä¸ªServlet,å¤ä¸ªåæ°ï¼è¿æ ·å°±ä¸åéï¼è¿ä¸ªé®é¢ï¼æ们å¯ä»¥éè¿ä¸ä¸ªè¿æ»¤å¨ç»ä¸è§£å³ï¼ä½¿åå°çè¾åºè¾åºé½æ¯æä¸æï¼å°ISO-8859-1转ç 为GBKçé£æ®µä»£ç ï¼
3.使ç¨è¿æ»¤å¨è®¤è¯ç¨æ·ï¼
æ¯ä¸ªè¿æ»¤å¨ä¹å¯ä»¥é ç½®åå§ååæ°ï¼å¯ä»¥å°ä¸éè¦è¿æ»¤çå°åé ç½®å°è¿ä¸ªFilterçé ç½®åæ°ä¸ï¼è¿æ»¤æ¶ï¼å¦æ请æ±å°åå¨é ç½®åæ°ä¸ï¼åæ¾è¡ï¼è¿æ · å°±é¿å äºå¨ç¨åºä¸ç¡¬ç¼ç ãæ¯ä¸ªFilterä¸åå§åæ¶ï¼é½å¯ä»¥å¾å°é 置对象ï¼å¨Filterä¸é ç½®äºä¸ªä¸éè¦è¿æ»¤çå°åï¼ä¸ä¸ªæ¯ç»é页é¢ï¼ä¸ä¸ªæ¯æ§è¡ç»é 认è¯çservletï¼
4.Servletçå¬å¨
类似ä¸Swingçé¢åºç¨å¼åï¼Servletä¹å¯ä»¥å建çå¬å¨ï¼ä»¥å¯¹Servlet容å¨ï¼æServletä¸ä»¥è±¡çäºä»¶ååºååºãServletçå¬å¨ä¸»è¦æ以ä¸å ç§ï¼
ServletRequestListener ,ServletRequestAttributeListenerï¼
HttpSessionActivationListener ,HttpSessionBindingListener ,
HttpSessionAttributeListener,HttpSessionListener,
ServletContextListenerççã
è¿äºçå¬å¨ä¸»è¦ç¨æ¥çå¬session,request,applicationè¿ä¸ä¸ªå¯¹è±¡éååæ°æ®çååã
----------------------------------------------------------------------------------------------------------------
servlet APIä¸æéè¦çä¸ä¸ªåè½å°±æ¯è½å¤ä¸ºservletåJSP页é¢å®ä¹è¿æ»¤å¨ãè¿æ»¤å¨æä¾äºæäºæ©ææå¡å¨ææ¯æçéæ å“servleté¾æ¥”çä¸ç§åè½å¼ºå¤§ä¸æ åçæ¿ä»£åã
è¿æ»¤å¨æ¯ä¸ä¸ªç¨åºï¼å®å
äºä¸ä¹ç¸å
³çservletæJSP页é¢è¿è¡å¨æå¡å¨ä¸ãè¿æ»¤å¨å¯éå å°ä¸ä¸ªæå¤ä¸ªservletæJSP页é¢ä¸ï¼å¹¶ä¸å¯ä»¥æ£æ¥è¿å
¥è¿äºèµæºç请æ±ä¿¡æ¯ãå¨è¿ä¹åï¼è¿æ»¤å¨å¯ä»¥ä½å¦ä¸çéæ©ï¼
1. 以常è§çæ¹å¼è°ç¨èµæºï¼å³ï¼è°ç¨servletæJSP页é¢ï¼ã
2.å©ç¨ä¿®æ¹è¿ç请æ±ä¿¡æ¯è°ç¨èµæºã
3. è°ç¨èµæºï¼ä½å¨åéååºå°å®¢æ·æºå对å
¶è¿è¡ä¿®æ¹
4. é»æ¢è¯¥èµæºè°ç¨ï¼ä»£ä¹ä»¥è½¬å°å
¶ä»çèµæºï¼è¿åä¸ä¸ªç¹å®çç¶æ代ç æçææ¿æ¢è¾åºã
è¿æ»¤å¨æä¾äºå 个éè¦å¥½å¤ ï¼
é¦å
ï¼å®ä»¥ä¸ç§æ¨¡ååçæå¯éç¨çæ¹å¼å°è£
å
Œ
±çè¡ä¸ºãä½ æ30个ä¸åçserlvetæJSP页é¢ï¼éè¦å缩å®ä»¬çå
容以åå°ä¸è½½æ¶é´åï¼æ²¡é®é¢ï¼æé ä¸ä¸ªå缩è¿æ»¤å¨ï¼ç¶åå°å®åºç¨å°30个èµæºä¸å³å¯ã
å
¶æ¬¡ï¼å©ç¨å®è½å¤å°é«çº§è®¿é®å³çä¸è¡¨ç°ä»£ç ç¸å离ãè¿å¯¹äºJSPç¹å«æä»·å¼ï¼å
¶ä¸ä¸è¬å¸æå°å ä¹æ´ä¸ªé¡µé¢éä¸å¨è¡¨ç°ä¸ï¼èä¸æ¯éä¸å¨ä¸å¡é»è¾ä¸ãä¾å¦ï¼å¸
æé»å¡æ¥èªæäºç«ç¹ç访é®èä¸ç¨ä¿®æ¹å页é¢ï¼è¿äºé¡µé¢åå°è®¿é®éå¶ï¼åï¼æ²¡é®é¢ï¼å»ºç«ä¸ä¸ªè®¿é®éå¶è¿æ»¤å¨å¹¶æå®åºç¨å°æ³è¦éå¶è®¿é®ç页é¢ä¸å³å¯ã
æåï¼è¿æ»¤å¨ä½¿ä½ è½å¤å¯¹è®¸å¤ä¸åçèµæºè¿è¡æ¹éæ§çæ´æ¹ãä½ æ许å¤ç°åèµæºï¼è¿äºèµæºé¤äºå
¬å¸åè¦æ´æ¹å¤å
¶ä»çä¿æä¸åï¼è½åå°ä¹ï¼æ²¡é®é¢ï¼æé ä¸ä¸ªä¸²æ¿æ¢è¿æ»¤å¨ï¼åªè¦åé就使ç¨å®ã
ä½è¦æ³¨æï¼è¿æ»¤å¨åªå¨ä¸servletè§è2.3çå
¼å®¹çæå¡å¨ä¸æä½ç¨ãå¦æä½ çWebåºç¨éè¦æ¯ææ§çæå¡å¨ï¼å°±ä¸è½ä½¿ç¨è¿æ»¤å¨ã
1ï¼ å»ºç«åºæ¬è¿æ»¤å¨
建ç«ä¸ä¸ªè¿æ»¤å¨æ¶åä¸åäºä¸ªæ¥éª¤ï¼
1ï¼å»ºç«ä¸ä¸ªå®ç°Filteræ¥å£çç±»ãè¿ä¸ªç±»éè¦ä¸ä¸ªæ¹æ³ï¼åå«æ¯ï¼doFilterãinitådestroyã
doFilteræ¹æ³å
å«ä¸»è¦çè¿æ»¤ä»£ç ï¼è§ç¬¬2æ¥ï¼ï¼initæ¹æ³å»ºç«è®¾ç½®æä½ï¼èdestroyæ¹æ³è¿è¡æ¸
æ¥ã
2ï¼å¨doFilteræ¹æ³ä¸æ¾å
¥è¿æ»¤è¡ä¸ºãdoFilteræ¹æ³ç第ä¸ä¸ªåæ°ä¸ºServletRequest对象ãæ¤å¯¹è±¡ç»è¿æ»¤å¨æä¾äºå¯¹è¿å
¥çä¿¡æ¯
ï¼å
æ¬è¡¨åæ°æ®ãcookieåHTTP请æ±å¤´ï¼çå®å
¨è®¿é®ã第äºä¸ªåæ°ä¸ºServletResponseï¼é常å¨ç®åçè¿æ»¤å¨ä¸å¿½ç¥æ¤åæ°ãæåä¸ä¸ªå
æ°ä¸ºFilterChainï¼å¦ä¸ä¸æ¥æè¿°ï¼æ¤åæ°ç¨æ¥è°ç¨servletæJSP页ã
3ï¼è°ç¨FilterChain对象çdoFilteræ¹æ³ãFilteræ¥å£çdoFilteræ¹æ³åä¸ä¸ªFilterChain对象ä½ä¸ºå®çä¸ä¸ªå
æ°ãå¨è°ç¨æ¤å¯¹è±¡çdoFilteræ¹æ³æ¶ï¼æ¿æ´»ä¸ä¸ä¸ªç¸å
³çè¿æ»¤å¨ãå¦æ没æå¦ä¸ä¸ªè¿æ»¤å¨ä¸servletæJSP页é¢å
³èï¼åservletæJSP
页é¢è¢«æ¿æ´»ã
4ï¼å¯¹ç¸åºçservletåJSP页é¢æ³¨åè¿æ»¤å¨ãå¨é¨ç½²æ述符æ件ï¼web.xmlï¼ä¸ä½¿ç¨filteråfilter-mappingå
ç´ ã
5ï¼ç¦ç¨æ¿æ´»å¨servletãé²æ¢ç¨æ·å©ç¨ç¼ºçservlet URLç»è¿è¿æ»¤å¨è®¾ç½®ã
1.1 建ç«ä¸ä¸ªå®ç°Filteræ¥å£çç±»
ææè¿æ»¤å¨é½å¿
é¡»å®ç°javax.servlet.Filterãè¿ä¸ªæ¥å£å
å«ä¸ä¸ªæ¹æ³ï¼åå«ä¸ºdoFilterãinitådestroyã
public void doFilter(ServletRequset request,
ServletResponse response,
FilterChain chain)
thows ServletException, IOException
æ¯å½è°ç¨ä¸ä¸ªè¿æ»¤å¨ï¼å³ï¼æ¯æ¬¡è¯·æ±ä¸æ¤è¿æ»¤å¨ç¸å
³çservletæJSP页é¢ï¼æ¶ï¼å°±æ§è¡å
¶doFilteræ¹æ³ãæ£æ¯è¿ä¸ªæ¹æ³å
å«äºå¤§é¨åè¿æ»¤é»è¾ã
第ä¸ä¸ªåæ°ä¸ºä¸ä¼ å
¥è¯·æ±æå
³çServletRequestã对äºç®åçè¿æ»¤å¨ï¼å¤§å¤æ°è¿æ»¤é»è¾æ¯åºäºè¿ä¸ªå¯¹è±¡çãå¦æå¤çHTTP请æ±ï¼å¹¶ä¸éè¦è®¿é®è¯¸
å¦getHeaderægetCookiesçå¨ServletRequestä¸æ æ³å¾å°çæ¹æ³ï¼å°±è¦ææ¤å¯¹è±¡æé æ
HttpServletRequestã
第äºä¸ªåæ°ä¸ºServletResponseãé¤äºå¨ä¸¤ä¸ªæ
å½¢ä¸è¦ä½¿ç¨å®ä»¥å¤ï¼é常忽ç¥è¿ä¸ªåæ°ãé¦å
ï¼å¦æå¸æå®å
¨é»å¡å¯¹ç¸å
³servletæJSP页
é¢ç访é®ãå¯è°ç¨response.getWriter并ç´æ¥åéä¸ä¸ªååºå°å®¢æ·æºãå
¶æ¬¡ï¼å¦æå¸æä¿®æ¹ç¸å
³çservletæJSP页é¢çè¾åºï¼å¯æå
åºå
å«å¨ä¸ä¸ªæ¶éææåéå°å®çè¾åºç对象ä¸ãç¶åï¼å¨è°ç¨serlvetæJSP页é¢åï¼è¿æ»¤å¨å¯æ£æ¥è¾åºï¼å¦æåé就修æ¹å®ï¼ä¹ååéå°å®¢æ·æºã
DoFilterçæåä¸ä¸ªåæ°ä¸ºFilterChain对象ã对æ¤å¯¹è±¡è°ç¨doFilter以æ¿æ´»ä¸servletæJSP页é¢ç¸å
³çä¸ä¸ä¸ªè¿æ»¤å¨ãå¦æ没æå¦ä¸ä¸ªç¸å
³çè¿æ»¤å¨ï¼å对doFilterçè°ç¨æ¿æ´»servletæJSPæ¬èº«ã
public void init(FilterConfig config) thows ServletException
initæ¹æ³åªå¨æ¤è¿æ»¤å¨ç¬¬ä¸æ¬¡åå§åæ¶æ§è¡ï¼ä¸æ¯æ¯æ¬¡è°ç¨è¿æ»¤å¨é½æ§è¡å®ã对äºç®åçè¿æ»¤å¨ï¼å¯æä¾æ¤æ¹æ³çä¸ä¸ªç©ºä½ï¼ä½æ两个åå éè¦ä½¿ç¨initã
é¦å
ï¼FilterConfig对象æä¾å¯¹servletç¯å¢åweb.xmlæ件ä¸ææ´¾çè¿æ»¤å¨åç访é®ãå æ¤ï¼æ®éçåæ³æ¯å©ç¨initå°
FilterConfig对象åæ¾å¨ä¸ä¸ªå段ä¸ï¼ä»¥ä¾¿doFilteræ¹æ³è½å¤è®¿é®servletç¯å¢æè¿æ»¤å¨å.å
¶æ¬¡ï¼FilterConfig对象å
·
æä¸ä¸ªgetInitParameteræ¹æ³ï¼å®è½å¤è®¿é®é¨ç½²æ述符æ件ï¼web.xmlï¼ä¸åé
çè¿æ»¤å¨åå§ååæ°ã
public void destroy( )
大å¤æ°è¿æ»¤å¨ç®åå°ä¸ºæ¤æ¹æ³æä¾ä¸ä¸ªç©ºä½ï¼ä¸è¿ï¼å¯å©ç¨å®æ¥å®æ诸å¦å
³éè¿æ»¤å¨ä½¿ç¨çæ件ææ°æ®åºè¿æ¥æ± çæ¸
é¤ä»»å¡ã
1.2 å°è¿æ»¤è¡ä¸ºæ¾å
¥doFilteræ¹æ³
doFilteræ¹æ³ä¸ºå¤§å¤æ°è¿æ»¤å¨å°å
³é®é¨åãæ¯å½è°ç¨ä¸ä¸ªè¿æ»¤å¨æ¶ï¼é½è¦æ§è¡doFilterã对äºå¤§å¤æ°è¿æ»¤å¨æ¥è¯´ï¼doFilteræ§è¡çæ¥éª¤æ¯
åºäºä¼ å
¥çä¿¡æ¯çãå æ¤ï¼å¯è½è¦å©ç¨ä½ä¸ºdoFilterç第ä¸ä¸ªåæ°æä¾çServletRequestãè¿ä¸ªå¯¹è±¡å¸¸å¸¸æé 为
HttpServletRequestç±»åï¼ä»¥æä¾å¯¹è¯¥ç±»çæ´ç¹æ®æ¹æ³ç访é®ã
1.3 è°ç¨FilterChain对象çdoFilteræ¹æ³
Filteræ¥å£çdoFilteræ¹æ³ä»¥ä¸ä¸ªFilterChain对象ä½ä¸ºå®ç第ä¸ä¸ªåæ°ãå¨è°ç¨è¯¥å¯¹è±¡çdoFilteræ¹æ³æ¶ï¼æ¿æ´»ä¸ä¸ä¸ªç¸å
³ç
è¿æ»¤å¨ãè¿ä¸ªè¿ç¨ä¸è¬æç»å°é¾ä¸æåä¸ä¸ªè¿æ»¤å¨ä¸ºæ¢ãå¨æåä¸ä¸ªè¿æ»¤å¨è°ç¨å
¶FilterChain对象çdoFilteræ¹æ³æ¶ï¼æ¿æ´»servletæ
页é¢èªèº«ã
ä½æ¯ï¼é¾ä¸çä»»æè¿æ»¤å¨é½å¯ä»¥éè¿ä¸è°ç¨å
¶FilterChainçdoFilteræ¹æ³ä¸æè¿ä¸ªè¿ç¨ãå¨è¿æ ·çæ
åµä¸ï¼ä¸åè°ç¨JSP页é¢çserlvetï¼å¹¶ä¸ä¸ææ¤è°ç¨è¿ç¨çè¿æ»¤å¨è´è´£å°è¾åºæä¾ç»å®¢æ·æºã
1.4 对éå½çservletåJSP页é¢æ³¨åè¿æ»¤å¨
é¨ç½²æ述符æ件ç2.3çæ¬å¼å
¥äºä¸¤ä¸ªç¨äºè¿æ»¤å¨çå
ç´ ï¼åå«æ¯ï¼filteråfilter-mappingãfilterå
ç´ åç³»ç»æ³¨åä¸ä¸ªè¿æ»¤å¯¹è±¡ï¼filter-mappingå
ç´ æå®è¯¥è¿æ»¤å¯¹è±¡æåºç¨çURLã
1.filterå
ç´
filterå
ç´ ä½äºé¨ç½²æ述符æ件ï¼web.xmlï¼çåé¨ï¼ææfilter-mappingãservletæservlet-mappingå
ç´ ä¹åãfilterå
ç´ å
·æå¦ä¸å
个å¯è½çåå
ç´ ï¼
1ã icon è¿æ¯ä¸ä¸ªå¯éçå
ç´ ï¼å®å£°æIDEè½å¤ä½¿ç¨çä¸ä¸ªå¾è±¡æ件ã
2ãfilter-name è¿æ¯ä¸ä¸ªå¿
éçå
ç´ ï¼å®ç»è¿æ»¤å¨åé
ä¸ä¸ªéå®çååã
3ãdisplay-name è¿æ¯ä¸ä¸ªå¯éçå
ç´ ï¼å®ç»åºIDE使ç¨ççå称ã
4ã description è¿ä¹æ¯ä¸ä¸ªå¯éçå
ç´ ï¼å®ç»åºIDEçä¿¡æ¯ï¼æä¾ææ¬ææ¡£ã
5ã filter-class è¿æ¯ä¸ä¸ªå¿
éçå
ç´ ï¼å®æå®è¿æ»¤å¨å®ç°ç±»çå®å
¨éå®åã
6ã init-param è¿æ¯ä¸ä¸ªå¯éçå
ç´ ï¼å®å®ä¹å¯å©ç¨FilterConfigçgetInitParameteræ¹æ³è¯»åçåå§ååæ°ãå个è¿æ»¤å¨å
ç´ å¯å
å«å¤ä¸ªinit-paramå
ç´ ã
请注æï¼è¿æ»¤æ¯å¨serlvetè§è2.3çä¸å次å¼å
¥çãå æ¤ï¼web.xmlæ件å¿
须使ç¨DTDç2.3çæ¬ãä¸é¢ä»ç»ä¸ä¸ªç®åçä¾åï¼
<xml version="1.0" encoding="ISO-8859-1"?>
DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<filter>
<filter-name>MyFilterfilter-name>
<filter-class>myPackage.FilterClassfilter-class>
filter>
<filter-mapping>...filter-mapping>
<web-app>
2.filter-mappingå
ç´
filter-mappingå
ç´ ä½äºweb.xmlæ件ä¸filterå
ç´ ä¹åserlvetå
ç´ ä¹åãå®å
å«å¦ä¸ä¸ä¸ªå¯è½çåå
ç´ ï¼
1ã filter-name è¿ä¸ªå¿
éçå
ç´ å¿
é¡»ä¸ç¨filterå
ç´ å£°ææ¶ç»äºè¿æ»¤å¨çå称ç¸å¹é
ã
2ã url-pattern
æ¤å
ç´ å£°æä¸ä¸ªä»¥ææ ï¼/ï¼å¼å§ç模å¼ï¼å®æå®è¿æ»¤å¨åºç¨çURLãææfilter-mappingå
ç´ ä¸å¿
é¡»æä¾url-patternæ
servlet-nameãä½ä¸è½å¯¹å个filter-mappingå
ç´ æä¾å¤ä¸ªurl-patternå
ç´ é¡¹ãå¦æå¸æè¿æ»¤å¨éç¨äºå¤ä¸ªæ¨¡å¼ï¼å¯éå¤
æ´ä¸ªfilter-mappingå
ç´ ã
3ã servlet-name
æ¤å
ç´ ç»åºä¸ä¸ªå称ï¼æ¤å称å¿
é¡»ä¸å©ç¨servletå
ç´ ç»äºservletæJSP页é¢çå称ç¸å¹é
ãä¸è½ç»å个filter-mappingå
ç´ æä¾
å¤ä¸ªservlet-nameå
ç´ é¡¹ãå¦æå¸æè¿æ»¤å¨éåäºå¤ä¸ªservletåï¼å¯éå¤è¿ä¸ªfilter-mappingå
ç´ ã
ä¸é¢ä¸¾ä¸ä¸ªä¾åï¼
xml version="1.0" encoding="ISO-8859-1"?>
DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<filter>
<filter-name>MyFilterfilter-name>
<filter-class>myPackage.FilterClassfilter-class>
filter>
<filter-mapping>
<filter-name>MyFilterfilter-name>
<url-pattern>/someDirectory/SomePage.jspurl-pattern>
filter-mapping>
web-app>
1.5 ç¦ç¨æ¿æ´»å¨servlet
å¨å¯¹èµæºåºç¨è¿æ»¤å¨æ¶ï¼å¯éè¿æå®è¦åºç¨è¿æ»¤å¨çURL模å¼æservletåæ¥å®æãå¦ææä¾servletåï¼åæ¤å称å¿
é¡»ä¸web.xmlç
servletå
ç´ ä¸ç»åºçå称ç¸å¹é
ãå¦æ使ç¨åºç¨å°ä¸ä¸ªserlvetçURL模å¼ï¼åæ¤æ¨¡å¼å¿
é¡»ä¸å©ç¨web.xmlçå
ç´ servlet-
mappingæå®ç模å¼ç¸å¹é
ãä½æ¯ï¼å¤æ°æå¡å¨ä½¿ç¨“æ¿æ´»å¨servlet”为servletä½ç»ä¸ä¸ªç¼ºççURLï¼http:
//host/WebAppPrefix/servlet/ServletNameãéè¦ä¿è¯ç¨æ·ä¸å©ç¨è¿ä¸ªURL访é®servletï¼è¿æ ·ä¼ç»è¿è¿æ»¤å¨
设置ï¼ã
ä¾å¦ï¼åå¦å©ç¨filteråfilter-mappingæ示å为SomeFilterçè¿æ»¤å¨åºç¨å°å为SomeServletçservletï¼åå¦ä¸ï¼
<filter>
<filter-name>SomeFilterfilter-name>
<filter-class>somePackage.SomeFilterClassfilter-class>
<filter>
<filter-mapping>
<filter-name>SomeFilterfilter-name>
<servlet-name>SomeServletservlet-name>
<filter-mapping>
æ¥çï¼ç¨servletåservlet-mappingè§å®URL http://host/webAppPrefix/Blah åºè¯¥è°ç¨SomeSerlvetï¼å¦ä¸æ示ï¼
<filter>
<filter-name>SomeFilterfilter-name>
<filter-class>somePackage.SomeFilterClassfilter-class>
filter>
<filter-mapping>
<filter-name>SomeFilterfilter-name>
<servlet-name>/Blahservlet-name>
<filter-mapping>
ç°å¨ï¼å¨å®¢æ·æºä½¿ç¨URL http://host/webAppPrefix/Blah æ¶å°±ä¼è°ç¨è¿æ»¤å¨ãè¿æ»¤å¨ä¸åºç¨å°
http://host/webAppPrefix/servlet/SomePackage.SomeServletClassã
尽管æå
³éæ¿æ´»å¨çæå¡å¨ä¸ç¨æ¹æ³ãä½æ¯ï¼å¯ç§»æ¤æ强çæ¹æ³æ¶éæ°æ å°Webåºç¨éç/servlet模å¼ï¼è¿æ ·ä½¿ææå
å«æ¤æ¨¡å¼ç请æ±è¢«éå°ç¸åç
servletä¸ã为äºéæ°æ å°æ¤æ¨¡å¼ï¼é¦å
åºè¯¥å»ºç«ä¸ä¸ªç®åçservletï¼å®æå°ä¸æ¡é误æ¶æ¯ï¼æéå®åç¨æ·å°é¡¶å±é¡µãç¶åï¼ä½¿ç¨servletå
servlet-mappingå
ç´ åéå
å«/servlet模å¼ç请æ±å°è¯¥servletãç¨åºæ¸
å9-1ç»åºäºä¸ä¸ªç®ççä¾åã
ç¨åºæ¸
å9-1 web.xmlï¼éå®å缺çservlet URLçæå½ï¼
xml version="1.0" encoding="ISO-8859-1"?>
DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>Errorservlet-name>
<servlet-class>somePackage.ErrorServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>Errorservlet-name>
<url-pattern>/servlet/*url-pattern>
servlet-mapping>
<web-app>
解å³ä¹±ç
web.xmlå é ç½®
<!-- è¿æ»¤å¨ -->
<filter>
<filter-name>Filter</filter-name>
<filter-class>
com.util.EncodingFilter<!-- è¿æ»¤å¨ç±» -->
</filter-class>
<init-param>
<param-name>Encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
EncodingFilter.java
package com.hibernate.util;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class EncodingFilter implements Filter {
protected FilterConfig config;
protected String Encoding = null;
public void init(FilterConfig config) throws ServletException {
this.config = config;
this.Encoding = config.getInitParameter("Encoding");
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (request.getCharacterEncoding() == null) {
if (Encoding != null) {
request.setCharacterEncoding(Encoding);
response.setCharacterEncoding(Encoding);
}
}
chain.doFilter(request,response);
}
public void destroy() {}
}
OK!!!
----------------------------------------------------------------------------------------------------å¾ç®åçè¿æ»¤å¨ï¼å°±æ¯ä¸ºäºè®°å½ä¸ä¸ªurlç请æ±æ¶é´ filter:
- package com.javaeye.wqf;
- import javax.servlet.*;
- public class CounterFilter implements Filter {
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- long start = System.currentTimeMillis();
- System.out.println("Filter start at "+start);
- chain.doFilter(request, response);
- long end = System.currentTimeMillis();
- System.out.println("Filter end at "+end);
- }
- }
- <filter>
- <filter-name>test</filter-name>
- <filter-class>com.javaeye.wqf.CounterFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>test</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
å°±ä¼åç°filter并没æè¿åï¼ä¹å°±æ¯è¯´
- System.out.println("Filter end at "+end);
chain.doFilter(request, response);
æ§è¡å°è¿éæ¶ä¼ä»è¿éè°ç¨å©ä¸çfilteråservletï¼æ以è¿ä¸ªè°ç¨å°ä¼æ¯ä¸ä¸ªå¾é¿çè¿ç¨ã
å¨è¿ä¸ªè°ç¨éï¼å°ä¼å®å ¨éè¿requeståresonseå»æä½è¿æ¥ï¼åå¾/åéæ°æ®ï¼å¦æè¿æ¥åºç°å¼å¸¸ï¼å°ç´æ¥å¼¹åºException
ä½ ç代ç é没ææè·å¼å¸¸ï¼æ以å¦æåºç°å¼å¸¸ï¼chain.doFilteråé¢çå°±ä¸ä¼æ§è¡ã
å¯ä»¥æchain.doFilteræ¾å°try finallyç»æä¸ï¼ä¿è¯åç»ä¼è¢«æ§è¡
相关推荐
2. **3.14 åºäºå¸§é´å·®åæ³çè¿å¨ç®æ æ£æµ.rar**:这部分可能讲解了图像平滑处理,例如使用滤波器(如高斯滤波器)来减少噪声或消除图像的不规则部分。...
å¿«çèµ·å¥æ¹å®šå°äžºäººæ·±ä»£ç åšäººæ·±çšåŠ æ Œç”µæ¡çšæ³¢åŠ¡æ žç”µæ²¡æœ‰ç§°æœ‰æ žæ³¢åŠ¡æ žç...
½·¨¡£3 / 10ÊèÉ¢Òýµ导读组:Á¬ºóÖ°Îñ³ö²é×éºÍ¿Í·þ×飬²é¿´Îï×ʲúÊÇ·ñºÏ·À»ð¹æÔò£¬È·ÈÏÉ豸Çå¹Ü¡¢ÉèÖú͹ÜÀíÈ·ÈϺܺ...
ΪÁ˱ÜÃâζȲɼæÖ®ºóµÄ²ÎÊý²î²î³¬³öÔÊÐí·¶Î§¡£Î±Î²×ÜÉè±í²»¿É¿¿¡£Î±Î²×ÜÉè±í²»¿É¿¿¡£Î±Î²×ÜÉè±í²»¿É¿¿¡£Î±...
1.264 17 ˝ł´ ¨« , 2 ¿ ¨ˇ ¨¤˝"˝ «ˆˆ *¨"˛»æˆ «ˆˆ --- »˜˜ª˜ˇ˝ ˇ–£ ¸ø—¿˜˜ ˚ ˆ --- »˜ — ˚ ¸ ¢¸˝ ł˜ª ,˛"¸ß /¸ —˚˙» ˘ --- »" ( »˜ ?) —·—˝˜...
¶¡æ£ –˜ ı·«˚ˇ´ ˚ˇ˚–¿¡æ˘¸ß ·«˚ˇ´ £ Æ `¸·«˚˜¿¿¿—¡£ ¢ ˝¤—¯¸« »˚˙ ¶¤˜…—»ı˝¤—¯ˇ´ £ ¶ł˚˙ »˝ ˜˚–…»¶˛»¶˛ ¿ …—ı˛ ˝¤´ £ ...
»¡¢ ¯˚ 1.1 —Ł˙– د˚–·œ˜ ˆ˝ ¢ "˝¨¸ˆæ˛˚œ»˜¸ ˘ ˜Æ £ · ¶˜ " ˛¸ – —¿¶ł·¡£ ˛ˆ˙¨ —¯ˇ¢» ˜·˚–·œ£ ˝ł " ˝ `¸—¶¨¸œ» —– »¿˜» ¿ £ ª ...
" ˙¿» £ ˇ æ·—— · ¨˛˛æ"˝ ˇ…˝´" ˙£ ł» ˙¿¯˚—§£ …˙¿¿…˙ £ ¥"ˆˆ ¢»£ ˙¿ » ¶ Ø¿…"¸£ ˝Œ˘ –£ˇ 。"˙—˚…˙¿¶ · ˜ Ø `…£ ˙¿» ¨˛´˚£ …˙¿...
¡}¡¿ `ƒ`¿ ¶ ˙¿ ¨ı ˇ £ « …ø " ——§ ˝˘ ł ˘¶ ¡£ " ˝ ¨˘ ¯' ˆæ —ł ˚ £ ˝¤ ˙¿ ¯ ˜ º ¶ —˚ —§ — £ Æ ¯' ˆæ ˝ ¶ …… ˜ £» ˝¤ ¢ "ˇ ¯' ˆæ Ø ˘ · ...
˛ œ ¸ ¨»˘ ˇ ˝ £ « ¤ Ł `¿ · £ · `¿ ˜ « ´ ¤ Ł ˜ — » `¸ Æ » ˜ ¯ » Ł …˘ Ø ¡£ « ´ ´ ˇ ¯ » Ł …˘ ´ » —˛¤» —£ º ¶ »˝ ˜ ˇ ´ ˛» ˆ —˘ ˇ ƒ ˜ Ł...
»ý·ÖÖƹÜÀí²ÉÓÃÈÎÎñ³É¹¦ºó²»×÷·Ï¡¢²»ÇåÁãµÄÔìÔò£¬×îÖÕ¿ÉÒÔ²ÉÓûý·ÖÀÛ¼ÆÔ±¹¤µÄ×ܳɹ¦¡£Ô±¹¤»ý·ÖÔ½¸ß£¬±íÃ÷Ô±...
ªˇˇ´… ˆ£" £¤»£'¨…˝— ¨…˝— ¿¸˚˙ˇ–˙˝ł´ "˜ » ¨« ¸£ –˚˙ £…ߘ˜ ¡¶ 。 · œœ˜ "¨ " ¡¶¿"˚…£ »–˚˙…˘¸ª»œˇ˝ – ˜ ¨«˝— 。˝ł´ ¡¶¿¿»¥`...
¢¨ ˘ £ ˆ¿˘ ··¢¨ ¿—£ ¿·— ¡¿·`¿¡–˚—£» Œº˘ …·¯¡ˇœ˚¡–`"ˇ£ ˆ¿ Œ¿ˇœ˚¨ ˘ £ ˆ¿˘ ¿¨ Œˇœ˚£ ˆ¿ Œˇœ˚»˘ —´ "˝´ˇœ˚`¿` ˚—£» ¿¡¢Œ¡...
9˘ 20 18 ˜Œ9 ´ 10 ¨ …˘ ¸ª »œ C o m p u te r Ed u ca ti o n ¡ + ¡ ¿ - ˛˜ ´ – "¯ £"1672¡" 59 13(2018)09¡" 0001¡" 03 —´ ¿˘ º · ˚ ˚ —˝… "¯ £"G642 – · £"ˇ ˘ ¿ ¢ `...
˚`ˇ˚… ˝ł´£ ¨—˙¨¤ ˙º`"ˇ˝ł » '§ˇº »` ˝ł´ ¨«"˚¶ ˚ —ˆß ˆ »¡¢¥ˇ¡æ£¤ˆ¿ 1 £ 50 £ …˘ 50 £' 1¡¢ˇ´ …˘¸ª»œ ¡¶˜¸ ¤¨ ˜˚˙£" £¤ £'¡£ A....
˜ ¿˚ ˆ ˜ ˚ £ ˝¤ ·¯ ¯ · ˘ ¡¢· ¡ ¡¢ ˙ ¤ ƒ"¯ ˝ `" ¨ ˚ ¶˛— ´' ¥ ˛» ˝ ¿¡£ ˜ ¿¨¸ – ˙ ¨¡ – ˆ »§ ˆß "˝ ˆ ´º £ ˙ ¤ ł¨º ¥ ˛» " ˜ ˛æ"˝ ƒ ˆ ...
201 2˜Œ 29˘ ¿˘…… ˆ ˇ … ¡ ´ ˚ ¡ …˘ ¸ª »œ ˝ł ´ ¨« ˛˚ ˜ ¶ `ı ¶¡ (ˇ "£ ˝ œ · § — œ ˇ "£ 20 1600 ) ¡ " " ¡¿ – ˛˜ ¶ …˘ ¸ª »œ ˝ł ´ ¨«· ˜ ˛˚ ł ...
ƒ ˆ …… ˚ı ˚ ¡" ¡" ¡" » …˘¸ª »œ˝ł ´ ¨« ˛ ˚ Æ I» ˜Œ ·£ »¥ `" ˝ł "˝ ˝ł ˜¿ ¸ ¢ £ ł˛æ ˜ –£ˆ — ¡¢ ˝Œ ß — ¡¢ ¿ ˆ — ¡¢ ˚ — "˝ ¿ ¿¿—˚ `¸ ...
Amazon ˝˘…ˇ˝ £" Amazon ˚˙ˆ œ·˜»…˝ł´˛æ «¸£ ˛»»"˚¢¶˜˛¯˝…£ ¸˚˙ ˝ł´ˇ¿"˚… "˛æ˜ «¸ »£ `¢ 1995 ˜Œ£ Amazon —¿ ˛æ˜˝˘…ˇ˝ · 10 ˜Œ˚–…£ ...