关于listener和filter的初步学习。
1 关于在线人数的实现:
建立一个静态变量onlinecounter, 当接收到一个session时,onlinecounter++, 当session销毁时 onlinecounter--。
这个 listener类的代码如下:
package com.dr;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.apache.log4j.Logger;
public class OnlineCounter extends HttpServlet implements HttpSessionListener
{
private static Logger log = Logger.getLogger(OnlineCounter.class);
private static final long serialVersionUID = 1L;
private static int sessionCounter = 1;
public OnlineCounter(){
log.info("OnlineCounter initialized.");
}
public void sessionCreated(HttpSessionEvent se) {
sessionCounter++;
log.info("session created:" + sessionCounter);
}
public void sessionDestroyed(HttpSessionEvent se) {
sessionCounter--;
log.info("session destroied");
}
public static int getOnlineSession() {
return sessionCounter;
}
}
在web.xml中加入这个监听器并设置session 10分钟自动注销
<listener>
<listener-class>
com.dr.OnlineCounter
</listener-class>
</listener>
<session-config>
<session-timeout>10</session-timeout><!-- 默认时间为分钟 -->
</session-config>
2 实现访问源统计
初始化一个static 的Map, 当filter接收到一次request时, 获得访问地址,如果存在于map中,则值加1,否则为0。
获得访问来源的方式为String path = req.getRequestURI().toString();
这个map要通过ServletRequest request传给jsp。
filter代码:
package com.dr;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ApplicationAware;
public class CountTimes implements Filter {
// 过滤器传来的初始化值
private String auther ;
private Integer time;
static Map<String,Integer>application=new HashMap();
@Override
public void destroy() {
}
ServletContext context;
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request ;
HttpServletResponse resp = (HttpServletResponse)response ;
// HttpServletApplication app=(HttpServletApplication)application;
String path = req.getRequestURI().toString();
time=application.get(path);
if(time==null){
time=0;
}
time++;
application.put(path, time);
request.setAttribute("application", application);
chain.doFilter(req, resp);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
//取得初始化值
auther = filterConfig.getInitParameter("auther");
context = filterConfig.getServletContext();
}
}
在web.xml中注册这个filter
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.dr.CountTimes</filter-class>
<!-- 初始化过滤器参数 -->
</filter>
<!--过滤器映射 -->
<filter-mapping>
<filter-name>MyFilter</filter-name>
<!-- 默认拦截说有的页面 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
jsp文件如下:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030" import= "com.dr.OnlineCounter" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
在线: <%= OnlineCounter.getOnlineSession() %>人
<br>
<c:forEach items="${application }" var="entry" varStatus="status"> <!- 遍历map->
<tr>
<td>${ entry.key}</td>
<td>${ entry.value}</td>
<br>
</tr>
</c:forEach>