title: Jetty Server's Session is Null
body: <p>I am trying to implement a hello world Jetty project (using Jetty 9.3.6.v20151106) with a servlet that looks at the session. However, when my code runs, I am not able to see the session or store any cookies. I have looked at <a href="https://stackoverflow.com/questions/5284262/jetty-http-session-is-always-null-embedded-container-servletholder">this</a> stackoverflow question to help troubleshoot and implemented the simple BaseServlet recommended, but every time I run it the BaseServlet outputs "no session". Am I missing some configuration?</p>
  
  <p>Here is my HelloWorld.java class:</p>
  
  <pre><code>package com.my.company;
  
  import org.eclipse.jetty.server.HttpConfiguration;
  import org.eclipse.jetty.server.HttpConnectionFactory;
  import org.eclipse.jetty.server.Server;
  import org.eclipse.jetty.server.ServerConnector;
  import org.eclipse.jetty.server.SessionManager;
  import org.eclipse.jetty.server.handler.ContextHandlerCollection;
  import org.eclipse.jetty.server.session.HashSessionIdManager;
  import org.eclipse.jetty.server.session.HashSessionManager;
  import org.eclipse.jetty.server.session.SessionHandler;
  import org.eclipse.jetty.servlet.ServletContextHandler;
  import org.eclipse.jetty.servlet.ServletHandler;
  import org.eclipse.jetty.servlet.ServletHolder;
  
  public class HelloWorld {
  
  public static void main(String[] args) throws Exception {
      //Create the server
      Server server = new Server();
  
      // Configure for Http
      HttpConfiguration http_config = new HttpConfiguration();
      http_config.setSecureScheme("https");
      http_config.setSecurePort(8443);
      http_config.setOutputBufferSize(32768);
  
      ServerConnector http = new ServerConnector(server,
              new HttpConnectionFactory(http_config));
      int http_port = 12043;
      http.setPort(http_port);
      http.setIdleTimeout(30000);
      server.addConnector(http);
  
      //Add ServletContextHandler
      ServletContextHandler servletContextHandler = new ServletContextHandler(
              ServletContextHandler.SESSIONS);
      servletContextHandler.setContextPath("/");
  
      SessionManager sessionManager = new HashSessionManager();
      sessionManager.setMaxInactiveInterval(60 * 15); //session time out of 15 minutes
      HashSessionIdManager idManager = new HashSessionIdManager();
      sessionManager.getSessionCookieConfig().setName("JSESSIONID_" + Integer.toString(http_port));
      sessionManager.setSessionIdManager(idManager);
      server.setSessionIdManager(idManager);
      SessionHandler sessionHandler = new SessionHandler(sessionManager);
      servletContextHandler.setSessionHandler(sessionHandler);
      server.setHandler(servletContextHandler);
  
      //Add BaseServlet
      ServletHolder holder = new ServletHolder(new BaseServlet());
      servletContextHandler.addServlet(holder, "/*");
  
      //Start the server
      server.start();
      System.out.println("SERVER DUMP AFTER START" + server.dump());
  
      // The use of server.join() the will make the current thread join and
      // wait until the server is done executing.
      // See http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
      server.join();
    }
  }
  </code></pre>
  
  <p>Here is my BaseServlet.java class:</p>
  
  <pre><code>package com.my.company;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.util.Date;
  
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  public class BaseServlet extends HttpServlet {
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
      boolean create = "true".equals(req.getParameter("create"));
  
      HttpSession session = req.getSession(create);
      if (create) {
          session.setAttribute("created", new Date());
      }
  
      PrintWriter pw = new PrintWriter(resp.getOutputStream());
      pw.println("Create = " + create);
      if (session == null) {
          pw.println("no session");
      } else {
          pw.println("Session = " + session.getId());
          pw.println("Created = " + session.getAttribute("created"));
      }
  
      pw.flush();
     }
  }
  </code></pre>
  
  <p>Here is my pom.xml file:</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;groupId&gt;EmbeddJettyHelloWorld&lt;/groupId&gt;
  &lt;artifactId&gt;EmbeddJettyHelloWorld&lt;/artifactId&gt;
  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  &lt;name&gt;EmbeddJettyHelloWorld&lt;/name&gt;
  
  &lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
              &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
              &lt;version&gt;1.4.0&lt;/version&gt;
              &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;java&lt;/goal&gt;
                      &lt;/goals&gt;
                  &lt;/execution&gt;
              &lt;/executions&gt;
              &lt;configuration&gt;
                  &lt;mainClass&gt;com.my.company.HelloWorld&lt;/mainClass&gt;
                  &lt;classpathScope&gt;test&lt;/classpathScope&gt;
                  &lt;systemProperties&gt;
                      &lt;property&gt;
                          &lt;key&gt;basedir&lt;/key&gt;
                          &lt;value&gt;${basedir}&lt;/value&gt;
                      &lt;/property&gt;
                  &lt;/systemProperties&gt;
              &lt;/configuration&gt;
          &lt;/plugin&gt;
  
          &lt;plugin&gt;
              &lt;groupId&gt;org.eclipse.jetty&lt;/groupId&gt;
              &lt;artifactId&gt;jetty-maven-plugin&lt;/artifactId&gt;
              &lt;version&gt;${jetty-version}&lt;/version&gt;
              &lt;configuration&gt;
                  &lt;webAppConfig&gt;
                      &lt;contextPath&gt;/&lt;/contextPath&gt;
                  &lt;/webAppConfig&gt;
                  &lt;webAppSourceDirectory&gt;target/${project.artifactId}-${project.version}&lt;/webAppSourceDirectory&gt;
                  &lt;webDefaultXml&gt;${project.basedir}/conf/jetty/webdefault.xml&lt;/webDefaultXml&gt;
              &lt;/configuration&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.eclipse.jetty&lt;/groupId&gt;
          &lt;artifactId&gt;jetty-server&lt;/artifactId&gt;
          &lt;version&gt;${jetty-version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.eclipse.jetty&lt;/groupId&gt;
          &lt;artifactId&gt;jetty-servlet&lt;/artifactId&gt;
          &lt;version&gt;${jetty-version}&lt;/version&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  
  &lt;properties&gt;
      &lt;jetty-version&gt;9.3.6.v20151106&lt;/jetty-version&gt;
  &lt;/properties&gt;
  </code></pre>
  
  <p></p>
  
  <p>Here is the output of the server dump:</p>
  
  <pre><code>    SERVER DUMP AFTER STARTorg.eclipse.jetty.server.Server@71f2a7d5 - STARTED
       += qtp1149319664{STARTED,8&lt;=8&lt;=200,i=5,q=0} - STARTED
       |   +- 15 qtp1149319664-15 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
       |   +- 13 qtp1149319664-13-acceptor-0@4c170d5c-ServerConnector@326de728    {HTTP/1.1,[http/1.1]}{0.0.0.0:12043} RUNNABLE @ sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method) prio=3
       |   +- 14 qtp1149319664-14 RUNNABLE @ sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)
       |   +- 12 qtp1149319664-12 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
       |   +- 18 qtp1149319664-18 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
       |   +- 16 qtp1149319664-16 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
       |   +- 11 qtp1149319664-11 RUNNABLE @ sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)
       |   +- 17 qtp1149319664-17 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
       += ServerConnector@326de728{HTTP/1.1,[http/1.1]}{0.0.0.0:12043} - STARTED
       |   +~ org.eclipse.jetty.server.Server@71f2a7d5 - STARTED
       |   +~ qtp1149319664{STARTED,8&lt;=8&lt;=200,i=5,q=0} - STARTED
       |   += org.eclipse.jetty.util.thread.ScheduledExecutorScheduler@1a6c5a9e - STARTED
       |   +- org.eclipse.jetty.io.ArrayByteBufferPool@2cfb4a64
       |   += HttpConnectionFactory@20ad9418[HTTP/1.1] - STARTED
       |   |   +- HttpConfiguration@5474c6c{32768/8192,8192/8192,https://:8443,[]}
       |   += org.eclipse.jetty.server.ServerConnector$ServerConnectorManager@4b6995df - STARTED
       |   |   +- org.eclipse.jetty.io.ManagedSelector@34033bd0 id=0 keys=0 selected=0 id=0
       |   |   |   +- sun.nio.ch.WindowsSelectorImpl@5200a403 keys=0
       |   |   +- org.eclipse.jetty.io.ManagedSelector@3aa9e816 id=1 keys=0 selected=0 id=1
       |   |       +- sun.nio.ch.WindowsSelectorImpl@33c7bf keys=0
       |   +- sun.nio.ch.ServerSocketChannelImpl[/0:0:0:0:0:0:0:0:12043]
       |   +- qtp1149319664-13-acceptor-0@4c170d5c-ServerConnector@326de728{HTTP/1.1,[http/1.1]}{0.0.0.0:12043}
   += org.eclipse.jetty.server.session.HashSessionIdManager@17d99928 - STARTED
   += o.e.j.s.ServletContextHandler@2e5c649{/,null,AVAILABLE} - STARTED
       |   += org.eclipse.jetty.server.session.SessionHandler@6fffcba5 - STARTED
       |   |   += org.eclipse.jetty.server.session.HashSessionManager@9e89d68 - STARTED
       |   |   |   +~ org.eclipse.jetty.server.session.HashSessionIdManager@17d99928 - STARTED
       |   |   |   += org.eclipse.jetty.util.thread.ScheduledExecutorScheduler@34340fab - STARTED
       |   |   |       +- sun.misc.Unsafe.park(Native Method)
       |   |   |       +- java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
       |   |   |       +- java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
       |   |   |       +- java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
       |   |   |       +- java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
       |   |   |       +- java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
       |   |   |       +- java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
       |   |   |       +- java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
       |   |   |       +- java.lang.Thread.run(Thread.java:745)
       |   |   += org.eclipse.jetty.servlet.ServletHandler@2aafb23c - STARTED
       |   |       += com.rtn.iaf.BaseServlet-484b61fc@3334c9b2==com.rtn.iaf.BaseServlet,-1,true - STARTED
       |   |       +- [/*]=&gt;com.rtn.iaf.BaseServlet-484b61fc
       |   |       += org.eclipse.jetty.servlet.ServletHandler$Default404Servlet-7a7b0070@1d1bf782==org.eclipse.jetty.servlet.ServletHandler$Default404Servlet,-1,false - STARTED
       |   |       +- [/]=&gt;org.eclipse.jetty.servlet.ServletHandler$Default404Servlet-7a7b0070
       |   |
       |   +&gt; No ClassLoader
       |   +&gt; Handler attributes o.e.j.s.ServletContextHandler@2e5c649{/,null,AVAILABLE}
       |   |   +- org.eclipse.jetty.server.Executor=qtp1149319664{STARTED,8&lt;=8&lt;=200,i=5,q=0}
       |   +&gt; Context attributes o.e.j.s.ServletContextHandler@2e5c649{/,null,AVAILABLE}
       |   |   +-     org.eclipse.jetty.util.DecoratedObjectFactory=org.eclipse.jetty.util.DecoratedObjectFactory[decorators=0]
       |   +&gt; Initparams o.e.j.s.ServletContextHandler@2e5c649{/,null,AVAILABLE}
       |
       +&gt; sun.misc.Launcher$AppClassLoader@73d16e93
         +- file:/C:/Users/myUser/tutorialAndDemo_workspace/EmbeddJettyHelloWorld/target/classes/
         +- file:/C:/Users/myUser/tutorialAndDemo_workspace/.m2/repository1/org/eclipse/jetty/jetty-server/9.3.6.v20151106/jetty-server-9.3.6.v20151106.jar
         +- file:/C:/Users/myUser/tutorialAndDemo_workspace/.m2/repository1/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar
         +- file:/C:/Users/myUser/tutorialAndDemo_workspace/.m2/repository1/org/eclipse/jetty/jetty-http/9.3.6.v20151106/jetty-http-9.3.6.v20151106.jar
         +- file:/C:/Users/myUser/tutorialAndDemo_workspace/.m2/repository1/org/eclipse/jetty/jetty-util/9.3.6.v20151106/jetty-util-9.3.6.v20151106.jar
         +- file:/C:/Users/myUser/tutorialAndDemo_workspace/.m2/repository1/org/eclipse/jetty/jetty-io/9.3.6.v20151106/jetty-io-9.3.6.v20151106.jar
         +- file:/C:/Users/myUser/tutorialAndDemo_workspace/.m2/repository1/org/eclipse/jetty/jetty-servlet/9.3.6.v20151106/jetty-servlet-9.3.6.v20151106.jar
         +- file:/C:/Users/myUser/tutorialAndDemo_workspace/.m2/repository1/org/eclipse/jetty/jetty-security/9.3.6.v20151106/jetty-security-9.3.6.v20151106.jar
         +- sun.misc.Launcher$ExtClassLoader@2b80d80f
  </code></pre>