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

编程入门 行业动态 更新时间:2024-10-11 03:21:38

Servlet监听统计网站历史访问<a href=https://www.elefans.com/category/jswz/34/1766191.html style=人数"/>

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 {@Overridepublic void contextDestroyed(ServletContextEvent arg0) {// TODO Auto-generated method stubSystem.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 blocke.printStackTrace();}}@Overridepublic 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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}



web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="" xmlns:xsi="" xsi:schemaLocation=" .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>


目录结构:


更多推荐

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

本文发布于:2024-03-23 17:49:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1741018.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:人数   历史   网站   Servlet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!