简体   繁体   中英

Error in invoking asynchronous java servlet using Eclipse & maven

I'm using Eclipse IDE to work on servlet. I have created a maven project to work with asynchronous servlet 3.0 and added proper dependency to it. The code request.isAsyncSupported() return true. Now when i'm calling the servlet it give this error

WARN:oejs.ServletHandler:/testplugin/JsonServlet java.lang.IllegalStateException: DISPATCHED,initial at org.eclipse.jetty.server.Request.getAsyncContext(Request.java:325) at com.ajitpals.search.grid.fs.JsonServlet.doGet(JsonServlet.java:42) at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:594) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:486) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:524) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:233) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1065) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:413) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandl er.java:192) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:999) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111) at org.eclipse.jetty.server.Server.handle(Server.java:351) at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:454) at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:890) at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:944) at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:634) at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:230) at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:77) at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:609) at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run (SelectChannelEndPoint.java:45) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:599) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:534) at java.lang.Thread.run(Unknown Source).

I have added following dependencies in maven project.

 <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1-b08</version>
</dependency>

<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-server</artifactId>
    <version>9.0.2.v20130417</version>
</dependency>

and the servlet code is

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    if(request.isAsyncSupported()){

    final AsyncContext asynCtx = request.getAsyncContext();

    //Set the timeout
    asynCtx.setTimeout(35000);

    //Add the listeners.
    asynCtx.addListener(new AsyncListener() {

        public void onTimeout(AsyncEvent arg0) throws IOException {
            System.out.println("timeout");              
        }

        public void onStartAsync(AsyncEvent arg0) throws IOException {
            System.out.println("on start");             
        }

        public void onError(AsyncEvent arg0) throws IOException {
            System.err.println("on error");             
        }

        public void onComplete(AsyncEvent arg0) throws IOException {
            System.out.println("On complete");              
        }
    });

    //start the new thread.
    asynCtx.start(new Runnable() {

        public void run() {
            try{
                asynCtx.getResponse().getWriter().write(MessageFormat.format("<h1>Process task id : [{0}] and Name [{1}]</h1>", 
                        Thread.currentThread().getId()));
            }catch(Exception ex){
                System.err.println("async error " + ex.getMessage());
            }
        }
    });
    }
}

Does anyone faced the similar issue and how can we resolve them.

EDIT:

sometime servlet also giving following error

PWC6345: There is an error in invoking javac. A full JDK (not just JRE) is required Caused by:org.apache.jasper.JasperException:PWC6345: There is an error in invoking javac. A full JDK (not just JRE) is required

Thanks

如果我们尝试在request.startAsync()生成错误之前获取getAsyncRequest() ,这是代码错误。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM