我想模拟一个真实的网页。在服务器上,当网页加载图像时速度很慢。我下载了我正在 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 秒,然后加载速度非常快。
最佳答案
我会使用过滤器:
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>
...
关于java - 用jsp模拟真实的网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18056738/