【2个有用的过滤器示例】给出了一些有用的过滤器示例。
仅通过过滤器发送响应的示例
MyFilter.java
import java.io.*;
import javax.servlet.*;
public class MyFilter implements Filter{
public void init(FilterConfig arg0) throws ServletException {} public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
PrintWriter out=res.getWriter();
out.print("<
br/>
this site is underconstruction..");
out.close();
}
public void destroy() {}
}
单页访问者计数示例
MyFilter.java
import java.io.*;
import javax.servlet.*;
public class MyFilter implements Filter{
static int count=0;
public void init(FilterConfig arg0) throws ServletException {} public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
PrintWriter out=res.getWriter();
chain.doFilter(request, response);
out.print("<
br/>
Total visitors "+(++count));
out.close();
}
public void destroy() {}
}
在过滤器中检查总响应时间的示例
MyFilter.java
import java.io.*;
import javax.servlet.*;
public class MyFilter implements Filter{
static int count=0;
public void init(FilterConfig arg0) throws ServletException {} public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
PrintWriter out=res.getWriter();
long before=System.currentTimeMillis();
chain.doFilter(request, response);
long after=System.currentTimeMillis();
out.print("<
br/>
Total response time "+(after-before)+" miliseconds");
out.close();
}
public void destroy() {}
}
推荐阅读
- 如何使用Servlet将数据写入PDF()
- Servlet过滤器(FilterConfig用法)
- Servlet如何显示图像()
- servlet将文件上传到服务器的示例
- Servlet中的注册表单示例
- 金馆长微信表情包:二人联合嘲讽表情包_微信
- 金馆长微信表情包:女生最喜欢听异性说的话_微信
- 带歌词的金馆长表情包大全_微信
- 学生党必备的金馆长表情包大全_微信