最开始按照网上配置了一个servlet class 没有继承Filter .结果报错.网上有文章说是tomcat 启动加载的servlet-3.0- api 加载了 tomcat 安装目录下lib里边的自带的 servlet api 与 环境中api冲突 改成
问题是配置文件里配置就是这样的
严重: Exception starting filter AsyncFilter
java.lang.ClassCastException: com.AsynDemo.AsynServ cannot be cast to javax.servlet.Filter
这个错误其实是没有继承Filter,重写一个过滤导致的
环境:
pom.xml
javax.servlet javax.servlet-api 3.0.1 provided
web.xml 配置
这个是把你重写的过滤类写入pageResponse class>com.AsynDemo.AsynServlet class> <async-supported>trueasync-supported>pageResponse /*
@Controller
@RequestMapping("/asyncServlet")
public class AsynServlet implements Filter
{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
@RequestMapping(value="/testAsyn")
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
System.out.println("start");
PrintWriter out = response.getWriter();
AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(5000);
asyncContext.addListener(new MyAsyncListener());
new Thread(new Work(asyncContext,request)).start();
out.print("异步执行中");
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
}
就这么样了 还会报错
A filter or servlet of the current chain does not support asynchronous opera request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
查了查资料,
所有的解决办法都在说可能是参数配置未配置正确,或者是需要在server.xml上配置。最后,在StackOverflow上找到了如下答案 request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
尝试这个改了改,问题解决
总结一下,这种配置文件的方式 .坑不少,但最终解决了.
还有一种写法有空再测试
https://blog.csdn.net/zxaini2010/article/details/50358763