嵌入式jetty实现HttpSessionListener

时间:2013-07-20 13:38:48

标签: jetty embedded-jetty

我正在尝试使用带有代理servlet的嵌入式jetty实现HttpSessionListener接口,我已经注册了SessionListener,但它根本没有被调用,这里是代码,

   public class JettyProxy {

    public static void main(String[] args) throws Exception {
        Server server = new Server();
        CustomProxyServlet customProxyServlet = new CustomProxyServlet();


        ServerConnector connector = new ServerConnector(server);
        connector.setPort(8888);
        server.addConnector(connector);

        ConnectHandler proxy = new ConnectHandler();

        server.setHandler(proxy);

        ServletContextHandler context = new ServletContextHandler(proxy, "/",
                ServletContextHandler.SESSIONS);
        ServletHolder proxyServlet = new ServletHolder(customProxyServlet);

        context.addServlet(proxyServlet, "/*");

        if (context.getSessionHandler() == null) {
            System.out.println("Session handler is null");
        } else {
            System.out.println("Session handler is not null");
        }

        if (context.getSessionHandler().getSessionManager() == null) {
            System.out.println("Managaer it null");
        } else {
            System.out.println("Manager is not null");
        }

        context.getSessionHandler().addEventListener(new CustomSessionHandler());

        server.start();
        server.join();
    }

}

SessionHandler不为null,会话创建事件没有被触发,请帮助我,获取会话事件的过程是什么?

1 个答案:

答案 0 :(得分:0)

你应该有一个SessionManager。我通常使用:  org.eclipse.jetty.server.session.HashSessionManager.HashSessionManager() 和 org.eclipse.jetty.server.session.SessionHandler.SessionHandler(SessionManager manager)

然后你应该设置上下文的处理程序

context.setHandler(sessionHandler);

sessionHandler.addEventListener("Your Session Listener");