Source Code for Session Example

#include <sl/java.hpp>
#include <servlet/servlet_headers.h>
using namespace sl::java::lang;
using namespace sl::java::io;
using namespace sl::java::util;
using namespace servlet;
using namespace servlet::http;

class SessionExample : public HttpServlet {
public :
    void doGet(HttpServletRequest &request, HttpServletResponse &response)
        throw (ServletException)
    {
        response.setContentType("text/html;charset=ISO-8859-1");

        PrintWriter out = response.getWriter();
        
        HttpSession session = request.getSession(true);

        // print session info

        time_t create = session.getCreationTime();
        time_t access = session.getLastAccessedTime();
        out.println("ID " + session.getId());
        out.println("Created: ");
        out.println(::ctime(&create));
        out.println("Last Accessed: ");
        out.println(::ctime(&access));

        // set session info if needed

        std::string dataName  = request.getParameter("dataName");
        std::string dataValue = request.getParameter("dataValue");
        if (dataName.length() && dataValue.length())
            session.setAttribute(dataName, Object(new String(dataValue)));

        // print session contents

        Enumeration e = session.getAttributeNames();
        while (e.hasMoreElements()) {
            std::string name  = object_cast<std::string>(e.nextElement());
            std::string value = object_cast<std::string>(session.getAttribute(name));
            out.println(name + " = " + value);
        }
    }
};

SL_REGIST_LODABLE_CLASS(SessionExample);