jsp Servlet½Å±¾ÔªËØ¡¢Ö¸ÁîºÍÔ¤¶¨Òå±äÁ¿
12.1 JSP½Å±¾ÔªËØ
¡¡¡¡ JSP½Å±¾ÔªËØÓÃÀ´²åÈëJava´úÂ룬ÕâЩJava´úÂ뽫³öÏÖÔÚÓɵ±Ç°JSPÒ³ÃæÉú³ÉµÄServletÖС£½Å±¾ÔªËØÓÐÈýÖÖ¸ñʽ£º
±í´ïʽ¸ñʽ£¼%= expression %£¾£º¼ÆËã±í´ïʽ²¢Êä³öÆä½á¹û¡£
Scriptlet¸ñʽ£¼% code %£¾£º°Ñ´úÂë²åÈëµ½ServletµÄservice·½·¨¡£
ÉùÃ÷¸ñʽ£¼%! code %£¾£º°ÑÉùÃ÷¼ÓÈëµ½ServletÀࣨÔÚÈκη½·¨Ö®Í⣩¡£
¡¡¡¡ ÏÂÃæÎÒÃÇÏêϸ˵Ã÷ËüÃǵÄÓ÷¨¡£
¡¡¡¡ 12.1.1 JSP±í´ïʽ
¡¡¡¡ JSP±í´ïʽÓÃÀ´°ÑJavaÊý¾ÝÖ±½Ó²åÈëµ½Êä³ö¡£ÆäÓï·¨ÈçÏ£º
£¼%= Java Expression %£¾
¡¡¡¡ ¼ÆËãJava±í´ïʽµÃµ½µÄ½á¹û±»×ª»»³É×Ö·û´®£¬È»ºó²åÈëµ½Ò³Ãæ¡£¼ÆËãÔÚÔËÐÐʱ½øÐУ¨Ò³Ãæ±»ÇëÇóʱ£©£¬Òò´Ë¿ÉÒÔ·ÃÎʺÍÇëÇóÓйصÄÈ«²¿ÐÅÏ¢¡£ÀýÈ磬ÏÂÃæµÄ´úÂëÏÔʾҳÃæ±»ÇëÇóµÄÈÕÆÚ/ʱ¼ä£º
Current time: £¼%= new java.util.Date() %£¾
¡¡¡¡ Ϊ¼ò»¯ÕâЩ±í´ïʽ£¬JSPÔ¤¶¨ÒåÁËÒ»×é¿ÉÒÔÖ±½ÓʹÓõĶÔÏó±äÁ¿¡£ºóÃæÎÒÃǽ«Ïêϸ½éÉÜÕâЩÒþº¬ÉùÃ÷µÄ¶ÔÏ󣬵«¶ÔÓÚJSP±í´ïʽÀ´Ëµ£¬×îÖØÒªµÄ¼¸¸ö¶ÔÏó¼°ÆäÀàÐÍÈçÏ£º
request£ºHttpServletRequest;
response£ºHttpServletResponse;
session£ººÍrequest¹ØÁªµÄHttpSession
out£ºPrintWriter£¨´ø»º³åµÄ°æ±¾£¬JspWriter£©£¬ÓÃÀ´°ÑÊä³ö·¢Ë͵½¿Í»§¶Ë
¡¡¡¡ ÏÂÃæÊÇÒ»¸öÀý×Ó£º
Your hostname: £¼%= request.getRemoteHost() %£¾
¡¡¡¡ ×îºó£¬Èç¹ûʹÓÃXMLµÄ»°£¬JSP±í´ïʽҲ¿ÉÒÔд³ÉÏÂÃæÕâÖÖÐÎʽ£º
£¼jsp:expression£¾
Java Expression
£¼/jsp:expression£¾
¡¡¡¡ Çë¼ÇסXMLÔªËغÍHTML²»Ò»Ñù¡£XMLÊÇ´óСдÃô¸ÐµÄ£¬Òò´ËÎñ±ØʹÓÃСд¡£ÓйØXMLÓï·¨µÄ˵Ã÷£¬Çë²Î¼û¡¶XML½Ì³Ì ¡·
¡¡¡¡ 12.1.2 JSP Scriptlet
¡¡¡¡ Èç¹ûÄãÒªÍê³ÉµÄÈÎÎñ±È²åÈë¼òµ¥µÄ±í´ïʽ¸ü¼Ó¸´ÔÓ£¬¿ÉÒÔʹÓÃJSP Scriptlet¡£JSP ScriptletÔÊÐíÄã°ÑÈÎÒâµÄJava´úÂë²åÈëServlet¡£JSP ScriptletÓï·¨ÈçÏ£º
£¼% Java Code %£¾
¡¡¡¡ ºÍJSP±í´ïʽһÑù£¬ScriptletÒ²¿ÉÒÔ·ÃÎÊËùÓÐÔ¤¶¨ÒåµÄ±äÁ¿¡£ÀýÈ磬Èç¹ûÄãÒªÏò½á¹ûÒ³ÃæÊä³öÄÚÈÝ£¬¿ÉÒÔʹÓÃout±äÁ¿£º
£¼%
String queryData = request.getQueryString();
out.println("Attached GET data: " + queryData);
%£¾
¡¡¡¡ ×¢ÒâScriptletÖеĴúÂ뽫±»ÕÕ°áµ½ServletÄÚ£¬¶øScriptletÇ°ÃæºÍºóÃæµÄ¾²Ì¬HTML£¨Ä£°åÎı¾£©½«±»×ª»»³ÉprintlnÓï¾ä¡£Õâ¾ÍÒâζ×Å£¬ScriptletÄÚµÄJavaÓï¾ä²¢·ÇÒ»¶¨ÒªÊÇÍêÕûµÄ£¬Ã»ÓйرյĿ齫ӰÏìScriptletÍâµÄ¾²Ì¬HTML¡£ÀýÈ磬ÏÂÃæµÄJSPƬ¶Ï»ìºÏÁËÄ£°åÎı¾ºÍScriptlet£º
£¼% if (Math.random() £¼
Ïà¹ØÎĵµ£º
ÔÚjspÖУ¬Èç¹ûÔÚurlÖд«µÝÖÐÎÄʱ»á³öÏÖÂÒÂ룬ÔÚÍøÉϹØÓÚÕâÒ»ÎÊÌâµÄ½â¾ö·½·¨Î廨°ËÃÅ£¬µ«¶¼²»ÊǺÜ×àЧ£¡Æäʵ½â¾ö·½·¨·Ç³£¼òµ¥£º µÚÒ»²½£º±àÂ루ÒÔ´«µÝµÄ²ÎÊýΪstrΪÀý£© ÔÚ´«µÝÊý¾ÝÇ°½«str½øÐбàÂëJava.net.URLEncoder.encode(str) µÚ¶þ²½£º½âÂë ÔÚ»ñÈ¡Êý¾Ý¶Ë½«µÃµ½µÄÊý¾Ý½øÐнâÂë new String£¨str.getBytes("ISO8859_1")£©
......
ÔÚjspÖв»Óö¨Òå¿ÉÒÔÖ±½ÓÓõľÍÊÇjspµÄ9¸öÄÚ½¨¶ÔÏóÁË
1£¬Request 2,Response 3,session 4,out 5,page
6,application 7,pageContext 8,config
9,exception
1¡¢Request¶ÔÏó
¸Ã¶ÔÏó·â×°ÁËÓû§Ìá½»µÄÐÅÏ¢£¬Í¨¹ýµ÷ÓøöÔÏóÏàÓ¦µÄ·½·¨¿ÉÒÔ»ñÈ¡·â×°µÄÐÅÏ¢£¬¼´Ê¹ÓøöÔÏó¿ÉÒÔ
»ñÈ¡Óû§Ìá½»µÄÐÅÏ¢¡£
µ±Request¶ÔÏó»ñÈ¡¿Í» ......
¹ØÓÚJSPÖÐorg.apache.jasper.JasperException:
This absolute uri http://java.sun.com/jsp/jstl/core ) cannot be resolved in either web.xml or the jar files deployed with this application
´ÓÍøÉÏËÑÁËһϣ¬ÏàͬµÄ˵·¨ÊÇ£ºÒ»¸ö¾ÍÊÇJSTLµÄ°æ±¾ÎÊÌ⣨1.0Óë1.1£©£¬ÊÇ·ñ²»Ò»Ö ......
4.1 ±íµ¥Êý¾Ý¸ÅÊö
¡¡¡¡ Èç¹ûÄãÔø¾Ê¹ÓùýWebËÑË÷ÒýÇ棬»òÕßä¯ÀÀ¹ýÔÚÏßÊéµê¡¢¹ÉƱ¼Û¸ñ¡¢»úƱÐÅÏ¢£¬»òÐí»áÁôÒ⵽һЩ¹Å¹ÖµÄURL£¬±ÈÈç“http://host/path?user=Marty+Hall&origin=bwi&dest=lax”¡£Õâ¸öURLÖÐλÓÚÎʺźóÃæµÄ²¿·Ö£¬¼´“user=Marty+Hall&origin=bwi&dest=lax”£¬¾ÍÊÇ±íµ¥Êý ......