1.1?概述
監聽器: 主要是用來監聽特定對象的創建或銷毀、屬性的變化的!
?是一個實現特定接口的普通java類!
?
對象:
自己創建自己用 (不用監聽)
別人創建自己用 (需要監聽)
?
Servlet中哪些對象需要監聽?
request / session / servletContext
分別對應的是request監聽器、session相關監聽器、servletContext監聽器
?
監聽器(listener)
?
?
監聽器接口:
一、監聽對象創建/銷毀的監聽器接口
Interface ServletRequestListener?????監聽request對象的創建或銷毀
Interface HttpSessionListener????????監聽session對象的創建或銷毀
Interface ServletContextListener?????監聽servletContext對象的創建或銷毀
二、監聽對象屬性的變化
Interface ServletRequestAttributeListener?監聽request對象屬性變化: 添加、移除、修改
Interface HttpSessionAttributeListener????監聽session對象屬性變化: 添加、移除、修改
Interface ServletContextAttributeListener ?監聽servletContext對象屬性變化
?
三、session相關監聽器
Interface HttpSessionBindingListener???監聽對象綁定到session上的事件
????Interface HttpSessionActivationListener(了解) 監聽session序列化及反序列化的事件
?
?
404(路徑寫錯)
500(服務器錯誤,調試)
1.2?生命周期監聽器
聲明周期監聽器: 監聽對象的創建、銷毀的過程!
監聽器開發步驟:
1.?寫一個普通java類,實現相關接口;
2.?配置(web.xml)
?
A.??ServletRequestListener????
監聽request對象的創建或銷毀。
?
代碼:
/** ?* ?監聽request對象的創建或銷毀 ?* @author?Jie.Yuan ?* ?*/ public?class?MyRequestListener implements?ServletRequestListener{ ? // 對象銷毀 @Override public?void?requestDestroyed(ServletRequestEvent sre) { // 獲取request中存放的數據 Object obj = sre.getServletRequest().getAttribute("cn"); System.out.println(obj); System.out.println("MyRequestListener.requestDestroyed()"); } ? // 對象創建 @Override public?void?requestInitialized(ServletRequestEvent sre) { System.out.println("MyRequestListener.requestInitialized()"); } } |
? Web.xml <!-- 監聽request對象創建、銷毀 --> <listener> <listener-class>cn.itcast.a_life.MyRequestListener</listener-class> </listener> |
? |
?
?
B.?HttpSessionListener
監聽session對象的創建或銷毀。
?
C.?ServletContextListener
監聽servletContext對象的創建或銷毀。
?
?
1.3?屬性監聽器
監聽:request/session/servletContext對象屬性的變化!
ServletRequestAttributeListener
HttpSessionAttributeListener
ServletContextAttributeListener
?
總結:先寫類,實現接口; ?再配置
?
?
1.4 其他監聽器: session相關監聽器
?
?
HttpSessionBindingListener???
監聽對象綁定/解除綁定到sesison上的事件!
?
步驟:
對象實現接口; 再把對象綁定/解除綁定到session上就會觸發監聽代碼。
作用:
(上線提醒!)
?
/** ?* 監聽此對象綁定到session上的過程,需要實現session特定接口 ?* @author?Jie.Yuan ?* ?*/ public?class?Admin implements?HttpSessionBindingListener { ???…?省略get/set // 對象放入session @Override public?void?valueBound(HttpSessionBindingEvent event) { System.out.println("Admin對象已經放入session"); } // 對象從session中移除 @Override public?void?valueUnbound(HttpSessionBindingEvent event) { System.out.println("Admin對象從session中移除!"); } } ? |
?
思考:
這個session監聽器,和上面的聲明周期、屬性監聽器區別?
--à?不用再web.xml配置
???-à?因為監聽的對象是自己創建的對象,不是服務器對象!
?
?
?
1.4?案例
需求:做一個在線列表提醒的功能!
??????用戶--à?登陸
???????????????---à?顯示登陸信息,列表展示。(list.jsp)
???--à??顯示在線用戶列表 ???????(list.jsp)
-à?列表點擊進入“在線列表頁面” ??onlineuser.jsp
?
實現:
1.?先增加退出功能; ?再把session活躍時間1min;
2.?寫監聽器,監聽servletContext對象的創建: 初始化集合(onlineuserlist)
3.?登陸功能: 用戶登陸時候,把數據保存到servletContext中
4.?List.jsp ?增加超鏈接, 點擊時候提交直接跳轉到online.jsp
5.?寫監聽器: 監聽session銷毀,把當前登陸用戶從onlineuserlist移除!
?
?
2.?國際化
Javaweb增強:過濾器、監聽器、國際化、文件上傳下載、javaMail
?
l?國際化又簡稱為 i18n:internationalization
?
國際化的人:
人,英語,漢語; ?可以說這個人是國際化的人;
?
軟件的國際化:
軟件
中國: 顯示中文,以及服務符合中國習慣的文本字符串!
?1999-09-09
美國: 顯示英文,以及服務符合他國習慣的文本字符串!
這種軟件,就叫國際化的軟件!
?
如何做到國際化的軟件,要求:
1.?軟件中存儲特定的字符串
2.?知道瀏覽器當前使用哪種語言(Locale ?)
?
Locale ?本地化
Java提供了一個本地化的對象!封裝當前語言、國家、環境等特征!
?
? public?class?App { ? @Test //1. 本地化對象:Locale // 封裝語言、國家信息的對象,有java.util提供 public?void?testLocale() throws?Exception { // 模擬中國語言等環境 //Locale?locale?= Locale.CHINA; Locale?locale = Locale.getDefault(); // 當前系統默認的語言環境 System.out.println(locale.getCountry()); ?? // CN ?國家的簡稱 System.out.println(locale.getDisplayCountry()); // 國家名稱 System.out.println(locale.getLanguage()); // zh?語言簡稱 ? // 模擬美國國家 Locale?l_us = Locale.US; System.out.println(l_us.getCountry()); System.out.println(l_us.getDisplayCountry()); } } |
?
?
國際化
靜態數據國際化
網站中顯示的固定文本的國際化: “用戶名”“密碼“
?
國際化的軟件:
1.?存儲所有國家顯示的文本的字符串
a)?文件: properties
b)?命名: ?基礎名_語言簡稱_國家簡稱.properties
例如:msg_zh_CN.properties ????存儲所有中文
??????Msg_en_US.properties ???存儲所有英文
2.?程序中獲取
ResourceBundle類,可以讀取國際化的資源文件!
?
?
?
動態文本國際化
中文:1987-09-19 ??¥1000
英文: Sep/09 1987 ?$100
?
l?數值,貨幣,時間,日期等數據由于可能在程序運行時動態產生,所以無法像文字一樣簡單地將它們從應用程序中分離出來,而是需要特殊處理。Java 中提供了解決這些問題的 API 類(位于 java.util 包和 java.text 包中)
?
// 國際化 - 靜態數據 @Test public?void?testI18N() throws?Exception { ? // 中文語言環境 Locale locale = Locale.US; ? // 創建工具類對象ResourceBundle ResourceBundle bundle = ResourceBundle.getBundle("cn.itcast.f_i18n.msg", locale); // 根據key獲取配置文件中的值 System.out.println(bundle.getString("hello")); System.out.println(bundle.getString("username")); System.out.println(bundle.getString("pwd")); ? } ? // 國際化 - 動態文本 - 0. 概述 @Test public?void?testI18N2() throws?Exception { // 國際化貨幣 NumberFormat.getCurrencyInstance(); // 國際化數字 NumberFormat.getNumberInstance(); // 國際化百分比 NumberFormat.getPercentInstance(); ? // 國際化日期 //DateFormat.getDateTimeInstance(dateStyle, timeStyle, aLocale) } ? // 國際化 - 動態文本 - 1. 國際化貨幣 @Test public?void?testI18N3() throws?Exception { // 模擬語言環境 Locale locale = Locale.CHINA; // 數據準備 double?number = 100; // 工具類 NumberFormat nf = NumberFormat.getCurrencyInstance(locale); // 國際化貨幣 String m = nf.format(number); // 測試 System.out.println(m); } ? //面試題: ?代碼計算: ?$100 * 10 ? @Test public?void?eg() throws?Exception { String str = "$100"; int?num = 10; ? // 1. 分析str值是哪一國家的貨幣 Locale us = Locale.US; // 2. 國際化工具類 NumberFormat nf = NumberFormat.getCurrencyInstance(us); // 3. 解析str國幣 Number n = nf.parse(str); ? System.out.println(n.intValue() * num); } ? // 國際化 - 動態文本 - 2. 國際化數值 @Test public?void?testI18N4() throws?Exception { // 模擬語言環境 Locale locale?= Locale.CHINA; NumberFormat nf = NumberFormat.getNumberInstance(Locale.US); String str = nf.format(1000000000); System.out.println(str); } ? // 國際化 - 動態文本 - 3. 國際化日期 /* ?* 日期 ?* ??FULL ??2015年3月4日 星期三 ?* ??LONG ??2015年3月4日 ?* ??FULL ??2015年3月4日 星期三 ?* ???MEDIUM 2015-3-4 ?* ???SHORT ?15-3-4 ?* ??? ?* 時間 ?* ??FULL ??下午04時31分59秒 CST ?* ??LONG ??下午04時32分37秒 ?* ???MEDIUM 16:33:00 ?* ???SHORT ?下午4:33 ?* ??? ?* ?*/ @Test public?void?testI18N5() throws?Exception { ? // 日期格式 int?dateStyle = DateFormat.SHORT; // 時間格式 int?timeStyle = DateFormat.SHORT; ? // 工具類 DateFormat df = DateFormat.getDateTimeInstance(dateStyle, timeStyle, Locale.CHINA); String date = df.format(new?Date()); ? System.out.println(date); } ? // 面試2: 請將時間值:09-11-28 上午10時25分39秒 CST,反向解析成一個date對象。 @Test public?void?eg2() throws?Exception { String str = "09-11-28 上午10時25分39秒 CST"; // 創建DateFormat工具類,國際化日期 DateFormat df = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.FULL, Locale.getDefault()); Date d = df.parse(str); ? System.out.println(d); } ? |
?
Jsp頁面國際化
<html> ??<head> ?? <% ?? ResourceBundle bundle = ResourceBundle.getBundle("cn.itcast.f_i18n.msg",request.getLocale()); ?? %> ????<title><%=bundle.getString("title") %></title> <meta?http-equiv="pragma"?content="no-cache"> <meta?http-equiv="cache-control"?content="no-cache"> <meta?http-equiv="expires"?content="0">???? ??</head> ?? ??<body> ??<form?name="frmLogin"?action="${pageContext.request.contextPath }/admin?method=login"?method="post"> ?? <table?align="center"?border="1"> ?? <tr> ?? <td><%=bundle.getString("username") %></td> ?? <td> ?? <input?type="text"?name="userName"> ?? </td> ?? </tr> ?? <tr> ?? <td><%=bundle.getString("pwd") %></td> ?? <td> ?? <input?type="password"?name="pwd"> ?? </td> ?? </tr> ?? <tr> ?? <td> ?? <input?type="submit"?value="<%=bundle.getString("submit") %>"> ?? </td> ?? </tr> ?? </table> ??</form> ??</body> </html> |
?
Jsp頁面國際化 –?使用jstl標簽
JSTL標簽:
核心標簽庫
國際化與格式化標簽庫
數據庫標簽庫(沒用)
函數庫
?
<fmt:setLocale value=""/>????????設置本地化對象
?? <fmt:setBundle basename=""/>?????設置工具類
?? <fmt:message></fmt:message>?????顯示國際化文本
格式化數值
<fmt:formatNumber pattern="#.##" value="100.99"></fmt:formatNumber>
格式化日期:
<fmt:formatDate pattern="yyyy-MM-dd" value="${date}"/>
?
<html> ??<head> ?? <!-- 一、設置本地化對象 --> ?? <fmt:setLocale?value="${pageContext.request.locale}"/> ?? <!-- 二、設置工具類 --> ?? <fmt:setBundle?basename="cn.itcast.f_i18n.msg"?var="bundle"/> ? ????<title><fmt:message?key="title"?bundle="${bundle}"></fmt:message></title> <meta?http-equiv="pragma"?content="no-cache"> <meta?http-equiv="cache-control"?content="no-cache"> <meta?http-equiv="expires"?content="0">???? ??</head> ?? ??<body> ??<form?name="frmLogin"?action="${pageContext.request.contextPath }/admin?method=login"?method="post"> ?? <table?align="center"?border="1"> ?? <tr> ?? <td><fmt:message?key="username"?bundle="${bundle}"></fmt:message></td> ?? <td> ?? <input?type="text"?name="userName"> ?? </td> ?? </tr> ?? <tr> ?? <td><fmt:message?key="pwd"?bundle="${bundle}"></fmt:message></td> ?? <td> ?? <input?type="password"?name="pwd"> ?? </td> ?? </tr> ?? <tr> ?? <td> ?? <input?type="submit"?value="<fmt:message?key="submit"?bundle="${bundle}"/>"> ?? </td> ?? </tr> ?? </table> ??</form> ??</body> </html> |
?