import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class numFilter implements Filter
{
@Override
public void init(FilterConfig filterConfig)throws ServletException
{
}
@Override
public void doFilter(ServletRequest request,ServletResponse response, FilterChain chain) throws ServletException , IOException
{
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse res = (HttpServletResponse)response;
//清理缓存
res.setHeader("Pragma","no-cache");
res.setHeader("Cache-Control","no-cache");
res.setHeader("Expires","0");
HttpSession session = req.getSession();
String uri = req.getRequestURI();
String[] str = uri.split("/"); //将字符串从"/"处截取并存入字符串数组中
uri = "/" + str[str.length-1];
System.out.println(uri);
if((session.getAttribute("number1")!=null )|| (session.getAttribute("number2")!=null ))
{
res.getWriter().write("Input error, please re-enter");
return;
}else
{
}
chain.doFilter(request,response);//通过当前Filter,可以访问
}
@Override
public void destroy()
{
}
}
xml
<filter>
<filter-name>numFilter</filter-name>
<filter-class>numFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>numFilter</filter-name>
<url-pattern>/result</url-pattern>
</filter-mapping>