监听器和监听器过滤器常见应用GUl中理解
實現一個監聽器的接口:(有N種)
1.編寫一個監聽器
實現監聽器的接口
//統計網上在線人數:統計Session
public class olinejqt implements HttpSessionListener {
//創建session監聽:看你的一舉一動
//一旦創建Session就會觸發這個事件
public void sessionCreated(HttpSessionEvent se) {
ServletContext ctx = se.getSession ().getServletContext ();
System.out.println (se.getSession ().getId ());
Integer inter = (Integer) ctx.getAttribute ("session");
//判斷inter為空就是沒有人
if (inter==null){
//加人
inter = new Integer (1); //1人
}else{
int count = inter.intValue ();
inter = new Integer (count+1);//+1=2人
}
ctx.setAttribute ("session",inter);
}
//銷毀Session監聽
//一旦銷毀Session就會觸發這一事件
public void sessionDestroyed(HttpSessionEvent se) {
ServletContext ctx = se.getSession ().getServletContext ();
Integer inter = (Integer) ctx.getAttribute ("session");
//判斷inter為空就是沒有人
if (inter==null){
//加人
inter = new Integer (0); //0人沒人了
}else{
int count = inter.intValue ();
inter = new Integer (count-1);//-1
}
ctx.setAttribute ("session",inter);
}
/**
*Session銷毀
* 1.手動銷毀 getSession() invalidate()
* 2.自動銷毀
*/
}
2.在web中配置監聽器
<!--注冊監聽器 -->
<listener>
<listener-class>com.bubbles.filter.jtqlistener.olinejqt</listener-class>
</listener>
3.看情況是否使用
監聽器和監聽器GUl中理解
過濾器、監聽器常見應用
監聽器:GUl編程中經常使用
package com.bubbles.filter.Server;import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.WindowListener;public class TestPane1 {public static void main(String[] args) {Frame frame = new Frame ("中秋節快樂"); //新建一個窗體Panel panel = new Panel (null);//面板frame.setLayout (null); //設置窗體的布局frame.setBounds (300,300,500,500);frame.setBackground (new Color (0,0,255));//設置背景顏色panel.setBounds (50,50,300,300);panel.setBackground (new Color (0,255,0));//設置背景顏色frame.add (panel);frame.setVisible (true);//監聽事件:監聽關閉事件frame.addWindowListener (new WindowListener () {public void windowOpened(WindowEvent e) {System.out.println ("打開");}public void windowClosing(WindowEvent e) {System.out.println ("關閉ing");System.exit (0);}public void windowClosed(WindowEvent e) {System.out.println ("關閉ed");}public void windowIconified(WindowEvent e) {}public void windowDeiconified(WindowEvent e) {}public void windowActivated(WindowEvent e) {System.out.println ("激活");}public void windowDeactivated(WindowEvent e) {System.out.println ("未激活");}});} } 用戶登錄之后才能進入首頁 !用戶注銷就不能打開主頁!1.用戶登錄之后,向Session中放入用戶的數據2.進入主頁的時候要判斷用戶是否已經登錄;要求:在過濾中實現! HttpServletRequest request1 = (HttpServletRequest) servletRequest; HttpServletResponse response1 = (HttpServletResponse) servletResponse;if (request1.getSession ().getAttribute (Constant.USER_SESSIN)==null) {response1.sendRedirect ("error.jsp"); }filterChain.doFilter (servletRequest,servletResponse); //過濾器往下走總結
以上是生活随笔為你收集整理的监听器和监听器过滤器常见应用GUl中理解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python实数运算
- 下一篇: python 爬虫 智联招聘