Jetty http session is always null (Embedded Container, ServletHolder)
Your code works fine with this skeletal implementation of BaseServlet:
public class BaseServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
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();
}
so the session is probably being invalidated somewhere else in your code.
The SessionHandler
can also be explicity set using the setSessionHandler()
method of ServletContextHandler
.