Servlet监听统计网站历史访问人数

需求:求网站访问历史访问人数

思路:必须有个监听器:ServletContextListener     文件存储       application传值      利用:contextInitialized和contextDestroyed实现功能



index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
//1.定义一个变量
//String num="0";
//2.取出原来的值(application)
Object obj=application.getAttribute("counter");

//3.加一
int intnum=Integer.parseInt(obj.toString());
intnum++;

 //4.再次存放在application中
application.setAttribute("counter",intnum);
 %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    您是本网站的第<span span style="color:red;font-size:34px"><%=intnum %>个用户</span><br>
    
  </body>
</html>


CounterListener.java

package cn.wfc.eshop.control;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class CounterListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
		
		System.out.println("--------------销毁----------------");
        String root=arg0.getServletContext().getRealPath("/");
		System.out.println("------得到路径---------"+root);
        try {
			FileWriter fw=new FileWriter(root+"WEB-INF\\count.txt");
			fw.write(arg0.getServletContext().getAttribute("counter").toString());
	              fw.close();
        } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		// TODO Auto-generated method stub

	/*String xx=	arg0.getServletContext().getInitParameter("x");
	String yy=	arg0.getServletContext().getInitParameter("y");


	System.out.println("------------x和y的值------------"+xx+"---------------"+xx);
		System.out.println("--------------初始化--------------");
*/
		System.out.println("-------------读取文件了-----------------");
		String num=null;
        String root=arg0.getServletContext().getRealPath("/");
 
		try {
			//字符流
			FileReader fr=new FileReader(root+"WEB-INF\\count.txt");
			//缓冲流
			BufferedReader br=new BufferedReader(fr);
			
			  num=br.readLine();
			if(num==null){
				num="0";
			}
			 
			br.close();
			fr.close();
			arg0.getServletContext().setAttribute("counter", num);

			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
		
	}

}



web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <listener>
        <listener-class>cn.wfc.eshop.control.CounterListener</listener-class>
    </listener>    
    <context-param>
        <param-name>x</param-name>
        <param-value>1000</param-value>
    </context-param>
    <context-param>
        <param-name>y</param-name>
        <param-value>100</param-value>
    </context-param>
</web-app>


目录结构:


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值