java servelet异步处理请求

servlet3.0以后支持异步处理请求,具体是使用AsyncContext ,包装httpservletRequest以及httpservletResponse具有异步的功能,

  

 

  final AsyncContext ac = request.startAsync(request, response);

  ac.setTimeout(3000);

 然后在具体的使用异步的线程中,调用AsyncContext 内的具有异步功能的httpservletRequest以及httpservletResponse即可,实现异步的功能,

 

 在springmvc中,需要配置web.xml,使其具有异步的功能,具体的实现是

 

  <filter>

<filter-name>encodingfilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<async-supported>true</async-supported>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

<init-param>

<param-name>forceEncoding</param-name>

<param-value>true</param-value>

</init-param>

  </filter>

 

 <servlet>

    <!-- 在这里的mvc 启动加载的时候  系统默认会加载web-inf下的mvc-servlet.xml -->

<servlet-name>mvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<load-on-startup>1</load-on-startup>

<async-supported>true</async-supported>

  </servlet>

 

 这里切记,所有的filter和servlet都要加入<async-supported>true</async-supported>

你可能感兴趣的