读书人

【100分】请问关于Servlet3.0 + Tomca

发布时间: 2012-01-16 23:36:51 作者: rapoo

【100分】请教关于Servlet3.0 + Tomcat7.0.x 异步调用的问题

Java code
@WebServlet(urlPatterns="/test",asyncSupported=true)public class ModuleAsyncServlet extends HttpServlet {@Override    protected void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        try {            response.setCharacterEncoding("UTF-8");            Writer out = response.getWriter();//            request.startAsync(); //报异常            AsyncContext ctx = request.getAsyncContext();//null            System.out.println(request.isAsyncSupported());  //false



异常信息如下

java.lang.IllegalStateException: Not supported.
at org.apache.catalina.connector.Request.startAsync(Request.java:1594)
at org.apache.catalina.connector.Request.startAsync(Request.java:1587)
at org.apache.catalina.connector.RequestFacade.startAsync(RequestFacade.java:1024)
at org.async.servlet.ModuleAsyncServlet.doGet(ModuleAsyncServlet.java:35)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)



=======================


当我提交一个 test请求的时候 , 可以进入到 get 方法中
并执行,当执行到request.startAsync()的时候, 会报如上异常

环境 jdk.1.6.0_22 apache7.0.6 正式版


=======================================

这个问题解决一上午了, 郁闷 不知道咋办



完整代码如下

Java code
package org.async.servlet;import java.io.IOException;import java.io.PrintWriter;import java.io.Writer;import java.util.Date;import javax.servlet.AsyncContext;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet(urlPatterns="/test",asyncSupported=true)public class ModuleAsyncServlet extends HttpServlet {    /**     *      */    private static final long serialVersionUID = 1L;    @Override    protected void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        try {            response.setCharacterEncoding("UTF-8");            Writer out = response.getWriter();            request.startAsync();            AsyncContext ctx = request.getAsyncContext();            System.out.println(request.isAsyncSupported());            //AsyncContext ctx = request.startAsync();            new Thread(new Executor(ctx)).start();            out.write("结束Servlet的时间:" + new Date() + ".");            out.flush();        } catch (Exception e) {            e.printStackTrace();        }    }    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {        super.doPost(req, resp);    }}class Executor implements Runnable {    private AsyncContext ctx = null;    public Executor(AsyncContext ctx) {        this.ctx = ctx;    }    public void run() {        try {            // 等待十秒钟,以模拟业务方法的执行            Thread.sleep(10000);            PrintWriter out = ctx.getResponse().getWriter();            out.println("业务处理完毕的时间:" + new Date() + ".");            out.flush();            ctx.complete();        } catch (Exception e) {            e.printStackTrace();        }    }}





[解决办法]
java.lang.IllegalStateException: Not supported. 不被支持

给你例子看看
tomcat7和Servlet3之异步特性体验
http://sunqi.javaeye.com/blog/708823
http://www.360doc.com/content/10/0920/08/1720440_55016710.shtml





希望对你有帮助

[解决办法]
帮忙顶一下,我最近也在搞这个,除了类似问题。楼主要是解决了,别忘也告知我一声……
[解决办法]
= = 。。经过我刚才测试了下``应该和代码无关。。是服务器的问题``tom7我没试。因为我用的tom6.。用jboss测试了你的代码可以通过``没问题
[解决办法]
新jar包没放?
not support?
[解决办法]

探讨

新jar包没放?
not support?

[解决办法]
tomcat 7.0.6下测试通过
结束Servlet的时间:Sat Feb 12 16:54:33 CST 2011.
[解决办法]
这个问题,是因为你没有在web.xml中配置指定的servlet对异步的支持,查看servlet3的dtd,你就明白了.
[解决办法]
使用servlet3.0的前提条件:
1.使用servlet3.0新标准jar包;
2.JDK必须在1.6以上版本;
3.编译器的编译级别为6.0
4.在web.xml文件中,使用3.0规范
5.使用支持servlet3.0特性的web容器,比如tomcat7

你满足以上条件之后,就没有问题了

读书人网 >J2EE开发

热点推荐