国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

監(jiān)聽SevletContext的創(chuàng)建與監(jiān)聽一個(gè)Sesison被保存到一個(gè)文件中的過程 (57)

2019-11-15 00:05:58
字體:
供稿:網(wǎng)友
監(jiān)聽SevletContext的創(chuàng)建與監(jiān)聽一個(gè)Sesison被保存到一個(gè)文件中的過程 (57)

其他監(jiān)聽器

監(jiān)聽者

被監(jiān)聽者

監(jiān)聽到事件對(duì)象

HttpsessionActivationListener

HttpSession – 監(jiān)聽HttpSession活化和頓化。

HttpSessionEvent

HttpSessionAttributeListener

HttpSession – 監(jiān)聽session的屬性變化的。S.setAttributee();

HttpSessionBindingEvent

HttpSessionBindingListener

HttpSession - 監(jiān)聽哪一個(gè)對(duì)象,綁定到了session上。S.setAtrri(name,User);

HttpSessionListener

HttpSesion – 監(jiān)聽sessioin創(chuàng)建銷毀

HttpSessionEvent

ServletContextAttributeListener

ServletContext – 屬性變化的

ServletContextListener[W1]

ServletContext 創(chuàng)建銷毀

ServletRequestListener - SerlvetRequestAttibuteListner

Rrequest -創(chuàng)建銷毀

Request屬性變化

1ServletContextListener 用于監(jiān)聽SevletContext的創(chuàng)建

在web中的所的監(jiān)聽器都是全局的 - 都是在項(xiàng)目啟動(dòng)時(shí)直接由tomcat創(chuàng)建。

監(jiān)聽器沒有順序。只是監(jiān)聽的對(duì)象不一樣。

在一個(gè)項(xiàng)目中可以存在多個(gè)監(jiān)聽器。

package cn.hx.listener;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.PRintWriter;

import java.net.URL;

import javax.servlet.ServletContextEvent;

import javax.servlet.ServletContextListener;

public class MyContextListener implements ServletContextListener {

//在啟動(dòng)時(shí)讀取數(shù)據(jù)庫(kù)之前保存的數(shù)據(jù)

public void contextInitialized(ServletContextEvent sc) {

System.err.println("application被創(chuàng)建了:"+sc.getServletContext());

URL url = MyContextListener.class.getClassLoader().getResource("count.txt");

String path = url.getFile();

System.err.println(path);

try {

BufferedReader bf = new BufferedReader(new FileReader(path));

String line = bf.readLine();

Integer count = Integer.valueOf(line);

sc.getServletContext().setAttribute("count",count);

System.err.println("初始的值是:"+count);

} catch (Exception e) {

e.printStackTrace();

}

}

//在銷毀這個(gè)對(duì)象時(shí)保存一些數(shù)據(jù)到數(shù)據(jù)庫(kù)或是文件中

public void contextDestroyed(ServletContextEvent e) {

System.err.println("銷毀了:"+e.getServletContext());

//保存到文件中去

URL url = MyContextListener.class.getClassLoader().getResource("count.txt");

String path = url.getFile();

System.err.println(path);

File file = new File(path);

try {

PrintWriter out = new PrintWriter(file);

//獲取applicat的數(shù)據(jù)

Integer count = (Integer) e.getServletContext().getAttribute("count");

out.print(count);

out.close();

} catch (FileNotFoundException e1) {

e1.printStackTrace();

}

}

}

在項(xiàng)目啟動(dòng)時(shí)一次加載所有的配置文件。

2 監(jiān)聽一個(gè)Sesison被保存到一個(gè)文件中的過程

第一步:書寫B(tài)ean實(shí)現(xiàn)HttpSessionActivationListener

package cn.hx.domain;

import java.io.Serializable;

import javax.servlet.http.HttpSessionActivationListener;

import javax.servlet.http.HttpSessionEvent;

public class Person implements Serializable,HttpSessionActivationListener{

private String name;

public Person() {

}

public Person(String name) {

this.name = name;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public void sessionWillPassivate(HttpSessionEvent se) {

System.err.println("保存到文件中去了..."+this.getName());

}

public void sessionDidActivate(HttpSessionEvent se) {

System.err.println("從文件中活化了...."+this.getName());

}

@Override

public String toString() {

return "Person [name=" + name + "]";

}

}


第二步:配置這個(gè)項(xiàng)目

<Context docBase="D://programfiles//MyEcl

<Manager className="org.apache.catalina.session.PersistentManager"

saveOnRestart="true">

<Store className="org.apache.catalina.session.FileStore"

directory="d:/a">

</Store>

</Manager>

</Context>


第三步:測(cè)試

<%

if(session.getAttribute("p")==null){

int a = new Random().nextInt(100);

Person p = new Person(""+a);

session.setAttribute("p",p);

}

//保存cookie

Cookie c = new Cookie("JSESSIONID",session.getId());

c.setMaxAge(60*30);

c.setPath(request.getContextPath());

response.addCookie(c);

%>

<hr/>

${p}

<hr/>

<%=session.getId()%>


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 通河县| 四会市| 晋江市| 大同市| 武义县| 高雄县| 资兴市| 交口县| 文昌市| 民权县| 武清区| 垫江县| 常德市| 绵阳市| 榆中县| 东乡族自治县| 舒兰市| 静安区| 修文县| 五大连池市| 聊城市| 德钦县| 宣城市| 雅安市| 黄大仙区| 湟源县| 施秉县| 龙井市| 勐海县| 砚山县| 太白县| 民乐县| 兴义市| 金门县| 吴旗县| 清涧县| 综艺| 北辰区| 麻阳| 南和县| 嫩江县|