濮阳杆衣贸易有限公司

主頁 > 知識庫 > jsp網(wǎng)頁計數(shù)器實現(xiàn)示例

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

熱門標簽:四川保險智能外呼系統(tǒng)商家 電銷機器人是有一些什么技術(shù) 北票市地圖標注 電銷機器人好賣么 杭州ai語音電銷機器人功能 商洛電銷 杭州語音電銷機器人軟件 地圖標注線上教程 高德地圖標注樣式
復(fù)制代碼 代碼如下:

//過濾器類
public class EcondingFilter implements Filter {
private String charset = null;
private ServletContext context = null;
private String path = "";
/**
* 在銷毀前將數(shù)據(jù)存入本地文件中
*/
public void destroy() {
//獲取servleContext中的屬性的那個值
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 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后");

}

復(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)建一個文件,保存在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();

}
}
}
//當每次tomcat啟動服務(wù)時,進行讀取創(chuàng)建的那個文件
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ào)用,當web服務(wù)啟動時候,自動會引用。首先說下,我寫到init方法的依據(jù)是,每次web服務(wù)啟動會調(diào)用一次init方法,當關(guān)閉服務(wù)的時候會調(diào)用一次destory方法,將計數(shù)的那個數(shù)據(jù)文件,這個方法寫到init方法和destory方法,這樣可以減少每次的不斷的讀取服務(wù)器和讀取寫入文件的次數(shù),當我們每登陸一次,就讓servletContext中的那個attr加1,從而實現(xiàn)當關(guān)閉服務(wù)的時候,把文件保存在磁盤中。下次從磁盤中讀取。
您可能感興趣的文章:
  • JavaScript實現(xiàn)計數(shù)器基礎(chǔ)方法
  • 使用JavaScript制作一個簡單的計數(shù)器的方法
  • 一個簡單的網(wǎng)站訪問JS計數(shù)器 刷新1次加1次訪問
  • javascript下計數(shù)器每秒自動加1
  • 用JAVASCRIPT幫我寫個計數(shù)器
  • js計數(shù)器代碼
  • 原生JS實現(xiàn)非常好看的計數(shù)器

標簽:紅河 宿州 云浮 西藏 貴州 江西 丹東 青島

巨人網(wǎng)絡(luò)通訊聲明:本文標題《jsp網(wǎng)頁計數(shù)器實現(xiàn)示例》,本文關(guān)鍵詞  jsp,網(wǎng)頁,計數(shù)器,實現(xiàn),示例,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《jsp網(wǎng)頁計數(shù)器實現(xiàn)示例》相關(guān)的同類信息!
  • 本頁收集關(guān)于jsp網(wǎng)頁計數(shù)器實現(xiàn)示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    诸城市| 育儿| 黄梅县| 上高县| 丽江市| 长垣县| 临泽县| 定西市| 大埔县| 夏邑县| 新巴尔虎右旗| 青冈县| 潜江市| 惠东县| 布尔津县| 化德县| 滦南县| 永泰县| 龙泉市| 蒲江县| 上饶市| 郸城县| 青河县| 沽源县| 浮梁县| 循化| 无棣县| 诸城市| 鄂托克旗| 玛曲县| 沙田区| 芒康县| 盐池县| 大姚县| 康马县| 延安市| 临泉县| 大竹县| 滦南县| 文安县| 沈阳市|