jsp Servlet ´¦Àí±íµ¥Êý¾Ý
4.1 ±íµ¥Êý¾Ý¸ÅÊö
¡¡¡¡ Èç¹ûÄãÔø¾Ê¹ÓùýWebËÑË÷ÒýÇæ£¬»òÕßä¯ÀÀ¹ýÔÚÏßÊéµê¡¢¹ÉƱ¼Û¸ñ¡¢»úƱÐÅÏ¢£¬»òÐí»áÁôÒ⵽һЩ¹Å¹ÖµÄURL£¬±ÈÈç“http://host/path?user=Marty+Hall&origin=bwi&dest=lax”¡£Õâ¸öURLÖÐλÓÚÎʺźóÃæµÄ²¿·Ö£¬¼´“user=Marty+Hall&origin=bwi&dest=lax”£¬¾ÍÊÇ±íµ¥Êý¾Ý£¬ÕâÊǽ«WebÒ³ÃæÊý¾Ý·¢Ë͸ø·þÎñÆ÷³ÌÐòµÄ×î³£Ó÷½·¨¡£¶ÔÓÚGETÇëÇó£¬±íµ¥Êý¾Ý¸½¼Óµ½URLµÄÎʺźóÃæ£¨ÈçÉÏÀýËùʾ£©£»¶ÔÓÚPOSTÇëÇó£¬±íµ¥Êý¾ÝÓÃÒ»¸öµ¥¶ÀµÄÐз¢Ë͸ø·þÎñÆ÷¡£
¡¡¡¡ ÒÔǰ£¬´ÓÕâÖÖÐÎʽµÄÊý¾ÝÌáÈ¡³öËùÐèÒªµÄ±íµ¥±äÁ¿ÊÇCGI±à³ÌÖÐ×îÂé·³µÄÊÂÇéÖ®Ò»¡£Ê×ÏÈ£¬GETÇëÇóºÍPOSTÇëÇóµÄÊý¾ÝÌáÈ¡·½·¨²»Í¬£º¶ÔÓÚGETÇëÇó£¬Í¨³£ÒªÍ¨¹ýQUERY_STRING»·¾³±äÁ¿ÌáÈ¡Êý¾Ý£»¶ÔÓÚPOSTÇëÇó£¬ÔòÒ»°ãͨ¹ý±ê×¼ÊäÈëÌáÈ¡Êý¾Ý¡£µÚ¶þ£¬³ÌÐòÔ±±ØÐ븺ÔðÔÚ“&”·ûºÅ´¦½Ø¶Ï±äÁ¿Ãû×Ö-±äÁ¿Öµ¶Ô£¬ÔÙ·ÖÀë³ö±äÁ¿Ãû×Ö£¨µÈºÅ×ó±ß£©ºÍ±äÁ¿Öµ£¨µÈºÅÓұߣ©¡£µÚÈý£¬±ØÐë¶Ô±äÁ¿Öµ½øÐÐURL·´±àÂë²Ù×÷¡£ÒòΪ·¢ËÍÊý¾ÝµÄʱºò£¬×ÖĸºÍÊý×ÖÒÔÔÀ´µÄÐÎʽ·¢ËÍ£¬µ«¿Õ¸ñ±»×ª»»³É¼ÓºÅ£¬ÆäËû×Ö·û±»×ª»»³É“%XX”ÐÎʽ£¬ÆäÖÐXXÊÇÊ®Áù½øÖƱíʾµÄ×Ö·ûASCII£¨»òÕßISO Latin-1£©±àÂëÖµ¡£ÀýÈ磬Èç¹ûHTML±íµ¥ÖÐÃûΪ“users”µÄÓòֵΪ“~hall, ~gates, and ~mcnealy”£¬Ôòʵ¼ÊÏò·þÎñÆ÷·¢Ë͵ÄÊý¾ÝΪ“users=%7Ehall%2C+%7Egates%2C+and+%7Emcnealy”¡£×îºó£¬¼´µÚËĸöµ¼Ö½âÎö±íµ¥Êý¾Ý·Ç³£À§ÄѵÄÔÒòÔÚÓÚ£¬±äÁ¿Öµ¼È¿ÉÄܱ»Ê¡ÂÔ£¨Èç“param1=val1£¦param2=£¦param3=val3”£©£¬Ò²ÓпÉÄÜÒ»¸ö±äÁ¿ÓµÓÐÒ»¸öÒÔÉϵÄÖµ£¬¼´Í¬Ò»¸ö±äÁ¿¿ÉÄܳöÏÖÒ»´ÎÒÔÉÏ£¨Èç“param1=val1£¦param2=val2£¦param1=val3”£©¡£
¡¡¡¡ Java ServletµÄºÃ´¦Ö®Ò»¾ÍÔÚÓÚËùÓÐÉÏÊö½âÎö²Ù×÷¶¼Äܹ»×Ô¶¯Íê³É¡£Ö»ÐèÒª¼òµ¥µØµ÷ÓÃÒ»ÏÂHttpServletRequestµÄgetParameter·½·¨¡¢ÔÚµ÷ÓòÎÊýÖÐÌṩ±íµ¥±äÁ¿µÄÃû×Ö£¨´óСдÃô¸Ð£©¼´¿É£¬¶øÇÒGETÇëÇóºÍPOSTÇëÇóµÄ´¦Àí·½·¨ÍêÈ«Ïàͬ¡£
¡¡¡¡ getParameter·½·¨µÄ·µ»ØÖµÊÇÒ»¸ö×Ö·û´®£¬ËüÊDzÎÊýÖÐÖ¸¶¨µÄ±äÁ¿Ãû×ÖµÚÒ»´Î³öÏÖËù¶ÔÓ¦µÄÖµ¾·´±àÂëµÃµ½µÃ×Ö·û´®£¨¿ÉÒÔÖ±½ÓʹÓã©¡£Èç¹ûÖ¸¶¨µÄ±íµ¥±äÁ¿´æÔÚ£¬µ«Ã»ÓÐÖµ£¬getParameter·µ»Ø¿Õ×Ö·û´®£»Èç¹ûÖ¸¶¨µÄ±íµ¥±äÁ¿²»´æÔÚ£¬Ôò·µ»Ønull¡£Èç¹û±íµ¥±äÁ¿¿ÉÄܶÔÓ¦¶à¸öÖµ£¬¿ÉÒÔÓÃgetParameterValuesÀ´È¡´úgetParameter¡£getParameterValuesÄܹ»·µ»ØÒ»¸ö×Ö·û´®Êý×é¡£
¡¡¡¡ ×îºó£¬ËäÈ»ÔÚʵ
Ïà¹ØÎĵµ£º
ÔÚjspÖв»Óö¨Òå¿ÉÒÔÖ±½ÓÓõľÍÊÇjspµÄ9¸öÄÚ½¨¶ÔÏóÁË
1£¬Request 2,Response 3,session 4,out 5,page
6,application 7,pageContext 8,config
9,exception
1¡¢Request¶ÔÏó
¸Ã¶ÔÏó·â×°ÁËÓû§Ìá½»µÄÐÅÏ¢£¬Í¨¹ýµ÷ÓøöÔÏóÏàÓ¦µÄ·½·¨¿ÉÒÔ»ñÈ¡·â×°µÄÐÅÏ¢£¬¼´Ê¹ÓøöÔÏó¿ÉÒÔ
»ñÈ¡Óû§Ìá½»µÄÐÅÏ¢¡£
µ±Request¶ÔÏó»ñÈ¡¿Í» ......
ÉÏÍø¿´Á˺ö࣬¾ÍÕâһƪ½â¾öÁËÎÒµÄÎÊÌ⣬ÏÖת£¬ÎÒµÄÏîÄ¿ÓõÄÊÇMVC¼Ü¹¹£¬ÓÐͳһµÄ¿ØÖÆÆ÷£¬×ªÏò²»Í¬µÄAction£¬ÉÏÍø¿´Á˺ö࣬´ó²¿·ÖÊÇ˵¼ÓÈërequest.setCharacterEncoding("utf-8");µ«ÊǾ¹ýÎÒµÄÊÔÑ飬ÕâÖ»ÓÐÔÚÀûÓÃJSP´¦Àí±íµ¥´«ÊäÊý¾Ýʱ²Å¿ÉÓã¬ÔÚÎҵĶ«Î÷ÖÐÎÞ·¨½â¾ö£¬ºóÀ´·¢ÏÖ»¹ÊÇÓÃFilterChainºÃ£¬ºÇºÇ£¬Öص㻹ÊÇÔÚrequest ......
ÕÒÏÂÃæÐ´·¨¡£
×¢Ò⣬¼Óµ¥ÒýºÅ£¬µÚ2¸ö°Ù·ÖºÅ֮ǰûÓпոñ£¬¶øÔÚjspÖе÷ÓõĻ°Òª¿Õ¸ñ¡£
You may be get some help from the code below.
<%
String str="str";
%>
<script>
function accessVar(){
var varStr='<%=str%>';
alert(varStr);// here will diplay ......
JSPÎļþÏÂÔØ-----------------------------------------------------------------
<%@ page contentType="text/html; charset=GBK"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.io.File"%>
<%@page import="java.io.InputStream"%>
<%@page ......
1.RequestDispatcher.forward()
¡¡¡¡ ÊÇÔÚ·þÎñÆ÷¶ËÆð×÷ÓÃ,µ±Ê¹ÓÃforward()ʱ,Servlet engine´«µÝHTTPÇëÇó´Óµ±Ç°µÄServlet or JSPµ½ÁíÍâÒ»¸öServlet,JSP »òÆÕͨHTMLÎļþ,Ò²¼´ÄãµÄformÌá½»ÖÁa.jsp,ÔÚa.jspÓõ½ÁËforward()ÖØ¶¨ÏòÖÁb.jsp,´ËʱformÌá½»µÄËùÓÐÐÅÏ¢ÔÚ b.jsp¶¼¿ÉÒÔ»ñµÃ,²ÎÊý×Ô¶¯´«µÝ.
¡¡¡¡µ«forward()ÎÞ·¨Öض¨ÏòÖ ......