实现Servlet的接口过滤器接口Filter实现对用户访问页面的监控
JavaBean类:
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.csair.amp.web.webinf.common.model.LoginInfo;
import com.csair.amp.web.webinf.utils.ReadPropertiesUtils;
import com.csair.smms.effmonitor.dto.MonitorLog;
import com.csair.smms.effmonitor.service.MonitorService;
/**
* 效能监控过滤器类
*
* @author ahomeeye
*
* 2012-7-15 下午9:20:49
*/
public class EffMonitorFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
HttpSession session = req.getSession();
String orginPath = req.getRequestURL().toString();
int index = orginPath.indexOf("/", 8);
String reqPath = orginPath.substring(index);
LoginInfo u = (LoginInfo) session.getAttribute("user");
if (u != null) {
// 获取monitorService实现bean,此处无法使用spring的自动封装功能,monitorService
ApplicationContext ac = WebApplicationContextUtils
.getWebApplicationContext(session.getServletContext());
MonitorService monitorService = (MonitorService) ac
.getBean("monitorService");
//ReadPropertiesUtils请参考http://ahomeeye.iteye.com/admin/blogs/1533760
Map<String, String> urlMap = ReadPropertiesUtils
.getPropMap("effmonitor.properties");
if (urlMap.containsKey(reqPath)) { // 在监控的url包含有
MonitorLog m = new MonitorLog();
m.setUsername(u.getUsername());
m.setLogtime(new Date());
m.setFunction(urlMap.get(reqPath));
m.setClassName(reqPath);
m.setMethodName(urlMap.get(reqPath));
monitorService.insertMonitorLog(m);// 保存效能监控日志
}
}
chain.doFilter(req, res);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
@Override
public void destroy() {
}
}
JavaBean类:
package com.csair.smms.effmonitor.dto;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* 监控日志实体
*
* @author ahomeeye
*
* 2012-7-11 下午7:22:49
*/
@Entity
@Table(name = "MONITOR_LOG")
public class MonitorLog implements Serializable {
private static final long serialVersionUID = 7707875472196483005L;
private int id;// 编号
private String username;// 用户名
private Date logtime;// 使用时间
private String function;// 使用功能
private String className;// 类名
private String methodName;// 方法名
public MonitorLog() {
super();
}
public MonitorLog(int id, String username, Date logtime, String function,
String className, String methodName) {
super();
this.id = id;
this.username = username;
this.logtime = logtime;
this.function = function;
this.className = className;
this.methodName = methodName;
}
@Id
@SequenceGenerator(name = "monitor", sequenceName = "seq_monitorlog", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "monitor")
@Column(name = "LOG_ID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "USERNAME", length = 20)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name = "LOG_TIME")
@Temporal(TemporalType.TIMESTAMP)
public Date getLogtime() {
return logtime;
}
public void setLogtime(Date logtime) {
this.logtime = logtime;
}
@Column(name = "FUNCTION", length = 20)
public String getFunction() {
return function;
}
public void setFunction(String function) {
this.function = function;
}
@Column(name = "CLASS_NAME", length = 100)
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
@Column(name = "METHOD_NAME", length = 30)
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
@Override
public String toString() {
return "MonitorLog [id=" + id + ", username=" + username + ", logtime="
+ logtime + ", function=" + function + ", className="
+ className + ", methodName=" + methodName + "]";
}
}