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

首頁 > 編程 > JSP > 正文

jsp網頁計數器實現、示例

2019-11-02 14:53:33
字體:
來源:轉載
供稿:網友

 網頁計數器想必大家都有見到過吧,記錄每一個訪問者,下面有個不錯的示例,感興趣的朋友可以參考下

 代碼如下://過濾器類 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"
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石楼县| 肃南| 界首市| 南岸区| 凤城市| 麻江县| 南充市| 马龙县| 宜城市| 绍兴县| 毕节市| 汕头市| 安平县| 根河市| 平舆县| 布尔津县| 广汉市| 灵山县| 盱眙县| 和林格尔县| 南溪县| 奉化市| 库尔勒市| 新化县| 太白县| 民和| 镇巴县| 永定县| 长岛县| 海伦市| 繁峙县| 科技| 沙湾县| 香格里拉县| 神木县| 宿迁市| 紫金县| 娱乐| 偏关县| 青海省| 板桥市|