Jetty Websocket servlet InstantiationException问题

时间:2013-09-14 14:00:27

标签: java jetty

我遇到了websocket example of jetty工作

的问题
public class App {

    public static void main(String[] args)  {

        Server server = new Server(8080);

        ServletHandler handler = new ServletHandler();
        server.setHandler(handler);

        handler.addServletWithMapping(MyEchoServlet.class, "/test/*");

        try {
            server.start();
            server.join();

        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    public class MyEchoServlet extends WebSocketServlet {

        @Override
        public void configure(WebSocketServletFactory factory) {
            //factory.getPolicy().setIdleTimeout(10000);
            factory.register(MyEchoSocket.class);
        }
    }
     @WebSocket(maxMessageSize = 64 * 1024)
    public class MyEchoSocket {

        Session session;

        @OnWebSocketConnect
        public void onConnect(Session session){
            this.session = session;
            System.out.println("init "+session.getUpgradeRequest().getRequestURI().getPath() );
        }

        @OnWebSocketMessage
        public void onText(String msg) {
             System.out.println("rec "+msg );
            try {
                session.getRemote().sendString(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @OnWebSocketClose
        public void onClose(int statusCode, String reason) {
            System.out.println("MyWebSocket.onClose()");
        } 
    }
}
一旦我尝试从javascript创建Websocket,

就会出现以下错误:

var ws = new WebSocket("ws://localhost:8080/test/");

服务器错误:

2013-09-14 15:57:04.076 INFO  Server - jetty-9.0.5.v20130815
2013-09-14 15:57:04.122 INFO  ServerConnector - Started ServerConnector@1a51ce0{HTTP/1.1}{0.0.0.0:8080}
2013-09-14 15:57:11.700 WARN  ContextHandler - unavailable
java.lang.InstantiationException: app.App$MyEchoServlet
    at java.lang.Class.newInstance(Unknown Source)
    at org.eclipse.jetty.servlet.ServletHolder.newInstance(ServletHolder.java:979)
    at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:523)
    at org.eclipse.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:424)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:671)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:505)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:434)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.Server.handle(Server.java:445)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:268)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:229)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:358)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:601)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:532)
    at java.lang.Thread.run(Unknown Source)
2013-09-14 15:57:11.706 WARN  ServletHandler - 
javax.servlet.ServletException: app.App$MyEchoServlet-4598284@e19c6ca5==app.App$MyEchoServlet,-1,false
    at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:563)
    at org.eclipse.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:424)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:671)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:505)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:434)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.Server.handle(Server.java:445)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:268)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:229)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:358)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:601)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:532)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.InstantiationException: app.App$MyEchoServlet
    at java.lang.Class.newInstance(Unknown Source)
    at org.eclipse.jetty.servlet.ServletHolder.newInstance(ServletHolder.java:979)
    at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:523)
    ... 13 more

jetty版本:9.0.5v20130815(来自maven eclipse.jetty repo)

1 个答案:

答案 0 :(得分:4)

由于您已将servlet和socket声明为内部类,因此请更改以下2行代码...

// From
public class MyEchoServlet extends WebSocketServlet {
// To
public static class MyEchoServlet extends WebSocketServlet {


// From
public class MyEchoSocket {
// To
public static class MyEchoSocket {

这是因为Java不能只实例化非静态内部类。

从main()方法尝试这个,你会看到......

Servlet test = new MyEchoServlet();