From 417fb1d7f884372faf0e8639f0cfb0a7bcbf8786 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Wed, 16 Nov 2016 14:31:31 +1100 Subject: [PATCH] Pull request #1107 Slight improvements and simplification. - Use the SessionHandler created by the ServletContext - Use a single servlet to set and get the greeting - Added more information about the session - Added convenience links for the demo --- .../jetty/embedded/HelloSessionServlet.java | 48 +++++++++++++++---- .../OneServletContextWithSession.java | 24 +++++----- .../jetty/embedded/StoreInSessionServlet.java | 47 ------------------ 3 files changed, 51 insertions(+), 68 deletions(-) delete mode 100644 examples/embedded/src/main/java/org/eclipse/jetty/embedded/StoreInSessionServlet.java diff --git a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/HelloSessionServlet.java b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/HelloSessionServlet.java index 01f0243799e..e40966b3342 100644 --- a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/HelloSessionServlet.java +++ b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/HelloSessionServlet.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.embedded; import org.eclipse.jetty.server.session.HashedSession; import java.io.IOException; +import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -29,8 +30,6 @@ import javax.servlet.http.HttpServletResponse; public class HelloSessionServlet extends HttpServlet { - private String greeting; - public HelloSessionServlet() {} @Override @@ -38,14 +37,47 @@ public class HelloSessionServlet extends HttpServlet HttpServletResponse response ) throws ServletException, IOException { - HashedSession session = (HashedSession)request.getSession(); - if((greeting = (String)session.getAttribute("greeting")) == null) { - greeting = "Hello"; - } response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); - response.getWriter().println( - "

" + greeting + " from HelloSessionServlet

"); + response.addHeader("Cache-Control","no-cache"); + + HashedSession session = (HashedSession)request.getSession(); + String message; + String link; + + String greeting = request.getParameter("greeting"); + if (greeting != null) + { + session.setAttribute("greeting", greeting); + message = "New greeting '" + greeting + "' set in session."; + link = "Click here to use the new greeting from the session."; + } + else + { + greeting = (String)session.getAttribute("greeting"); + + if (greeting != null) + { + message = "Greeting '" + greeting + "' set from session."; + } + else + { + greeting = "Hello"; + message = "Greeting '" + greeting + "' is default."; + } + + link = "Click here to set a new greeting."; + } + + PrintWriter out = response.getWriter(); + out.println("

" + greeting + " from HelloSessionServlet

"); + out.println("

" + message + "

"); + out.println("
");
+        out.println("session.getId() = " +session.getId());
+        out.println("session.isNew() = " +session.isNew());
+        out.println("
"); + out.println("

" + link + "

"); + } } diff --git a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/OneServletContextWithSession.java b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/OneServletContextWithSession.java index 06a42a10eef..655687a94db 100644 --- a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/OneServletContextWithSession.java +++ b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/OneServletContextWithSession.java @@ -32,29 +32,27 @@ public class OneServletContextWithSession { Server server = new Server(8080); - SessionHandler handler = new SessionHandler(); - handler.setServer(server); - - HashSessionManager manager = new HashSessionManager(); - handler.setSessionManager(manager); - + // Create an ID manager for the server. This is normally done + // by default, but is done explicitly here for demonstration. AbstractSessionIdManager idManager = new HashSessionIdManager(); - manager.setSessionIdManager(idManager); server.setSessionIdManager(idManager); + // Create a ServletContext, with a session handler enabled. ServletContextHandler context = new ServletContextHandler( ServletContextHandler.SESSIONS); context.setContextPath("/"); context.setResourceBase(System.getProperty("java.io.tmpdir")); server.setHandler(context); - //Servlet to store a different greeting in the session - //Can be accessed using http://localhost:8080/store - context.addServlet(StoreInSessionServlet.class, "/store/*"); + // Access the SessionHandler from the context. + SessionHandler sessions = context.getSessionHandler(); + + // Set a SessionManager. This is normally done by default, + // but is done explicitly here for demonstration. + sessions.setSessionManager(new HashSessionManager()); + - //Servlet to read the greeting stored in the session, if you have not visited the - //StoreInSessionServlet a default greeting is shown, after visiting you will see - //a different greeting. + //Servlet to read/set the greeting stored in the session. //Can be accessed using http://localhost:8080/hello context.addServlet(HelloSessionServlet.class, "/"); diff --git a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/StoreInSessionServlet.java b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/StoreInSessionServlet.java deleted file mode 100644 index a5276b17579..00000000000 --- a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/StoreInSessionServlet.java +++ /dev/null @@ -1,47 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.embedded; - -import org.eclipse.jetty.server.session.HashedSession; - -import java.io.IOException; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -public class StoreInSessionServlet extends HttpServlet -{ - public StoreInSessionServlet() {} - - @Override - protected void doGet(HttpServletRequest request, - HttpServletResponse response ) throws ServletException, - IOException - { - HashedSession session = (HashedSession)request.getSession(); - session.setAttribute("greeting", "New Hello"); - - response.setContentType("text/html"); - response.setStatus(HttpServletResponse.SC_OK); - response.getWriter().println( - "

Value 'New Hello' stored in session.

"); - } -}