
如果根据IP来判断,那还要有一个表记录IP,
进入filter后->获取客户端IP->查询是否存在->存在访问量不加,不存在就加1,还可以限定每个IP一天一次或是一周一次什么的。。。
filter在网上找最简单的例子就行了,在doFilter里写
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest) request
HttpSession session=req.getSession()
String ip=客户端IP
从数据库查IP是否存在
if(不存在){
今日访问量和总访问量加1
if(今日时间不是当前时间){
今日时间变为当前时间
}
}
long todayVisit=今日访问量(从数据库查)
long totalVisit=总访问量
session.setAttribute("todayVisit", todayVisit)
session.setAttribute("totalVisit", totalVisit)
chain.doFilter(req, response)
}
然后在jsp页面里
今日访问量为:${todayVisit},总访问量为:${totalVisit}
用其他方式类似
public class Counter {
private int count
// 每访问一次,计数器自加一
public int getCount() {
return ++count
}
public void setCount(int count) {
this.count = count
}
}
<%-- 定义一个 session 范围内的计数器 记录个人访问信息 --%>
<jsp:useBean id="personCount" class="com.helloweenvsfei.jspweb.bean.Counter" scope="session" />
<%-- 定义一个 application 范围内的计数器 记录所有人的访问信息 --%>
<jsp:useBean id="totalCount" class="com.helloweenvsfei.jspweb.bean.Counter" scope="application" />
<div align="center">
<form action="method.jsp" method="get">
<fieldset style='width: 300'>
<legend>计数器</legend>
<table align="center" width="400">
<tr>
<td width=150 align="right" style="font-weight:bold">您的访问次数:</td>
<td>
<%-- 获取个人的 访问次数 --%>
<jsp:getProperty name="personCount" property="count" />次
</td>
</tr>
<tr>
<td width=150 align="right" style="font-weight:bold">总共的访问次数:</td>
<td>
<%-- 获取所有人的 访问次数 --%>
<jsp:getProperty name="totalCount" property="count" />次
</td>
</tr>
</table>
</fieldset>
</form>
</div>
希望你能帮到你
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)