天天看点

servlet原理

杞?

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) 蹇呴』鍦ㄩ儴缃叉弿杩扮涓厤缃疄鐜扮被;