08-03
04
JSP中计算页面执行(处理)时间
作者:Java伴侣 日期:2008-03-04
在CJWBlog的用户日志首页最下方,会显示页面执行时间 Processed in 4 ms ,实现如下:
1. TimerFilter源码如下(或请下载附件):
2. 在web.xml中配置:
3. 在JSP页面中写上代码:
4. 完成.
1. TimerFilter源码如下(或请下载附件):
复制内容到剪贴板 程序代码
package com.chinajavaworld.util;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
public class TimerFilter
implements Filter
{
public static class Timer
{
long time;
public void start()
{
time = System.currentTimeMillis();
}
public void stop()
{
time = System.currentTimeMillis() - time;
}
public long getTime()
{
return time;
}
public Timer()
{
time = 0L;
}
}
public static final String DEFAULT_TIMER_ATTRIBUTE = "cjw.timer";
private String key;
public TimerFilter()
{
key = null;
}
public void init(FilterConfig filterConfig)
throws ServletException
{
if(filterConfig.getInitParameter("key") != null)
key = filterConfig.getInitParameter("key");
else
key = DEFAULT_TIMER_ATTRIBUTE;
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException
{
HttpServletRequest request = (HttpServletRequest)servletRequest;
Timer timer = new Timer();
timer.start();
request.setAttribute(key, timer);
filterChain.doFilter(servletRequest, servletResponse);
timer.stop();
handleTimer(timer);
}
public void handleTimer(Timer timer1)
{
}
public void destroy()
{
}
}
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
public class TimerFilter
implements Filter
{
public static class Timer
{
long time;
public void start()
{
time = System.currentTimeMillis();
}
public void stop()
{
time = System.currentTimeMillis() - time;
}
public long getTime()
{
return time;
}
public Timer()
{
time = 0L;
}
}
public static final String DEFAULT_TIMER_ATTRIBUTE = "cjw.timer";
private String key;
public TimerFilter()
{
key = null;
}
public void init(FilterConfig filterConfig)
throws ServletException
{
if(filterConfig.getInitParameter("key") != null)
key = filterConfig.getInitParameter("key");
else
key = DEFAULT_TIMER_ATTRIBUTE;
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException
{
HttpServletRequest request = (HttpServletRequest)servletRequest;
Timer timer = new Timer();
timer.start();
request.setAttribute(key, timer);
filterChain.doFilter(servletRequest, servletResponse);
timer.stop();
handleTimer(timer);
}
public void handleTimer(Timer timer1)
{
}
public void destroy()
{
}
}
2. 在web.xml中配置:
复制内容到剪贴板 程序代码
<filter>
<filter-name>Timer</filter-name>
<filter-class>com.chinajavaworld.util.TimerFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Timer</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-name>Timer</filter-name>
<filter-class>com.chinajavaworld.util.TimerFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Timer</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3. 在JSP页面中写上代码:
复制内容到剪贴板 程序代码
<%
// Get the timer from the request as an attribute if it exists:
TimerFilter.Timer timer
= (TimerFilter.Timer)request.getAttribute(TimerFilter.DEFAULT_TIMER_ATTRIBUTE);
if (timer != null) {
timer.stop();
%>
<p style="font-size:11px;">Processed in <b><%= timer.getTime() %></b> ms
<%}%>
// Get the timer from the request as an attribute if it exists:
TimerFilter.Timer timer
= (TimerFilter.Timer)request.getAttribute(TimerFilter.DEFAULT_TIMER_ATTRIBUTE);
if (timer != null) {
timer.stop();
%>
<p style="font-size:11px;">Processed in <b><%= timer.getTime() %></b> ms
<%}%>
4. 完成.
评论: 0 | 引用: 0 | 查看次数: 629
发表评论