使用Filter实现访问监控

16 篇文章 0 订阅
15 篇文章 0 订阅
实现Servlet的接口过滤器接口Filter实现对用户访问页面的监控



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 + "]";
}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值