Added an example of embeded Jetty using hashed session and servlets. (#1107)

Signed-off-by: Luis Daniel <lalducin@nearsoft.com>
This commit is contained in:
Luis Daniel Alducin 2016-11-15 21:04:38 -06:00 committed by Greg Wilkins
parent 05f2e56aa1
commit 2c88bf0f83
3 changed files with 163 additions and 0 deletions

View File

@ -0,0 +1,52 @@
//
// ========================================================================
// 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 HelloSessionServlet extends HttpServlet
{
private String greeting;
public HelloSessionServlet() {}
@Override
protected void doGet( HttpServletRequest request,
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(
"<h1>" + greeting + " from HelloSessionServlet</h1>");
}
}

View File

@ -0,0 +1,64 @@
//
// ========================================================================
// 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.Server;
import org.eclipse.jetty.server.session.AbstractSessionIdManager;
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;
public class OneServletContextWithSession
{
public static void main(String[] args) throws Exception
{
Server server = new Server(8080);
SessionHandler handler = new SessionHandler();
handler.setServer(server);
HashSessionManager manager = new HashSessionManager();
handler.setSessionManager(manager);
AbstractSessionIdManager idManager = new HashSessionIdManager();
manager.setSessionIdManager(idManager);
server.setSessionIdManager(idManager);
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/*");
//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.
//Can be accessed using http://localhost:8080/hello
context.addServlet(HelloSessionServlet.class, "/");
server.start();
server.join();
}
}

View File

@ -0,0 +1,47 @@
//
// ========================================================================
// 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>");
}
}