1.javaä¸JSPåServletçåºå«ï¼
2.严重: Servlet.service() for servlet jsp threw exception java.lang.NullPointerException
javaä¸JSPåServletçåºå«ï¼
ä¸. Servletæ¯ä»ä¹?源码
客æ·æº/æå¡å¨è®¡ç®çåå±ãJavaæä¾äºä¸æ´å¥å®¢æ·æº/æå¡å¨è§£å³æ¹æ¡ï¼å¨è¿ä¸ªæ¹æ¡ä¸ï¼ç¨åºå¯ä»¥èªå¨å°ä¸è½½å°å®¢æ·ç«¯å¹¶æ§è¡ï¼è¿å°±æ¯appletãä½æ¯å®ä» ä» æ¯é®é¢çä¸åãé®é¢çå¦ä¸åå°±æ¯Servletã
servletå¯ä»¥è¢«è®¤ä¸ºæ¯æå¡å¨ç«¯çappletãservlet被Webæå¡å¨å è½½åæ§è¡ï¼å°±å¦åapplet被æµè§å¨å è½½åæ§è¡ä¸æ ·ãservletä»å®¢æ·ç«¯(éè¿Webæå¡å¨)æ¥æ¶è¯·æ±ï¼æ§è¡æç§ä½ä¸ï¼ç¶åè¿åç»æã使ç¨servletçåºæ¬æµç¨å¦ä¸ï¼
·客æ·ç«¯éè¿HTTPæåºè¯·æ±.
·Webæå¡å¨æ¥æ¶è¯¥è¯·æ±å¹¶å°å ¶åç»servletãå¦æè¿ä¸ªservletå°æªè¢«å è½½ï¼Webæå¡å¨å°æå®å è½½å°Javaèææºå¹¶ä¸æ§è¡å®ã
·servletå°æ¥æ¶è¯¥HTTP请æ±å¹¶æ§è¡æç§å¤çã
·servletå°åWebæå¡å¨è¿ååºçã
·Webæå¡å¨å°ä»servletæ¶å°çåºçåéç»å®¢æ·ç«¯ã
ç±äºservletæ¯å¨æå¡å¨ä¸æ§è¡ï¼é常ä¸appletç¸å ³çå®å ¨æ§çé®é¢å¹¶ä¸éå®ç°ãè¦æ³¨æçæ¯Webæµè§å¨å¹¶ä¸ç´æ¥åservletéä¿¡ï¼servletæ¯ç±Webæå¡å¨å è½½åæ§è¡çã
èservletæ¯ç¨Javaç¼åçï¼æ以å®ä»¬ä¸å¼å§å°±æ¯å¹³å°æ å ³çãè¿æ ·ï¼Javaç¼åä¸æ¬¡å°±å¯ä»¥å¨ä»»ä½å¹³å°è¿è¡(write once,run anywhere)çæ¿è¯ºå°±åæ ·å¯ä»¥å¨æå¡å¨ä¸å®ç°äºãservletè¿æä¸äºCGIèæ¬æä¸å ·å¤çç¬ç¹ä¼ç¹ï¼ (æ¬äººå¯¹CGI并ä¸æ¯ååäºè§£ï¼æ以è¿äºç¹ç¹ä¸è½å®å ¨çä½ä¼å°ï¼è¿ä¹æ¯æèªè®ºåçè´´åï¼è¯·è§è° )
servletæ¯æä¹ çãservletåªéWebæå¡å¨å è½½ä¸æ¬¡ï¼èä¸å¯ä»¥å¨ä¸å请æ±ä¹é´ä¿ææå¡(ä¾å¦ä¸æ¬¡æ°æ®åºè¿æ¥)ãä¸ä¹ç¸åï¼CGIèæ¬æ¯çæçãç¬æçãæ¯ä¸æ¬¡å¯¹CGIèæ¬ç请æ±ï¼é½ä¼ä½¿Webæå¡å¨å 载并æ§è¡è¯¥èæ¬ãä¸æ¦è¿ä¸ªCGIèæ¬è¿è¡ç»æï¼å®å°±ä¼è¢«ä»å åä¸æ¸ é¤ï¼ç¶åå°ç»æè¿åå°å®¢æ·ç«¯ãCGIèæ¬çæ¯ä¸æ¬¡ä½¿ç¨ï¼é½ä¼é æç¨åºåå§åè¿ç¨(ä¾å¦è¿æ¥æ°æ®åº)çéå¤æ§è¡ã
servletæ¯ä¸å¹³å°æ å ³çãå¦åæè¿°ï¼servletæ¯ç¨Javaç¼åçï¼å®èªç¶ä¹ç»§æ¿äºJavaçå¹³å°æ å ³æ§ã
servletæ¯å¯æ©å±çãç±äºservletæ¯ç¨Javaç¼åçï¼å®å°±å ·å¤äºJavaæè½å¸¦æ¥çææä¼ç¹ãJavaæ¯å¥å£®çãé¢å对象çç¼ç¨è¯è¨ï¼å®å¾å®¹ææ©å±ä»¥éåºä½ çéæ±ãservletèªç¶ä¹å ·å¤äºè¿äºç¹å¾ã
servletæ¯å®å ¨çãä»å¤çè°ç¨ä¸ä¸ªservletçæä¸æ¹æ³å°±æ¯éè¿Webæå¡å¨ãè¿æä¾äºé«æ°´å¹³çå®å ¨æ§ä¿éï¼å°¤å ¶æ¯å¨ä½ çWebæå¡å¨æé²ç«å¢ä¿æ¤çæ¶åã
ãã setvletå¯ä»¥å¨å¤ç§å¤æ ·ç客æ·æºä¸ä½¿ç¨ãç±äºservletæ¯ç¨Javaç¼åçï¼æä»¥ä½ å¯ä»¥å¾æ¹ä¾¿å°å¨HTMLä¸ä½¿ç¨å®ä»¬ï¼å°±åä½ ä½¿ç¨appletä¸æ ·ã
é£ä¹ï¼Servletæ¯ææ ·æ§è¡çï¼ææ ·æ¥åä¸ä¸ªServlet,å®çåºæ¬æ¶ææ¯æä¹æ ·çï¼
è¿äºé®é¢ï¼å°å¨åé¢é¨åç»äºä»ç»ã
äº.JSPä¸Servlet
ç°å¨å·²ç»å¯¹Servletæäºå¤§æ¦çäºè§£ï¼ç°å¨æ们就æ¥è¯´è¯´JSPåServletçå ³ç³»ã
JSPæ¯ä¸ç§èæ¬è¯è¨ï¼å è£ äºJava Servletç³»ç»ççé¢ï¼ç®åäºJavaåServletç使ç¨é¾åº¦ï¼åæ¶éè¿æ©å±JSPæ ç¾(TAG)æä¾äºç½é¡µå¨ææ§è¡çè½åã尽管å¦æ¤ï¼JSPä»æ²¡æè¶ åºJavaåServletçèå´ï¼ä¸ä» JSP页é¢ä¸å¯ä»¥ç´æ¥åJava代ç ï¼èä¸JSPæ¯å 被è¯æServletä¹åæå®é è¿è¡çãJSPå¨æå¡å¨ä¸æ§è¡ï¼å¹¶å°æ§è¡ç»æè¾åºå°å®¢æ·ç«¯æµè§å¨ï¼æ们å¯ä»¥è¯´åºæ¬ä¸ä¸æµè§å¨æ å ³ãå®æ¯ä¸JavaScriptä¸åçï¼JavaScriptæ¯å¨å®¢æ·ç«¯çèæ¬è¯è¨ï¼å¨å®¢æ·ç«¯æ§è¡ï¼ä¸æå¡å¨æ å ³ã é£ä¹JSPæ¯ä»ä¹ï¼å°±æ¯Servlet.
JSPä¸Servletä¹é´ç主è¦å·®å¼å¨äºï¼JSPæä¾äºä¸å¥ç®åçæ ç¾ï¼åHTMLèåçæ¯è¾å¥½ï¼å¯ä»¥ä½¿ä¸äºè§£Servletç人å¯ä»¥ååºå¨æç½é¡µæ¥ã对äºJavaè¯è¨ä¸çæç人(æ¯å¦åæ),ä¼è§å¾JSPå¼åæ¯è¾æ¹ä¾¿ãJSPä¿®æ¹åå¯ä»¥ç«å³çå°ç»æï¼ä¸éè¦æå·¥ç¼è¯ï¼JSPå¼æä¼æ¥åè¿äºå·¥ä½ï¼èServelt缺éè¦ç¼è¯ï¼éæ°å¯å¨Servletå¼æçä¸ç³»åå¨ä½ãä½æ¯å¨JSPä¸ï¼HTMLä¸ç¨åºä»£ç æ··æå¨ä¸èµ·ï¼èServletå´ä¸æ¯è¿æ ·ãä¹è®¸å¤§å®¶æ¯è¾æ··ä¹±äºï¼é£ä¹Servletåæ¯ä»ä¹ï¼ä¸é¢æ们对JSPçè¿è¡æ¥åä¸ä¸ªç®åçä»ç»ï¼åè¯å¤§å®¶ææ ·æ¥æ§è¡ä¸ä¸ªJSPæ件ï¼
å½Webæå¡å¨(æServletå¼æ,åºç¨æå¡å¨)æ¯æJSPå¼ææ¶ï¼JSPå¼æä¼ç §çJSPçè¯æ³ï¼å°JSPæ件转æ¢æServlet代ç æºæ件ï¼æ¥çServletä¼è¢«ç¼è¯æJavaå¯æ§è¡åèç (bytecode)ï¼å¹¶ä»¥ä¸è¬çServletæ¹å¼è½½å ¥æ§è¡
JSPè¯æ³ç®åï¼å¯ä»¥æ¹ä¾¿çåµå ¥HTMLä¹ä¸ï¼å¾å®¹æå å ¥å¨æçé¨åï¼æ¹ä¾¿çè¾åºHTMLãå¨Servletä¸è¾åºHTML缺éè¦è°ç¨ç¹å®çæ¹æ³ï¼å¯¹äºå¼å·ä¹ç±»çå符ä¹è¦åç¹æ®çå¤çï¼å å¨å¤æçHTML页é¢ä¸ä½ä¸ºå¨æé¨åï¼æ¯èµ·JSPæ¥è¯´æ¯æ¯è¾å°é¾çã
é¤å»äºè½¬æ¢åç¼è¯é¶æ®µï¼JSPåServletä¹é´çåºå«å®å¨æ¯ä¸å¤§ã
JSPå¼æé常æ¶æå¨Servletå¼æä¹ä¸ï¼æ¬èº«å°±æ¯ä¸ä¸ªServletï¼æJSPæ件转è¯æServletæºä»£ç ï¼åè°ç¨Javaç¼è¯å¨ï¼ç¼è¯æServletãè¿ä¹æ¯JSPå¨ç¬¬ä¸æ¬¡è°ç¨æ¶é度æ¯è¾æ ¢çåå ï¼å¨ç¬¬ä¸æ¬¡ç¼è¯ä¹åï¼JSPä¸Servleté度ç¸å.ä¸é¢æ们æ¥çç为ä»ä¹ä»ä»¬å¨ç¬¬ä¸æ¬¡ç¼è¯ååç¼è¯çé度ç¸åï¼
å¨æ´ä¸ªè¿è¡è¿ç¨ä¸ï¼JSPå¼æä¼æ£æ¥ç¼è¯å¥½çJSP(以Servletå½¢å¼åå¨)æ¯å¦æ¯åå§çJSPæ件è¿æ°ï¼å¦ææ¯ï¼JSPå¼æä¸ä¼ç¼è¯;å¦æä¸æ¯ï¼è¡¨ç¤ºJSPæ件æ¯è¾æ°ï¼å°±ä¼éæ°æ§è¡è½¬è¯ä¸ç¼è¯çè¿ç¨ã
为äºæ个深å»çäºè§£ï¼æ们çä¸ä¸JSPçè¿è¡åå¼åç¯å¢:
æµè§å¨ï¼å¸¸è§çæµè§å¨æIEåNetscape两ç§ã
æ°æ®åºï¼å¸¸ç¨çæ°æ®åºæOracle,SQL Server,Informix,DB2,Sybase,Access,MySQLçã
æä½ç³»ç»ï¼å¸¸è§çæWindows,Linux,以ååç§Unixç³»ç»ã
Webæå¡å¨ï¼å¸¸è§çæIIS,Apache,Netscape Enterprise Serverçã
JSPå¼æï¼ä¸è¬JSPå¼æé½ä»¥Servletå¼æ为åºç¡ï¼å¹¶ä»¥Servletçå½¢å¼åºç°ãåæ¶ï¼å¨åç§å è´¹ååä¸å¼æçå®ç°ä¸ï¼Servletå¼æåJspå¼æé常ä¹æ¯ä¸èµ·åºç°ï¼æ们æ为Servlet/JSPå¼æï¼æä»æç§æ为JSPå¼æã
JSPå¼ææ¯å¯ä»¥æä¾JSPåServletè¿è¡æ¯æå¹¶å¯¹å ¶çåå¨æè¿è¡ç®¡ççç³»ç»çº§å®ä½ã
å¨JSP页é¢ç¬¬ä¸æ¬¡è¢«è¯·æ±æ¶ï¼JSPå¼æä¼å°JSPåå§æ件转æ¢æServletæºä»£ç ï¼ç¶åè°ç¨Javaç¼è¯å¨ï¼ç¼è¯æServlet,并å¨Servletå¼æä¸æ§è¡ãå½å次æ请æ±çæ¶åï¼JSPå¼æä¼è§å·®å¼ç¼è¯å¥½çJSPæ¯å¦æ¯åæ¥çJSPåå§æ件è¦æ°ï¼å¦ææ¯,è¿è¡Servletï¼å¦æä¸æ¯ï¼è¡¨ç¤ºæ件已ç»æ´æ°çäºï¼å°±ä¼ä»æ°æ§è¡è½¬æ¢åç¼è¯çè¿ç¨ã
说å°è¿éï¼ä¹åºæ¬æJSPåServletçå ³ç³»è¯´æ¸ æ¥äºï¼ä»æçæè§ä¸çç¨JSPå°±å¯ä»¥äºï¼ç®ååæ¹ä¾¿ï¼åå¯ä»¥åBean å¾å¥½çå ¼å®¹ä½¿ç¨ï¼åè½åå¾å¼ºå¤§ï¼ä¸ºä»ä¹ååºç°äºServlet,å®åæä»ä¹ç¨ï¼ä½åµå®çç¼ååç¸å¯¹å¤æã为äºæé®é¢è¯´å¾æ´æ¸ æ¥ä¸ç¹ï¼ææ³å¨è¿é说ä¸ä¸åå²ï¼é¡ºä¾¿å讲ä¸ä¸ä¸ºä»ä¹è¿è¦ç¨Servletï¼Servletç好å¤æ¯ä»ä¹ã
åå²ç®è¿°ï¼ï¼æèªæ论åæå èï¼æ¹åï¼
ç®åç说ï¼SUNé¦å åå±åºSERVLETï¼å ¶åè½æ¯è¾å¼ºå²ï¼ä½ç³»è®¾è®¡ä¹å¾å è¿ï¼åªæ¯ï¼å®è¾åºHTMLè¯å¥è¿æ¯éç¨äºèçCGIæ¹å¼ï¼æ¯ä¸å¥ä¸å¥è¾åºï¼æ以ï¼ç¼ååä¿®æ¹HTMLé常ä¸æ¹ä¾¿ã
åæ¥SUNæ¨åºäºç±»ä¼¼äºASPçé¶åµåçJSPï¼æ¯Servletåå±ç产ç©ï¼ï¼æJSP TAGé¶åµå°HTMLè¯å¥ä¸ï¼è¿æ ·ï¼å°±å¤§å¤§ç®ååæ¹ä¾¿äºç½é¡µç设计åä¿®æ¹ãæ°åçç½ç»è¯è¨å¦ASPï¼PHPï¼JSPé½æ¯é¶åµåçSCRIPTè¯è¨ã
ä»ç½ç»ä¸å±ç»æçè§åº¦çï¼ä¸ä¸ªç½ç»é¡¹ç®æå°åä¸å±ï¼data layer,business layer, presentation layerãå½ç¶ä¹å¯ä»¥æ´å¤æãSERVLETç¨æ¥åbusiness layeræ¯å¾å¼ºå¤§çï¼ä½æ¯å¯¹äºåpresentation layerå°±å¾ä¸æ¹ä¾¿ãJSPå主è¦æ¯ä¸ºäºæ¹ä¾¿åpresentation layerè设计çãå½ç¶ä¹å¯ä»¥åbusiness layerãåæ¯äºASPï¼PHPï¼CGIçæåï¼ç»å¸¸ä¼ä¸èªè§çæpresentation layeråbusiness layeræ··å¨ä¸èµ·ãææ°æ®åºå¤çä¿¡æ¯æ¾å°JSPä¸ï¼å ¶å®ï¼å®åºè¯¥æ¾å¨business layerä¸ã
æ ¹æ®SUNèªå·±çæ¨èï¼JSPä¸åºè¯¥ä» ä» åæ¾ä¸presentation layeræå ³çå 容ï¼ä¹å°±æ¯è¯´ï¼åªæ¾è¾åºHTMLç½é¡µçé¨ä»½ãèææçæ°æ®è®¡ç®ï¼æ°æ®åæï¼æ°æ®åºèç»å¤çï¼ç»ç»æ¯å±äºbusiness layerï¼åºè¯¥æ¾å¨JAVA BEANSä¸ãéè¿JSPè°ç¨JAVA BEANSï¼å®ç°ä¸¤å±çæ´åã
å®é ä¸ï¼å¾®è½¯æ¨åºçDNAææ¯ï¼ç®å说ï¼å°±æ¯ASP+COM/DCOMææ¯ãä¸JSP+BEANSå®å ¨ç±»ä¼¼ï¼ææçpresentation layerç±ASPå®æï¼ææçbusiness layerç±COM/DCOMå®æãéè¿è°ç¨ï¼å®ç°æ´åãç°å¨å¾®è½¯æ¨åºç.NETä¹æ¯éè¿è¿ä¸ªç念ï¼ææçpresentation layerç±ASP.NETå®æï¼business layerç±C#æVB.NETæVC.NETæ¥å®æã
为ä»ä¹è¦éç¨è¿äºç»ä»¶ææ¯å¢ï¼å 为å纯çASP/JSPè¯è¨æ¯é常ä½æçæ§è¡çï¼å¦æåºç°å¤§éç¨æ·ç¹å»ï¼çº¯SCRIPTè¯è¨å¾å¿«å°±å°è¾¾äºä»çåè½ä¸éï¼èç»ä»¶ææ¯å°±è½å¤§å¹ 度æé«åè½ä¸éï¼å å¿«æ§è¡é度ã
å¦å¤ä¸æ¹é¢ï¼çº¯SCRIPTè¯è¨å°presentation layeråbusiness layeræ··å¨ä¸èµ·ï¼é æä¿®æ¹ä¸æ¹ä¾¿ï¼å¹¶ä¸ä»£ç ä¸è½éå¤å©ç¨ãå¦ææ³ä¿®æ¹ä¸ä¸ªå°æ¹ï¼ç»å¸¸ä¼çµæ¶å°åå 页CODEï¼éç¨ç»ä»¶ææ¯å°±åªæ¹ç»ä»¶å°±å¯ä»¥äºã
综ä¸æè¿°ï¼SERVLETæ¯ä¸ä¸ªæ©æçä¸å®åç产åï¼åbusiness layerå¾å¥½ï¼åpresentation layerå°±å¾ä¸å¥½ï¼å¹¶ä¸ä¸¤å±æ··æï¼æ¾å¾ååæ··ä¹±ã
æ以ï¼æ¨åºJSP+BAENï¼ç¨JSPåpresentation layer,ç¨BAENåbusiness layerãSUNèªå·±çææä¹æ¯å°æ¥ç¨JSPæ¿ä»£SERVLETã
çäºä¸é¢çåè¿°ï¼å¤§å®¶å¯è½å¯¹JSPä¸Servletå ±åæäºæ¯è¾å¥½ç认è¯ãå¯ä»¥çå°JSPåBeanç»ååççå®ç¨æ§ï¼å¼ºå¤§ç表ç°åè½ï¼æç¨æ§é½æ¯Servletæä¸è½åçãé£ä¹æ¯ä¸æ¯Servlet就被å代äºï¼ä¸æ¯ï¼å¨ä»¥åçåå±ä¸ï¼å®è¿æ¯æç巨大çä½ç¨çãä¸é¢åªä¸è¿æ¯å°äºé®é¢çä¸æ¹é¢ï¼ä¸é¢æ们æ¥ççServletæ¬èº«çç¹ç¹ã
ç±äºå®æ¯ç±javaæ¥åçï¼æ以ç¸å ³çç¹ç¹æ们就ä¸è¯´äºï¼ä¸æå·²ç»æäºè¯¦ç»çä»ç»ï¼æ们æ¥ççå ¶ä»çï¼
Servletæ¯ç¨äºå¼åæå¡å¨ç«¯åºç¨ç¨åºçä¸ç§ç¼ç¨æ¨¡åï¼å¦æåªæ¯ä¸ä¸ªæ®éçjavaåºç¨ï¼å¯ä»¥ä¸ä½¿ç¨servletæ¥ç¼åï¼ä½æ¯å¦ææ³è¦æä¾åºäºwebçæå¡è½åï¼é£ä¹å°±å¿ é¡»æç §è¿ç§æ¨¡åæ¥ç¼åï¼èä¸servletä¹å¿ é¡»å 许å¨ç¬¦åservletè§èçjava web server or app serverä¹ä¸ï¼å¦åæ æ³è¿è¡ãé¤éä½ èªå·±å®ç°ä¸ä¸ªweb server,ä½æ¯å ¶å¤æ度æ¯æ¯è¾é«çï¼ç¹å«æ¯å¨ä¼ä¸çº§åºç¨ä¸ï¼å¯¹ç³»ç»ç稳å®æ§åå¥å£®æ§é½è¦æ±æ¯è¾é«ï¼æ以servletç模åå®é ä¸æ¯ç®åäºç¼å稳å¥çæå¡å¨ç«¯çåºç¨å¼åè¿ç¨ãServlet å¯ä»¥ä½ä¸ºæä¾webæå¡è½åçä¸ä¸ªæ¥å ¥æ¹å¼
ç°å¨ä¹è®¸å¯ä»¥ç解äºä»ä¹æ¯Servletä»ä¹æ¯JSPï¼å®ä»¬ä¹é´çå ³ç³»æ¯ææ ·çã
严重: Servlet.service() for servlet jsp threw exception java.lang.NullPointerException
出现此错误一般都是在jsp中使用了输出流(如输出验证码,文件下载等),源码没有妥善处理好的源码安卓商城源码下载原因。具体的源码原因就是在tomcat中jsp编译成servlet之后在函数_jspService(HttpServletRequestrequest,HttpServletResponseresponse)的最后有一段这样的代码finally{ if(_jspxFactory!=null)_jspxFactory.releasePageContext(_jspx_page_context);}这里是在释放在jsp中使用的对象,会调用response.getWriter(),源码如何找到小网站网址源码因为这个方法是和response.getOutputStream()相冲突的!所以会出现以上这个异常。源码开源论坛网源码免费下载然后当然是源码要提出解决的法,其实挺简单的源码(并不是和某些朋友说的那样--将jsp内的所有空格和回车符号所有都删除掉),在使用完输出流以后调用以下两行代码即可:out.clear();out=pageContext.pushBody();最后这里是源码一个输出彩色验证码例子(这样的例子几乎随处可见)imag.jsp)fc=;if(bc>)bc=;intr=fc+random.nextInt(bc-fc);intg=fc+random.nextInt(bc-fc);intb=fc+random.nextInt(bc-fc);returnnewColor(r,g,b);}%>如有不足之处,欢迎斧正!源码2getOutputStream()hasalreadybeencalledforthisresponse问题的源码解决在jsp向页面输出的时候,使用response.getOutputStream()会有这样的提示:java.lang.IllegalStateException:getOutputStream()hasalreadybeencalledforthisresponse,会抛出Exception原因一:JSP默认的输出流为PrintWriter,即以外的东西所默认的输出方式,如果你尝试在JSP中使用ServletOutputStream就会引起错误.要嘛直接改用Servlet输出(复写service方法),要嘛删除除%>0){ output.write(b,0,len);}output.flush();而不是把response.getOutputStream().write()放到循环体内在页面中直接写:将会出现错误消息如下:java.lang.IllegalStateException:getOutputStream()hasalreadybeencalledforthisresponseorg.apache.catalina.connector.Response.getWriter(Response.java:)org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:)org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:)org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:)