jsp网页计数器

2 篇文章 0 订阅
2 篇文章 0 订阅
 
//过滤器类
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的相对路径
		//判断路径,如果是登陆的那个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,从而实现当关闭服务的时候,把文件保存在磁盘中。下次从磁盘中读取。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值