麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JSP > 正文

jsp網(wǎng)頁計(jì)數(shù)器實(shí)現(xiàn)示例

2024-09-05 00:22:00
字體:
供稿:網(wǎng)友
網(wǎng)頁計(jì)數(shù)器想必大家都有見到過吧,記錄每一個(gè)訪問者,下面有個(gè)不錯(cuò)的示例,感興趣的朋友可以參考下

復(fù)制代碼 代碼如下:


//過濾器類
public class EcondingFilter implements Filter {
private String charset = null;
private ServletContext context = null;
private String path = "";
/**
* 在銷毀前將數(shù)據(jù)存入本地文件中
*/
public void destroy() {
//獲取servleContext中的屬性的那個(gè)值
String nums = (String) context.getAttribute("nums");
//創(chuàng)建寫入流
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(path);
bw = new BufferedWriter(fw);
bw.write(nums);
} catch (Exception e) {
e.printStackTrace();
} finally {

try {
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
System.out.println("filter銷毀");
}


復(fù)制代碼 代碼如下:


public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
System.out.println("doFilter前");
String path = ((HttpServletRequest)request).getServletPath();//獲取每次訪問的action的相對路徑
<img src="http://img.blog.csdn.net/20130728233435953?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2tyZ3diag==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"><img src="http://img.blog.csdn.net/20130728233445625?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2tyZ3diag==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"> //判斷路徑,如果是登陸的那個(gè)action,就讓保存的context里面的那個(gè)屬性加1
if(path.endsWith("/login.action")){
context.setAttribute("nums",Integer.parseInt(context.getAttribute("nums").toString())+1+"");
}
request.setCharacterEncoding(charset);
response.setCharacterEncoding(charset);
chain.doFilter(request, response);
System.out.println("doFilter后");

}


復(fù)制代碼 代碼如下:


public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
System.out.println("filter初始化");
//獲取編碼格式
charset = filterConfig.getInitParameter("encoding");
//獲取servletContext
context = filterConfig.getServletContext();
System.out.println(charset);

path = context.getRealPath("");
File file = new File("D://text.txt");
if (!file.exists()) {//判斷文件是否存在
// 如果文件不存在,就創(chuàng)建一個(gè)文件,保存在D盤中
file = new File("d://text.txt");
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
bw.write(0 + "");// 寫入初始化數(shù)據(jù)0
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
}
}
//當(dāng)每次tomcat啟動服務(wù)時(shí),進(jìn)行讀取創(chuàng)建的那個(gè)文件
path = "d://text.txt";
// 從本地讀取訪問的人數(shù)的文件
FileReader fr = null;
BufferedReader bf = null;
String nums = "";
try {
fr = new FileReader(path);
bf = new BufferedReader(fr);
nums = bf.readLine();
System.out.println(nums);
} catch (Exception e) {
e.printStackTrace();
} finally {

try {
if (bf != null) {
bf.close();
}
if (fr != null) {
fr.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//將獲得到的數(shù)據(jù)保存在servletContext中
context.setAttribute("nums", nums);
}

}


用過濾器方便的一點(diǎn),不需要我們每次手動去調(diào)用,當(dāng)web服務(wù)啟動時(shí)候,自動會引用。首先說下,我寫到init方法的依據(jù)是,每次web服務(wù)啟動會調(diào)用一次init方法,當(dāng)關(guān)閉服務(wù)的時(shí)候會調(diào)用一次destory方法,將計(jì)數(shù)的那個(gè)數(shù)據(jù)文件,這個(gè)方法寫到init方法和destory方法,這樣可以減少每次的不斷的讀取服務(wù)器和讀取寫入文件的次數(shù),當(dāng)我們每登陸一次,就讓servletContext中的那個(gè)attr加1,從而實(shí)現(xiàn)當(dāng)關(guān)閉服務(wù)的時(shí)候,把文件保存在磁盤中。下次從磁盤中讀取。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 经典三级在线视频 | 激情大乳女做爰办公室韩国 | 国产精品久久久久影院老司 | 日韩视频一二区 | 在线观看免费视频麻豆 | 九九热视频免费 | 精品一区二区三区毛片 | 免费在线观看国产精品 | 一级做a爰片性色毛片2021 | 免费国产不卡午夜福在线 | 欧美a视频| va免费视频| 成人毛片在线 | 香蕉国产9 | 国产一区二区精品免费 | 777sesese| 久久久久久久一区 | 久草在线公开视频 | 亚洲午夜在线观看 | 国产精品午夜一区 | 精品一区二区三区中文字幕老牛 | 美女视频免费一区二区 | 国产资源在线免费观看 | 性少妇videosexfreexxx片 | 污视频在线免费播放 | 久久亚洲精品11p | 7777网站| 亚洲精品午夜国产va久久成人 | 成人在线精品视频 | 伦一区二区三区中文字幕v亚洲 | 久草在线新时代视觉 | 欧美日韩亚洲成人 | 俄罗斯hdxxx 日夜操天天干 | 黑人一区二区 | 中文字幕在线一 | 在线视频 中文字幕 | 1024亚洲天堂 | 婷婷亚洲一区二区三区 | 夜添久久精品亚洲国产精品 | 午夜视频在线观 | 国产一级免费片 |