您缺少三件事。
- 一个上下文
- 一个会话ID经理
- 一个会话处理程序(与相关的会话管理器)
Main.java
import org.eclipse.jetty.server.Server;import org.eclipse.jetty.server.handler.ContextHandler;import org.eclipse.jetty.server.session.HashSessionIdManager;import org.eclipse.jetty.server.session.HashSessionManager;import org.eclipse.jetty.server.session.SessionHandler;public class Main{ public static void main(String[] args) { MyDumpHandler dump = new MyDumpHandler(); Server server = new Server(8080); // Specify the Session ID Manager HashSessionIdManager idmanager = new HashSessionIdManager(); server.setSessionIdManager(idmanager); // Sessions are bound to a context. ContextHandler context = new ContextHandler("/"); server.setHandler(context); // Create the SessionHandler (wrapper) to handle the sessions HashSessionManager manager = new HashSessionManager(); SessionHandler sessions = new SessionHandler(manager); context.setHandler(sessions); // Put dump inside of SessionHandler sessions.setHandler(dump); // Tree is now // Server // + ContextHandler("/") // + SessionHandler(Hash) //+ MyDumpHandler try { server.start(); server.join(); } catch (Throwable t) { t.printStackTrace(); } }}
MyDumpHandler.java
import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.eclipse.jetty.server.Request;import org.eclipse.jetty.server.handler.AbstractHandler;public class MyDumpHandler extends AbstractHandler{ public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { PrintWriter out = response.getWriter(); response.setContentType("text/plain"); try { HttpSession session = request.getSession(); if (session.isNew()) { out.printf("New Session: %s%n", session.getId()); } else { out.printf("Old Session: %s%n", session.getId()); } } catch (IllegalStateException ex) { out.println("Exception!" + ex); ex.printStackTrace(out); } out.close(); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)