Filter 问题,我要通过 Filter 做一个1分钟内限制请求次数的程序

dfjjhb 2009-04-16 05:26:48

我要通过 Filter 做一个1分钟内限制请求次数的程序。基本思想是这样,先记录一个开始时间,

当请求过来时先判断时间是否已经过了你要求的时间,计数器清零,然后在++,如果没有过那么

就直接判断是否已过了你要求的数目上限。下边是已经完成的程序,高手补充下,谢谢。
public class SessionSingleFilter implements Filter {

private static final Logger LOG = Logger.getLogger(SessionSingleFilter.class);

public void init(FilterConfig filterConfig) throws ServletException {
}

public void doFilter(ServletRequest servletRequest, ServletResponse

servletResponse, FilterChain filterChain)
throws IOException, ServletException {
filterChain.doFilter(servletRequest, servletResponse);
System.out.println("Filter66");

Calendar c =Calendar.getInstance();
Date date = c.getTime();
HttpServletRequest httpServletRequest=(HttpServletRequest)servletRequest;
HttpServletResponse httpServletResponse=(HttpServletResponse)servletResponse;
HttpSession httpsession=httpServletRequest.getSession();
Object jspzi= httpsession.getAttribute("a");
System.out.println(jspzi);


}

public void destroy() {
}
}
...全文
186 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
dfjjhb 2009-04-17
  • 打赏
  • 举报
回复
你的意思是直接加个 记录总时间变量就可以 记录 过去的请求次数了?
你msn是多少?或者qq号也行,请教请教,呵呵
abc130314 2009-04-17
  • 打赏
  • 举报
回复
请求一次,就会过滤一次。次数一样的。
dfjjhb 2009-04-17
  • 打赏
  • 举报
回复
state 加一个属性 int totalTime

state.totalTime++;

if (currentTime <= state.endTime) {
……

state 类里面吗? 记录 请求 次数 需要 记录 doFilter()?
abc130314 2009-04-17
  • 打赏
  • 举报
回复
state 加一个属性 int totalTime

state.totalTime++;

if (currentTime <= state.endTime) {
……

dfjjhb 2009-04-17
  • 打赏
  • 举报
回复
我现在想记录一下 filter的请求次数,如何实现保存filter请求次数功能呢?
abc130314 2009-04-17
  • 打赏
  • 举报
回复
System.currentTimeMillis()——API上有。当前时间与协调世界时 1970 年 1 月 1 日午夜之间的时间差(以毫秒为单位测量)
private static final long 间隔时间 = 1000 * 60; —— 自己定义
dfjjhb 2009-04-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 abc130314 的回复:]
Java code
public void init(FilterConfig filterConfig) throws ServletException {
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpSession session = ((HttpServletRequest) request).getSession();
State state = (State) session.getAttribute("State");
long currentTime = Syst…
[/Quote]
System.currentTimeMillis() 这个时间是什么时间,单位是毫秒?怎么算出来的,我不懂这个时间的计算公式是怎么来得,给我解释一下吧。 state.endTime = currentTime + 间隔时间;间隔时间 是自己定义的变量吧?
dfjjhb 2009-04-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 abc130314 的回复:]
Java code
public void init(FilterConfig filterConfig) throws ServletException {
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpSession session = ((HttpServletRequest) request).getSession();
State state = (State) session.getAttribute("State");
long currentTime = Syst…
[/Quote]
好的,呵呵,我先试试看,谢谢1楼的哥们
abc130314 2009-04-17
  • 打赏
  • 举报
回复
我QQ号19656842
richard_2010 2009-04-16
  • 打赏
  • 举报
回复
mark下
abc130314 2009-04-16
  • 打赏
  • 举报
回复

public void init(FilterConfig filterConfig) throws ServletException {
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpSession session = ((HttpServletRequest) request).getSession();
State state = (State) session.getAttribute("State");
long currentTime = System.currentTimeMillis();
if (state == null) {
state = new State();
state.endTime = currentTime + 间隔时间;
session.setAttribute("State", state);
}
if (currentTime <= state.endTime) {
state.time++;
} else {
state.endTime += 间隔时间;
state.time = 1;
}
if (state.time <= 允许登录次数) {
chain.doFilter(request, response);
}
}

public void destroy() {
}
private static final long 间隔时间 = 1000 * 60;
private static final int 允许登录次数 = 10;

private class State {

private long endTime;
private int time;
}

如果你不想用内部类,就用一个long[2];

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧