天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

jsp網頁計數器實現示例

復制代碼 代碼如下:
//過濾器類
public class EcondingFilter implements Filter {
private String charset = null;
private ServletContext context = null;
private String path = "";
/**
* 在銷毀前將數據存入本地文件中
*/
public void destroy() {
//獲取servleContext中的屬性的那個值
String nums = (String) context.getAttribute("nums");
//創建寫入流
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銷毀");
}

復制代碼 代碼如下:
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 alt="" src="http://img.blog.csdn.NET/20130728233435953?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2tyZ3diag==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"><img alt="" src="http://img.blog.csdn.NET/20130728233445625?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2tyZ3diag==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"> //判斷路徑,如果是登陸的那個action,就讓保存的context里面的那個屬性加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后");

}

復制代碼 代碼如下:
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()) {//判斷文件是否存在
// 如果文件不存在,就創建一個文件,保存在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 + "");// 寫入初始化數據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();

}
}
}
//當每次tomcat啟動服務時,進行讀取創建的那個文件
path = "d://text.txt";
// 從本地讀取訪問的人數的文件
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();
}
}
//將獲得到的數據保存在servletContext中
context.setAttribute("nums", nums);
}

}

用過濾器方便的一點,不需要我們每次手動去調用,當web服務啟動時候,自動會引用。首先說下,我寫到init方法的依據是,每次web服務啟動會調用一次init方法,當關閉服務的時候會調用一次destory方法,將計數的那個數據文件,這個方法寫到init方法和destory方法,這樣可以減少每次的不斷的讀取服務器和讀取寫入文件的次數,當我們每登陸一次,就讓servletContext中的那個attr加1,從而實現當關閉服務的時候,把文件保存在磁盤中。下次從磁盤中讀取。

jsp技術jsp網頁計數器實現示例,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 色狠狠xx| 戳女人屁股流水羞羞漫画 | 中文在线无码高潮潮喷在线 | 亚洲精品无码不卡 | av亚洲2017色天堂 | 国产国拍亚洲精品永久软件 | 擼擼擼麻豆密臀AV | 久久久久久亚洲精品影院 | 制服丝袜 快播 | 超嫩校花被灌醉在线观看 | 国内自拍 在线 亚洲 欧美 | 做你的爱人BD日本 | 纯肉高H种马艳遇风流多 | 天天综合网网欲色 | 花蝴蝶免费版高清版 | 做a爱片的全过程 | 亚洲第一成年人网站 | 99久酒店在线精品2019 | SM调教贱屁股眼哭叫求饶H | 蜜芽无码亚洲资源网站 | 国产主播AV福利精品一区 | 苍井空教师BD在线观看全集 | 日本高清加勒比 | 一本道久在线综合色姐 | WWW污污污抽搐喷潮COM | 少妇仑乱A毛片 | 国产中文视频无码成人精品 | 久久这里有精品 | 青草久久精品亚洲综合专区 | 精品无码国产AV一区二区三区 | 国产精品色午夜视频免费看 | 辣文肉高h粗暴 | 成人毛片免费播放 | 少妇精油按摩 | 揉抓捏打抽插射免费视频 | 国产精品伊人 | 久青草国产在线观看视频 | 把手戳进美女尿口里动态图 | 欧美激情一区二区三区视频 | 99精品中文字幕在线观看 | av在线观看网站免费 |