0

我想模拟一个真实的网页。当网页加载图像时,服务器上的速度很慢。我下载了我在 Eclipse Tomcat 服务器上工作的网站。显然图像加载非常过去。我要他们慢慢来。

我试过 Thread.sleep()

<div style="width:100px; height:75px; float:left" id="rsim"><a href="inc/6.html"><img border="0" 
 width="100" height="75" **<%  Thread.sleep(2000); %>src="inc/images/bo.jpg"**  /></a></div>

<div style="width:100px; height:75px; float:left" id="rsim"><a href="inc/4.html"><img border="0"  width="100" height="75" **<%  Thread.sleep(2000); %> src="inc/images/la1.jpg"**  /></a></div>

但它会等待整个网站 4 秒,然后非常快速地加载该网站。

4

1 回答 1

2

我会使用过滤器:

package mypackage;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class WaitFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {
        // nothing
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // ignore
        }
        chain.doFilter(request, response);
    }

    public void destroy() {
        // nothing
    }
}

然后,在您的 web.xml 中:

...
    <filter>
        <filter-name>WaitFilter</filter-name>
        <filter-class>mypackage.WaitFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>WaitFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
...
于 2013-08-05T11:50:43.070 回答