找到你要的答案

Q:Embedded Jetty: How do I call setSessionTrackingModes without a ServletContextListener

Q:嵌入式Jetty:我怎么称呼setsessiontrackingmodes没有servletcontextlistener

I'm wiring up an embedded Jetty server in my main and I want to enforce only cookies as a session tracking mode.

So I try to do:

//in main
ServletContextHandler contextHandler = 
    new ServletContextHandler(ServletContextHandler.SESSIONS);

contextHandler
    .getServletContext()
    .setSessionTrackingModes(EnumSet.of(SessionTrackingMode.COOKIE));

But I get the following:

Exception in thread "main" java.lang.IllegalStateException
at org.eclipse.jetty.servlet.ServletContextHandler$Context.setSessionTrackingModes(ServletContextHandler.java:1394)

My servlet context is not yet initialized.

The obvious solution is to do this in a ServletContextListener, but I'd rather not. I want all wiring and setup to stay in one central place without using Listeners.

Is there a way?

我连接嵌入式Jetty服务器在我的主,我想只执行饼干作为会话跟踪模式。

所以我试着去做:

//in main
ServletContextHandler contextHandler = 
    new ServletContextHandler(ServletContextHandler.SESSIONS);

contextHandler
    .getServletContext()
    .setSessionTrackingModes(EnumSet.of(SessionTrackingMode.COOKIE));

但我得到以下:

Exception in thread "main" java.lang.IllegalStateException
at org.eclipse.jetty.servlet.ServletContextHandler$Context.setSessionTrackingModes(ServletContextHandler.java:1394)

我尚未初始化servlet上下文。

显而易见的解决方案是在一个servletcontextlistener这样做,但我不想。我希望所有的布线和安装在一个中心位置不使用监听器。

有办法吗?

answer1: 回答1:

The reason for the exception is that the ServletContext doesn't exist yet (your server isn't started yet).

But there's 2 ways to accomplish this.

Technique 1) Explicit Session Management:

    Server server = new Server(8080);

    // Specify the Session ID Manager
    HashSessionIdManager idmanager = new HashSessionIdManager();
    server.setSessionIdManager(idmanager);

    // Create the SessionHandler to handle the sessions
    HashSessionManager manager = new HashSessionManager();
    manager.setSessionTrackingModes(EnumSet.of(SessionTrackingMode.COOKIE)); // <-- here
    SessionHandler sessions = new SessionHandler(manager);

    // Create ServletContext
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setSessionHandler(sessions); // <-- set session handling
    server.setHandler(context);

This is probably the more appropriate way to accomplish this for embedded jetty, as you can now control the entire creation / storage / behavior of Sessions.

Technique 2) Use Defaults, Configure HashSessionManager:

    Server server = new Server(8080);

    // Create ServletContext
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.getSessionHandler()
           .getSessionManager()
           .setSessionTrackingModes(EnumSet.of(SessionTrackingMode.COOKIE));
    server.setHandler(context);

For simple web apps this will work just fine.

异常的原因是ServletContext尚不存在(你的服务器没有开始呢)。

但是有2种方法可以做到这一点。

技术1)明确的会话管理:

    Server server = new Server(8080);

    // Specify the Session ID Manager
    HashSessionIdManager idmanager = new HashSessionIdManager();
    server.setSessionIdManager(idmanager);

    // Create the SessionHandler to handle the sessions
    HashSessionManager manager = new HashSessionManager();
    manager.setSessionTrackingModes(EnumSet.of(SessionTrackingMode.COOKIE)); // <-- here
    SessionHandler sessions = new SessionHandler(manager);

    // Create ServletContext
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setSessionHandler(sessions); // <-- set session handling
    server.setHandler(context);

这可能是完成这一嵌入式码头更合适的方式,你现在可以控制整个创作/存储/行为的会议。

技巧2)使用默认设置,Configure HashSessionManager:

    Server server = new Server(8080);

    // Create ServletContext
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.getSessionHandler()
           .getSessionManager()
           .setSessionTrackingModes(EnumSet.of(SessionTrackingMode.COOKIE));
    server.setHandler(context);

对于简单的Web应用程序,这将工作只是罚款。

servlets  jetty  embedded-jetty  servlet-3.0