杞?
1 B/S concept
B/S缁撴瀯锛圔rowser/Server缁撴瀯锛夌粨鏋勫嵆娴忚鍣ㄥ拰鏈嶅姟鍣ㄧ粨鏋勩€?
瀹冩槸闅忕潃Internet鎶€鏈殑鍏磋捣锛屽C/S缁撴瀯鐨勪竴绉嶅彉鍖栨垨鑰呮敼杩涚殑缁撴瀯銆?
2 How to develop a WebApp.
1)WebApp鐨勭粍鎴?绗﹀悎JAVAEE瑙勮寖).
2)渚嬪瓙.
firstWeb(webroot)
/WEB-INF/web.xml
/index.html
/student.html
3) 閮ㄧ讲鍒板簲鐢ㄦ湇鍔″櫒.
閫氳繃url璁块棶:
http://localhost:8080/firstWeb/
or
http://127.0.0.1:8080/firstWeb/
鎬濊€冿細閫氳繃url璁块棶index.html鍜岀洿鎺ョ敤娴忚鍣ㄦ墦寮€index.html鏂囦欢鏁堟灉涓€鏍蜂箞锛?
鍏惰繃绋嬫湁浣曞樊鍒紵
3 HTTP introduction.
HTTP锛圚yper Text Transfer Protocol锛夋槸瓒呮枃鏈紶杈撳崗璁殑缂╁啓锛?
瀹冪敤浜庝紶閫乄WW鏂瑰紡鐨勬暟鎹€侶TTP鍗忚閲囩敤浜嗚姹?鍝嶅簲妯″瀷銆?
瀹㈡埛绔悜鏈嶅姟鍣ㄥ彂閫佷竴涓姹傦紝璇锋眰澶村寘鍚姹傜殑鏂规硶銆乁RI銆佸崗璁増鏈€?
浠ュ強鍖呭惈璇锋眰淇グ绗︺€佸鎴蜂俊鎭拰鍐呭鐨勭被浼间簬 MIME鐨勬秷鎭粨鏋勩€?
request:
<!--start-->
GET /firstWeb/ HTTP/1.1
Accept: **
Referer: http://localhost:8090/guessWeb/
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
Host: 127.0.0.1
Connection: Keep-Alive
<!--end-->
POST--->閫氳繃Http Body鎻愪氦琛ㄥ崟淇℃伅锛屼紶杈撶殑鏁版嵁閲忓ぇ锛屽彲浠ヨ揪鍒?M銆?
<!--post鏂规硶鎻愪氦琛ㄥ崟-->
POST /guessWeb/guess HTTP/1.1
Host: 127.0.0.1
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,**
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
Host: 192.168.2.53:8082
Connection: Keep-Alive
Cookie: userName=Tommy; classNo=SD0702
渚嬪瓙锛氭樉绀烘墍鏈夌殑Cookie
ListCookieServlet
涓夌绫诲瀷鐨刢ookie
瀵垮懡
setMaxAge:0 涓嶆敮鎸丆ookie
璐熸暟锛屼細璇滳ookie锛屼繚鎸佸湪娴忚鍣紝涓嶅啓鏂囦欢
鏁存暟锛屾櫘閫欳ookie锛屽啓鏂囦欢
娉ㄦ剰锛欳ookie闇€瑕佸鎴风娴忚鍣ㄧ殑鏀寔锛?
濡傛灉娴忚鍣ㄧ鐢–ookie鐨勮瘽锛屾垜浠殑鎵€鏈塁ookie灏辨棤鏁?
2 Trace state:Session.
Session:鏈嶅姟鍣ㄧ淇濆瓨鐘舵€佺殑鏈哄埗锛岄€氳繃HttpSession瀵硅薄鏉ヨ褰曚俊鎭?
Session缂栫▼:
鑾峰緱Session: request.getSession() getSession(boolean b)
request.getSession()涓巊etSession(true)绛変环锛?
娌℃湁session瀵硅薄鏃跺€欒嚜鍔ㄥ垱寤簊ession瀵硅薄
getSession(false):娌℃湁session瀵硅薄鏃跺€欒繑鍥瀗ull锛屼笉浼氳嚜鍔ㄥ垱寤簊ession瀵硅薄
Session瀛樹俊鎭? HttpSession.setAttribute(String name,Object o)
Session鍙栦俊鎭? Object HttpSession.getAttribute()
璁剧疆Session鏈夋晥鏃堕棿:(鍗曚綅绉掞級
HttpSession.setMaxInactiveInterval(int interval)
璁剧疆session鏃犳晥鐨勬椂闂达紝璐熸暟琛ㄧず姘镐笉杩囨椂銆?
鎵嬪伐閿€姣丼ession: HttpSession.invalidate()
鍒ゆ柇Session鏄惁鏂板缓: boolean HttpSession.isNew()
鑾峰緱SessionID: HttpSession.getId()
鐢ㄦ埛璁块棶璁℃暟(SessionVisitorServlet):session鐗堟湰
绗?00鍚嶈闂€呭鍔变竴涓皬绀煎搧
瀹㈡埛绔淮鎶essionID:
1)閫氳繃Cookie鏉ョ淮鎶essionID
Web Server鐢熸垚Session瀵硅薄鍚庯紝姣忔浣渞esponse鏃讹紝浼氳嚜鍔ㄦ妸SessionID鍙戝洖鍒板鎴风锛?
瀹㈡埛绔妸SessionID璁板叆浼氳瘽Cookie,涓嬫璇锋眰鏃惰嚜鍔ㄥ甫涓?
鏈嶅姟鍣ㄧ涓€娆$殑鐩稿簲浼氱敓鎴愪竴涓猻ession id锛屽彂閫佸埌瀹㈡埛绔?
HTTP response:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=46C8177FB3EAC6F7FD868E6FA147E9CD; Path=/sessionWeb
Content-Type: text/html;charset=gbk
Transfer-Encoding: chunked
Date: Fri, 18 May 2007 03:54:31 GMT
+++++++++++++++++++++++++++++++++++++++++++++++++++++
涓嬩竴娆″鎴风鐨勮姹備細甯︿笂浠ookie鐨勫舰寮弒ession id
HTTP request:
GET /sessionWeb/visitor HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
Host: 192.168.2.53:8082
Connection: Keep-Alive
Cookie: JSESSIONID=46C8177FB3EAC6F7FD868E6FA147E9CD
2)閲嶅啓URL鏉ョ淮鎶essionID锛堝綋瀹㈡埛绔樆姝ookie鏃讹紝鍙噰鐢ㄨ繖绉嶆柟寮忕淮鎶essionid锛?
鎶妔essionID鍔犲埌璇锋眰鐨刄RL涓?
out.println("<a href="+response.encodeURL(" target="_blank" rel="external nofollow" visitor")+">access again</a>");
http://192.168.2.53:8082/sessionWeb/visitor;jsessionid=1CA21297D00524960931F00F69FF7A4A
閿€姣丼ession:
1)session.invalidate()
2)
a session.setMaxInactiveInterval(int interval)(a浼樺厛绾ч珮浜巄鏂规硶锛?1800
涔熷彲閫氳繃web.xml鏉ヨ鏈€闀夸笉婵€娲绘椂闂?
b <session-config>
<session-timeout>30</session-timeout>
</session-config>
default value: 60min
3)Web Server crash(鏈嶅姟鍣ㄥ畾鍒讹紝webapp琚仠姝紝session閮戒細琚攢姣併€?
3
缁存姢鐘舵€佺殑鏂瑰紡
1)Cookie
2)Session
3)闅愯棌琛ㄥ崟鍩?
<input type="hidden" name="times" value=""/>
4)URL閲嶅啓
http://localhost:8080/visitorWeb/visitor?name=Alice&age=23
1 Filter.
2 Listener.
3 exercise.
*************************************************************
杩囨护鍣?
1. 浠€涔堟槸杩囨护鍣?
绛旓細涓嶴ervlet鐩镐技锛岃繃婊ゅ櫒鏄竴浜沇eb搴旂敤绋嬪簭缁勪欢锛屽彲浠ョ粦瀹氬埌涓€涓猈eb搴旂敤绋嬪簭妗f涓€備絾鏄笌鍏朵粬Web搴旂敤绋嬪簭缁勪欢涓嶅悓鐨勬槸锛岃繃婊ゅ櫒鏄€滈摼鈥濆湪瀹瑰櫒鐨勫鐞嗚繃绋嬩腑鐨勩€傝繖灏辨剰鍛崇潃瀹冧滑浼氬湪servlet澶勭悊鍣ㄤ箣鍓嶈闂竴涓繘鍏ョ殑璇锋眰锛屽苟涓斿湪澶栧彂鐨勫搷搴斾俊鎭繑鍥炲埌瀹㈡埛鍓嶈闂繖浜涘搷搴斾俊鎭€傝繖绉嶈闂娇寰楄繃婊ゅ櫒鍙互妫€鏌ュ苟淇敼璇锋眰鍜屽搷搴旂殑鍐呭銆?
2. 杩囨护鍣ㄥ彲浠ョ敤浜庯細
绛旓細1) 涓轰竴涓猈eb搴旂敤绋嬪簭鐨勬柊鍔熻兘寤虹珛鍘熷瀷(鍙娣诲姞鍒癢eb搴旂敤绋嬪簭涓垨鑰呬粠Web搴旂敤绋嬪簭涓垹闄よ€屼笉闇€閲嶅啓鍩哄眰搴旂敤绋嬪簭浠g爜);
2) 鍚戣繃鍘荤殑浠g爜涓坊鍔犳柊鍔熻兘銆?
3. 杩囨护鍣ㄦ斁鍦ㄥ鍣ㄧ粨鏋勪粈涔堜綅缃紵
绛旓細杩囨护鍣ㄦ斁鍦╓eb璧勬簮涔嬪墠锛屽彲浠ュ湪璇锋眰鎶佃揪瀹冩墍搴旂敤鐨刉eb璧勬簮(鍙互鏄竴涓猻ervlet銆佷竴涓狫SP椤甸潰锛岀敋鑷虫槸涓€涓狧TML椤甸潰杩欐牱鐨勯潤鎬佸唴瀹?涔嬪墠鎴幏杩涘叆鐨勮姹傦紝骞朵笖鍦ㄥ畠杩斿洖鍒板鎴蜂箣鍓嶆埅鑾疯緭鍑鸿姹傘€?
4. 杩囨护鍣ㄧ殑瀛樻椿鍛ㄦ湡
绛旓細杩囨护鍣ㄦ湁鍥涗釜闃舵(涓巗ervlet绫讳技)锛?
1) 瀹炰緥鍖?
2) 鍒濆鍖?璋冪敤init()鏂规硶);
3) 杩囨护(璋冪敤doFilter()鏂规硶);
4) 閿€姣?璋冪敤destroy()鏂规硶);
5. 杩囨护鍣ㄧ被鍜屾帴鍙?
绛旓細鎵€鏈夌殑杩囨护鍣ㄩ兘蹇呴』瀹炵幇javax.servlet.Filter鎺ュ彛:
1) 瀹瑰櫒璋冪敤init()鏂规硶鍒濆鍖栬繃婊ゅ櫒瀹炰緥锛?
public void init(FilterConfig config) throws ServletException
2) doFilter()鏂规硶鍖呭惈杩囨护鍣ㄩ€昏緫锛?
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
3) destroy()鏂规硶鐢卞鍣ㄥ湪閿€姣佽繃婊ゅ櫒瀹炰緥涔嬪墠璋冪敤锛?
public void destroy();
4) FilterChain鐨刣oFilter()鏂规硶涔嬪悗鐨勪唬鐮佹瀯鎴愪簡鍚庢湡澶勭悊杩囨护鍣ㄨ皟鐢ㄣ€?
6. 閰嶇疆杩囨护鍣?
绛旓細浣跨敤<filter>鍜?lt;filter-mapping>鍏冪礌鏉ラ厤缃細
<filter>
<filter-name>XSLTFilter</filter-name> //杩囨护鍣ㄥ悕
<filter-class>filters.SmartXSLFilter</filter-class> //鍏蜂綋杩囨护鍣ㄧ被
<init-param> //鍒濆鍖栧弬鏁?
<param-name>xsltfile</param-name>
<param-value>/xsl/stockquotes.xsl</param-value>
</init-param>
</filter>
<filter-mapping> //灏嗚繃婊ゅ櫒搴旂敤浜嶹eb搴旂敤绋嬪簭涓殑姣忎釜Web璧勬簮
<filter-name>Logger</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
濡備綍寮€鍙慒ilter:
1)implement javax.servlet.Filter
2)鐢熷懡鍛ㄦ湡鏂规硶
init()
doFilter()
destroy()
3)filter鍦╳ebapp鍚姩鏃跺€欒瀹炰緥鍖栵紝瀹瑰櫒浼氭墽琛屽叾init()鏂规硶銆?
4)filter request, response
public void doFilter(ServletRequest request, ServletResponse response,FilterChain filterChain) {
try {
//filter request
request.setCharacterEncoding("gbk");
//妫€鏌ilter鏄笉鏄悓涓€涓疄渚?
System.out.println("process request:"+this);
filterChain.doFilter(request, response);
//filter response
System.out.println("process response:"+this);
}
catch (ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
}
catch (IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
}
5)deploy filter in web.xml
<filter>
<filter-name>encodingfilter</filter-name>
<filter-class>filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
6)澶氫釜filter鐨勬墽琛屾搴?
鏍规嵁 <filter-mapping> 鐨勫厛鍚庢搴忓喅瀹?
web.xml
<filter-mapping>
<filter-name>encodefilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>visitorfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
鎶婃墍鏈夎姹傚鎴风鐨処P鍦板潃鍜岃姹傜殑鏃堕棿鍐欏叆鏃ュ織鏂囦欢銆?
鐢ㄦ埛鎶曠エ
渚嬪瓙锛?
鎯版€ц璇?
淇濇姢鏌愪簺椤甸潰
if (role == null || !role.equals("admin")) {
try {
res.sendRedirect("../login.html");
}
catch (IOException ex) {
}
}
try {
filterChain.doFilter(request, response);
}
catch (ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
}
catch (IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
2 Listener.
Listener:
--->ServletContext,HttpSession(2.3) ServletRequest(2.4)
1)implements ServletContextListener.
2)override listener method.
3)鍦╳ebapp鍚姩鏄瀹炰緥鍖栥€?
4)web.xml
<listener>
<listener-class>com.MyListener</listener-class>
</listener>
渚嬪瓙锛?
Web搴旂敤绋嬪簭鐢熷懡鍛ㄦ湡浜嬩欢鍙婄洃鍚櫒(Servlet V2.3鐗堟湰浠ュ悗鏂板鍔熻兘)
1. 浠€涔堟槸浜嬩欢鐩戝惉鍣?
绛旓細1) 鏀寔ServletContext銆丠ttpSession(since v2.3)鍙奡ervletRequest(since v2.4)涓姸鎬佹敼鍙樼殑浜嬩欢閫氱煡;
2) 瀹炵幇浜嗕竴涓垨澶氫釜servlet浜嬩欢鐩戝惉鍣ㄦ帴鍙g殑绫诲瀷;
3) 鎺у埗ServletContext銆丠ttpSession(since v2.3)鍙奡ervletRequest(since v2.4)涓殑鐢熷懡鍛ㄦ湡;
2. Servlet Context浜嬩欢鐩戝惉鍣?
绛旓細1) 瀵逛簬搴旂敤绋嬪簭鑰岃█鍦↗VM灞傚埆绠$悊璧勬簮鎴栦繚瀛樼姸鎬?
2) 鏈変簩绉嶇被鍨嬬殑浜嬩欢鐩戝惉鍣細
a. ServletContextListener(浠ヤ笅鏄鐩戝惉鍣ㄧ殑鏂规硶)
contextDestroyed(ServletContextEvent sce)
contextInitialized(ServletContextEvent sce)
b. ServletContextAttributeListener(浠ヤ笅鏄鐩戝惉鍣ㄧ殑鏂规硶)
attributeAdded(ServletContextAttributeEvent scab)
attributeRemoved(ServletContextAttributeEvent scab)
attributeReplaced(ServletContextAttributeEvent scab)
3. HTTP Session浜嬩欢鐩戝惉鍣?
绛旓細1) 绠$悊浠庡悓涓€涓鎴风鎴栫敤鎴峰悜涓€涓猈eb搴旂敤绋嬪簭鍙戝嚭鐨勪竴绯诲垪璇锋眰鐩稿叧鐨勭姸鎬佹垨璧勬簮;
2) 鏈変簩绉嶇被鍨嬬殑浜嬩欢鐩戝惉鍣?
a. HttpSessionListener(浠ヤ笅鏄鐩戝惉鍣ㄧ殑鏂规硶)
sessionCreated(HttpSessionEvent se)
sessionDestroyed(HttpSessionEvent se)
b. HttpSessionAttributeListener(浠ヤ笅鏄鐩戝惉鍣ㄧ殑鏂规硶)
attributeAdded(HttpSessionBindingEvent se)
attributeRemoved(HttpSessionBindingEvent se)
attributeReplaced(HttpSessionBindingEvent se)
4. Servlet Requst浜嬩欢鐩戝惉鍣?
绛旓細1) 绠$悊鏁翠釜request鐢熷懡鍛ㄦ湡鐨勭姸鎬?
2) 鏈変簩绉嶇被鍨嬬殑浜嬩欢鐩戝惉鍣?
a. ServletRequestListener(浠ヤ笅鏄鐩戝惉鍣ㄧ殑鏂规硶)
requestDestroyed(ServletRequestEvent sre)
requestInitialized(ServletRequestEvent sre)
b. ServletRequestAttributeListener(浠ヤ笅鏄鐩戝惉鍣ㄧ殑鏂规硶)
attributeAdded(ServletRequestAttributeEvent srae)
attributeRemoved(ServletRequestAttributeEvent srae)
attributeReplaced(ServletRequestAttributeEvent srae)
5. 鐩戝惉鍣ㄧ被鐨勮瀹?
绛旓細1) 蹇呴』鍦ㄩ儴缃叉弿杩扮涓厤缃疄鐜扮被;