Merge remote-tracking branch 'origin/jetty-9.3.x' into jetty-9.4.x
This commit is contained in:
commit
65b26336bd
|
@ -21,6 +21,7 @@ package org.eclipse.jetty.embedded;
|
||||||
import org.eclipse.jetty.server.session.HashedSession;
|
import org.eclipse.jetty.server.session.HashedSession;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServlet;
|
||||||
|
@ -29,8 +30,6 @@ import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
public class HelloSessionServlet extends HttpServlet
|
public class HelloSessionServlet extends HttpServlet
|
||||||
{
|
{
|
||||||
private String greeting;
|
|
||||||
|
|
||||||
public HelloSessionServlet() {}
|
public HelloSessionServlet() {}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -38,14 +37,47 @@ public class HelloSessionServlet extends HttpServlet
|
||||||
HttpServletResponse response ) throws ServletException,
|
HttpServletResponse response ) throws ServletException,
|
||||||
IOException
|
IOException
|
||||||
{
|
{
|
||||||
HashedSession session = (HashedSession)request.getSession();
|
|
||||||
if((greeting = (String)session.getAttribute("greeting")) == null) {
|
|
||||||
greeting = "Hello";
|
|
||||||
}
|
|
||||||
response.setContentType("text/html");
|
response.setContentType("text/html");
|
||||||
response.setStatus(HttpServletResponse.SC_OK);
|
response.setStatus(HttpServletResponse.SC_OK);
|
||||||
response.getWriter().println(
|
response.addHeader("Cache-Control","no-cache");
|
||||||
"<h1>" + greeting + " from HelloSessionServlet</h1>");
|
|
||||||
|
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 <a href=\"/\">here</a> 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 <a href=\"/?greeting=Hola\">here</a> to set a new greeting.";
|
||||||
|
}
|
||||||
|
|
||||||
|
PrintWriter out = response.getWriter();
|
||||||
|
out.println("<h1>" + greeting + " from HelloSessionServlet</h1>");
|
||||||
|
out.println("<p>" + message + "</p>");
|
||||||
|
out.println("<pre>");
|
||||||
|
out.println("session.getId() = " +session.getId());
|
||||||
|
out.println("session.isNew() = " +session.isNew());
|
||||||
|
out.println("</pre>");
|
||||||
|
out.println("<p>" + link + "</p>");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,29 +32,27 @@ public class OneServletContextWithSession
|
||||||
{
|
{
|
||||||
Server server = new Server(8080);
|
Server server = new Server(8080);
|
||||||
|
|
||||||
SessionHandler handler = new SessionHandler();
|
// Create an ID manager for the server. This is normally done
|
||||||
handler.setServer(server);
|
// by default, but is done explicitly here for demonstration.
|
||||||
|
|
||||||
HashSessionManager manager = new HashSessionManager();
|
|
||||||
handler.setSessionManager(manager);
|
|
||||||
|
|
||||||
AbstractSessionIdManager idManager = new HashSessionIdManager();
|
AbstractSessionIdManager idManager = new HashSessionIdManager();
|
||||||
manager.setSessionIdManager(idManager);
|
|
||||||
server.setSessionIdManager(idManager);
|
server.setSessionIdManager(idManager);
|
||||||
|
|
||||||
|
// Create a ServletContext, with a session handler enabled.
|
||||||
ServletContextHandler context = new ServletContextHandler(
|
ServletContextHandler context = new ServletContextHandler(
|
||||||
ServletContextHandler.SESSIONS);
|
ServletContextHandler.SESSIONS);
|
||||||
context.setContextPath("/");
|
context.setContextPath("/");
|
||||||
context.setResourceBase(System.getProperty("java.io.tmpdir"));
|
context.setResourceBase(System.getProperty("java.io.tmpdir"));
|
||||||
server.setHandler(context);
|
server.setHandler(context);
|
||||||
|
|
||||||
//Servlet to store a different greeting in the session
|
// Access the SessionHandler from the context.
|
||||||
//Can be accessed using http://localhost:8080/store
|
SessionHandler sessions = context.getSessionHandler();
|
||||||
context.addServlet(StoreInSessionServlet.class, "/store/*");
|
|
||||||
|
// 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
|
//Servlet to read/set the greeting stored in the session.
|
||||||
//StoreInSessionServlet a default greeting is shown, after visiting you will see
|
|
||||||
//a different greeting.
|
|
||||||
//Can be accessed using http://localhost:8080/hello
|
//Can be accessed using http://localhost:8080/hello
|
||||||
context.addServlet(HelloSessionServlet.class, "/");
|
context.addServlet(HelloSessionServlet.class, "/");
|
||||||
|
|
||||||
|
|
|
@ -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(
|
|
||||||
"<h1>Value 'New Hello' stored in session.</h1>");
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue