JSPÒ³Ãæ´ÓÐÎʽÉϾÍÊÇÔÚ´«Í³µÄÍøÒ³HTMLÎļþÖмÓÈëJava³ÌÐòƬ¶Î£¨Scriptlet£©ºÍJSP±êÇ©¡£Servlet/JSPÈÝÆ÷ÊÕµ½¿Í»§¶Ë·¢³öµÄÇëÇóʱ£¬Ê×ÏÈÖ´ÐÐÆäÖеijÌÐòƬ¶Î£¬È»ºó½«Ö´Ðнá¹ûÒÔHTML¸ñʽÏìÓ¦¸ø¿Í»§¶Ë¡£
»ù±¾Óï·¨£º
×¢ÊÍ£º
1¡¢¿Í»§¶Ë¿ÉÒÔ¿´µ½µÄ×¢ÊÍ£º<!-- comment [<%=expression%>] --> (¿ÉÒÔÔÚÅú×¢ÖмÓÉ϶¯Ì¬µÄ±í´ïʽ)
2¡¢¿ª·¢³ÌÐòԱרÓõÄ×¢ÊÍ£º<%-- comment --%> »òÕß <%/** this is a comment **/%>
ÉùÃ÷£º
<%!declaration; [declaration;]+...%>
¸ÃÉùÃ÷µÄ±äÁ¿ÎªÀà±äÁ¿¡£Òâζ×Å£ºÈôͬʱÓÐn¸öÓû§ÔÚÖ´ÐдËJSPÍøҳʱ£¬ËûÃǽ«»á¹²Ïí´Ë±äÁ¿¡£»áÒýÆðÏß³Ìͬ²½ÉϵÄÂé·³¡££¨²»½¨ÒéʹÓã©
С½Å±¾£º
<% Java ´úÂë%>
´ËÀඨÒåµÄ±äÁ¿Îª¾Ö²¿±äÁ¿£¬²»´æÔÚ¶àÏß³Ìͬ²½ÎÊÌ⣬ÊÇḬ̈߳²È«µÄ¡£
±í´ïʽ£º
<%=expression %> ²»ÄÜʹÓá°£»¡±×÷Ϊ±í´ïʽµÄ½áÊø·ûºÅ¡£
JSPÖ¸ÁîÔªËØ£º
Ö»ÊǸæËßJSPÒýÇæ¶ÔJSPÒ³ÃæÈçºÎ±àÒë¡£
pageÖ¸ÁÉ趨Õû¸öJSPÍøÒ³µÄÊôÐÔºÍÏà¹Ø¹¦ÄÜ¡£
<%@page attribute1="value1" attribute2="value2" ...%> <jsp:directive.page attribute1="value1"...>ÀýÈ磺
<%@page contentType="text/html; charset=utf-8" language="java" import="java.net.*"%>includeÖ¸ÁÓÃÓÚָʾJSPÈÝÆ÷ÔÚJSP±àÒëʱ²åÈëÒ»¸ö°üº¬Îı¾»ò´úÂëµÄÎļþ£¬Õâ¸ö°üº¬µÄ¹ý³ÌÊǾ²Ì¬µÄ¡£×¢Òâ°üº¬ÎļþÖÐÒª±ÜÃâʹÓÃ<html><body>£¬¿ÉÄܻᵼÖ´íÎó¡£
<%@include file="relativeURLspec"%> <jsp:directive.include file="relativeURLspec"> ÀýÈ磺 <%@include file="Hello.html"> taglibÖ¸Á <%@taglib uri="tagLibraryURI" prefic="tagPrefix"%> <jsp:directive.taglib uri="tagLibraryURI" prefic="tagPrefix">
JSP²Ù×÷ÔªËØ£º
<jsp:include> <jsp:forward> <jsp:param> <jsp:useBean> <jsp:setProperty> <jsp:getProperty>
request¶ÔÏóÓëÍøÂçÐÅÏ¢µÄÌá½»£º
JSPÖÐÌṩÁËrequest¶ÔÏóÀ´½ÓÊÕ¿Í»§¶Ë·¢ËÍ»ØÀ´µÄÊý¾Ý¡£
¡ôURLÒ»°ãÓï·¨£ºprotocol://host:port/virtualPath?queryString
¡ôprotocol£º±íʾÐÒ飬ÓÃÓÚÉùÃ÷ÔÚÔ¶³Ì»úÆ÷Ö®¼ä´«ËÍÐÅÏ¢µÄµÍ²ã»úÖÆ£¨http ftp httpsµÈ£©¡£
¡ôhost£º±íʾÇëÇóÒª·¢Ë͵½µÄÔ¶³Ì»úÆ÷µÄÃû³Æ»òÕßipµØÖ·¡£
¡ôport£ºÉùÃ÷·þÎñÆ÷Òª¼àÌýÇëÇóµÄ»úÆ÷¶Ë¿ÚºÅ¡£
¡ôvirtualPath£º°üº¬Ò»¸öÒÔбÏßΪ·Ö¸ô·ûµÄÒ»×é±êʶ·û£¬·þÎñÆ÷°ÑËüÓ³Éäµ½Ò»¸öÎïÀí·¾¶ºÍJSPµÄλÖá£
¡ôquerString£ºÊDzéѯ×Ö·û´®£¬ËüÊÇÒ»¸ö³É¶ÔµÄÃû³ÆºÍÖµµÄÁÐ±í£¬×÷ΪJSPµÄ²ÎÊý±»´«µÝµ½´¦ÀíËüµÄJSPÒ³Ãæ¡£Èç¹ûÓжà¸ö²ÎÊýÐèÒª´«µÝ£¬½«ÒÔ¡°&¡±ºÅ·Ö¸ô¿ª³É¶ÔµÄÃû³ÆºÍÖµ¡£
request¶ÔÏóËùÌṩµÄ·½·¨£º
»ñȡ·¾¶ºÍÐÒéÐÅÏ¢£º
String getProtocol() »ñȡͨÐÅËùʹÓõÄÐÒéºÍ°æ±¾ºÅ£¨È磺HTTP /1.1£© String getScheme() »ñÈ¡ÇëÇóÖеÄÐÒéÃû³Æ£¨È磺HTTP£© String getPathInfo() »ñÈ¡ÇëÇóÖд¦ÓÚJSP·¾¶ºÍ²éѯ×Ö·û´®Ö®¼äµÄ¶îÍâÐÅÏ¢ String getContextPath() String getRequestURI() String getServletPath() String getRealPath(String path)»ñÈ¡Ö÷»úºÍ¶Ë¿ÚÐÅÏ¢£º
String getServerName() »ñÈ¡ÏìÓ¦ÇëÇóµÄ·þÎñÆ÷Ãû³Æ¡£ int getServerPort() »ñÈ¡ÏìÓ¦ÇëÇóµÄ·þÎñÆ÷¶ËÖ÷»ú¶Ë¿ÚºÅ¡£ String getLocalName() »ñÈ¡ÏìÓ¦ÇëÇóµÄ·þÎñÆ÷¶ËÖ÷»úÃû¡£ String getLocalAddr() »ñÈ¡ÏìÓ¦ÇëÇóµÄ·þÎñÆ÷¶ËµØÖ·¡£ int getLocalPort() »ñÈ¡ÏìÓ¦ÇëÇóµÄ·þÎñÆ÷¶Ë¶Ë¿Ú¡£ String getRemoteAddr() »ñÈ¡·¢³öÇëÇóµÄ¿Í»§¶ËIPµØÖ·¡£ String getRemoteHost() »ñÈ¡·¢³öÇëÇóµÄ¿Í»§¶ËÖ÷»úÃû¡£ int getRemotePort() »ñÈ¡·¢³öÇëÇóµÄ¿Í»§¶ËÖ÷»ú¶Ë¿Ú¡£
»ñÈ¡²éѯ×Ö·û´®£º
String getQueryString() »ñÈ¡get()·½·¨´«µÝµÄ²ÎÊý×Ö·û´®£¬¼´URLÖУ¿ºóÃæµÄ²¿·Ö¡£
»ñÈ¡²éѯ×Ö·û´®ÖеıäÁ¿Öµ£º
String getParameter(String name) »ñÈ¡¿Í»§¶Ë·¢Ë͸ø·þÎñÆ÷¶ËµÄ²ÎÊýÖµ¡£ Enumeration getParameterNames() ·µ»ØÇëÇóÖÐËùÓвÎÊýµÄ¼¯ºÏ¡£ String[] getParameterValues(String name) »ñµÃÇëÇóÖÐÖ¸¶¨²ÎÊýµÄËùÓÐÖµ¡£ ÀýÈ磺 http://localhost:8080/liuxl/jsp/querystring.jsp?str=JSP String s=request.getParameter("str"); »ñÈ¡±äÁ¿strµÄÖµ£¬Ö´ÐкósµÄֵΪ¡°JSP¡±¡£×¢Ò⣺getParameter()·½·¨µÄ·µ»ØÖµÖ»ÄÜÊÇ×Ö·û´®¡£Èô´«µÝval=100£¬ÔòÐèÒª½øÐÐÀàÐÍת»»¡£
if(request.getParameter("val")!=null) //ÅжÏ×Ö·û´®ÊÇ·ñΪ¿Õ { num=Integer.parseInt(request.getParameter("val")); //½«×Ö·û´®×ª»¯ÎªÕûÊý } else { num=0; } if(request.getParameter("val")!=null) //ÅжÏ×Ö·û´®ÊÇ·ñΪ¿Õ { num=Integer.parseInt(request.getParameter("val")); //½«×Ö·û´®×ª»¯ÎªÕûÊý } else { num=0; }
±íµ¥¼°ÆäÔÚÐÅÏ¢´«µÝÖеÄÓ¦Óãº
±íµ¥:
ÊÇÒ»¸öÄܹ»°üº¬±íµ¥ÔªËصÄÇøÓò£¬HTMLΪ±íµ¥ÌṩÁ˶àÖÖͼÐÎÓû§½çÃæ×é¼þÔªËØ £¨ÈçÎı¾¿ò£¬ÃÜÂë¿ò£¬ÏÂÀ²Ëµ¥µÈ£©£¬ÕâЩ±íµ¥ÔªËØÄܹ»ÈÃÓû§ÔÚ±íµ¥ÊäÈëÐÅÏ¢£¬¿ÉÒÔÓÃËüÃÇÀ´¹¹³É±íµ¥ÄÚÈÝ£¬×÷ΪÓû§¿ÉÒÔÊäÈëµÄÓò£¬²¢¿ÉÒÔ½«ÆäÌá½»¸øÒ»¸öJSP½øÐд¦Àí¡£
¸ñʽ£º
<form name="" action="" method=""> ... </form>
ÆäÖУº nameÓÃÓÚÖ¸¶¨±íµ¥µÄÃû³Æ¡£actionÓÃÓÚÖ¸¶¨±íµ¥±»ÌύʱËùÒªÖ´ÐеĶ¯×÷¡£methodÖ¸¶¨´«µÝÇëÇó¸øJSPËùÓõķ½·¨£¨GET¡¢POST¡¢PUT£¬³£ÓÃPOST£©¡£
1¡¢<input>±êÇ© ³£ÓÃÓÚÊäÈëÐÅÏ¢»òÌá½»ÐÅÏ¢
Óï·¨£º<input type="" name="" value="">
˵Ã÷£ºtypeÔÊÐíÖ¸¶¨ÊäÈëÀàÐÍ£¨text(Îı¾¿ò)¡¢password(ÃÜÂë¿ò)¡¢checkbox(¸´Ñ¡¿ò)¡¢radio(µ¥Ñ¡°´Å¥)¡¢ image(ͼÏñÓò)¡¢reset(ÖØÖð´Å¥)¡¢submit(Ìá½»°´Å¥)£©¡£ nameΪ±íµ¥ÔªËصÄÃû³Æ¡£valueΪ±íµ¥ÔªËصijõʼֵ¡£
ÀýÈ磺
Îı¾¿ò£º<input name="" type="text" value="" size="" maxlength=""> ÃÜÂë¿ò£º<input name="" type="password" value="" size="" maxlength=""> Ìá½»°´Å¥£º<input name="" type="submit" value=""> ÖØÖð´Å¥£º<input name="" type="reset" value=""> µ¥Ñ¡°´Å¥£º<input name="" type="radio" value="" checked> £¨ÓÃÓÚ´ÓÒ»×éÑ¡ÏîÖÐÑ¡ÔñÆäÖÐÒ»¸öÑ¡ÏcheckedÊÇ¿ÉÑ¡ÊôÐÔ£¬Èç¹ûÓиÃÊôÐÔ£¬Ôò±íʾ¸Ã°´Å¥ÏÔʾʱΪ±»Ñ¡ÖУ¬·ñÔòΪδѡÖС££© e.g:<input name="radiobutton" type="radio" value="1" checked>Ñ¡Ïî1 <input name="radiobutton" type="radio" value="2">Ñ¡Ïî2 ÔÚÑ¡Öа´Å¥1µÄÇé¿öÏÂÌá½»URLÈçÏ£º...?radiobutton=1... ¸´Ñ¡¿ò£º<input name="" type="checkbox" value="" checked>£¨ÓÃÓÚ´ÓÒ»×éÑ¡ÏîÖÐÑ¡Ôñ¶à¸öÑ¡ÏcheckedͬÉÏ¡£Ó뵥ѡ°´Å¥ÏàËÆ£¬¸´Ñ¡¿ò³£³É×éʹÓã¬Ó뵥ѡ°´Å¥²»Í¬µÄÊÇ£¬±»Ñ¡ÖеĶà¸ö¸´Ñ¡¿ò¶¼»á±»Ìá½»£¬Òò´ËÆäname¿ÉÒÔÏàͬ(°´Êý×é´¦Àí)£¬Ò²¿ÉÒÔ²»Í¬(°´²»Í¬±äÁ¿½øÐд¦Àí)£©
e.g:<input name="checkbox" type="checkbox" value="swim">swim <input name="checkbox" type="checkbox" value="run">run
Èç¹ûÔÚÌύʱÁ½¸ö¶¼Ñ¡ÖУ¬ÔòURLÐÎʽÈçÏ£º...?checkbox=swim&checkbox=run...
2¡¢<textarea>±êÇ© ÉùÃ÷ÁËÒ»¸öÓû§¿ÉÒÔÊäÈë¶àÐÐÎı¾µÄÇøÓò
Óï·¨£º<textarea name="" rows="" cols="" >text</textarea>
˵Ã÷£ºcolsΪÎı¾Óò¿í¶È£¨×Ö·ûÊý£©£¬rowsΪÎı¾ÓòµÄ¸ß¶È£¨ÐÐÊý£©
3¡¢<select>±êÇ© ÉùÃ÷ÁËÒ»¸ö¿ÉÑ¡ÏîµÄÁÐ±í£¬Óû§¿ÉÒÔÑ¡ÔñÒ»¸ö»ò¶à¸öÑ¡Ïî
<select name="" size="" multiple>
<option value="" selected>option</option>
...
<option value="">option</option>
</select>
˵Ã÷£ºnameÊÇÃû³Æ£¬sizeÊÇÁбíÇøÓò¸ß¶È£¨¼´¿ÉÏÔʾµÄÑ¡ÏîÊý£©£¬multipleÊÇ¿ÉÑ¡ÊôÐÔ£¬ÈôÓбíʾ¿ÉÑ¡Ôñ¶àÏî¡£selectedÊÇÒ»¸ö¿ÉÑ¡ÊôÐÔ£¬Óбíʾ¸ÃÑ¡ÏîÉèÖÃΪÒÑÑ¡ÖС£
ÍƼöÐÅÏ¢
- ÔÝÎ޼Ǽ
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- java urlÖØд¼¼Êõ(UrlReWriter)
- [ת]JavaµÄÊý×飨Array£©¡¢Vector¡¢ArrayLis....
- jsp»·¾³ÏÂajaxÂÒÂëÎÊÌâµÄ½â¾ö
- JSP¼¼Êõ Ö¸Áî ÂÒÂëÎÊÌâ ÔËÐÐÔÀíºÍ¾Å´óÒþʽ¶ÔÏó web¿ª·¢µÄË....
- JAVA ˮӡ ͼƬ ÎÄ×Ö
- java¶þ½øÖÆ£¬ÔÂë¡¢·´Âë¡¢²¹Â롢λÔËËã
- Java WebÓ¦ÓÃÆô¶¯¼ä¸ôÖ´ÐеijÌÐò
- jspÉú³ÉxmlÎļþʾÀý
- javaÖÐÀà±äÁ¿¡¢Àà·½·¨¡¢ÊµÀý±äÁ¿¡¢ÊµÀý·½·¨
- JSPÒ³ÃæµÄÎåÖÖÌøת·½·¨
ÆÀÂÛ