BBxWebSession::getCreationTime

Description

In BBj 21.00 and higher, this method returns the creation time of the BBxWebSession.

Syntax

Return Value Method
long getCreationTime()

Parameters

None.

Return Value

A long value representing the creation time of the BBxWebSession, measured in milliseconds since midnight January 1, 1970 GMT.

Remarks

This method is inherited from the Java HttpSession object.

Example

REM /**
REM  * Enter the values for your username, password, and the filepath to this file
REM  */
username$ = "admin"
password$ = "admin123"
pathToThisFile$ = "/path/to/BBxWebSessionDemo.bbj"

context! = BBjAPI().getAdmin(username$, password$).getJettyServerConfig().getCustomContext("root")
context!.addBBxServlet("/BBxWebSessionDemo", pathToThisFile$, "BBxWebSessionDemo", "bbxWebSessionDemo", 1) 
BBjAPI().getAdmin(username$, password$).getJettyServer().getContext("root").restart()

CLASS PUBLIC BBxWebSessionDemo

    REM Demonstration of various BBxWebSession capabilities
    METHOD PUBLIC VOID bbxWebSessionDemo(BBxServletContext context!)
        REM  Get the request and response objects from the BBxServletContext
        request! = context!.getRequest()
        response! = context!.getResponse()
        
        REM Get ServletOutputStream from BBxServletResponse
        out! = response!.getOutputStream()
        
        REM Get the BBxWebSession
        session! = request!.getSession()
        
        out!.write("<h2>BBxWebSession</h2>")
        out!.write("<p>BBxWebSessions can be retrieved through either BBxServletContext or BBxServletRequest through ")
        out!.write("the <code>getSession()</code> method. A BBxWebSession is created using the JSESSIONID cookie and ")
        out!.write("is different for each client connecting to the servlet.  This makes the BBxWebSession useful in ")
        out!.write("keeping track of client information. Below are demonstrations of the various actions that can be ")
        out!.write("carried out with BBxWebSessions.</p>")
        
        REM Demonstration of session invalidation
        #sessionInvalidationDemo(context!)
        out!.write("<p>See <code>sessionInvalidationDemo</code> in source code for method usage examples</p>")
        
        REM Demonstration of attribute setting and removal in BBxWebSessions
        #sessionAttributeDemo(context!)
        out!.write("<p>See <code>sessionAttributeDemo</code> in source code for method usage examples</p>")
        
        REM Demonstration of methods relating to the maxInactiveInterval value of the BBxWebSession
        #maxInactiveIntervalDemo(context!)
        out!.write("<p>See <code>maxInactiveIntervalDemo</code> in source code for method usage examples</p>")
        
        REM Demonstration of the various informational methods in BBxWebSessions
        #sessionInfoMethodsDemo(context!)
        out!.write("<p>See <code>sessionInfoMethodsDemo</code> in source code for method usage examples</p>")
        
    METHODEND
    
    REM Demonstration of the isNew() and invalidate() methods in BBxWebSession
    METHOD PUBLIC VOID sessionInvalidationDemo(BBxServletContext context!)
        REM  Get the request and response objects from the BBxServletContext
        request! = context!.getRequest()
        response! = context!.getResponse()
        
        REM Get ServletOutputStream from BBxServletResponse
        out! = response!.getOutputStream()
        
        REM Get the BBxWebSession
        session! = request!.getSession()
        
        REM Check parameters for invalidateSession parameter
        invalidateSession! = request!.getParameter("invalidateSession")
        
        REM Invalidate session if invalidateSession parameter is set to true
        IF invalidateSession! = "true" THEN
            
            REM Invalidate the session
            session!.invalidate()
            
            REM Get a new session
            session! = request!.getSession()
        FI
        
        REM get the return value of isNew()
        isNew! = session!.isNew()
        
        out!.write("<h3>Session Invalidation</h3>")
        out!.write("<p>Invalidating a session in BBxWebSession causes the JSESSIONID cookie to be deleted causing a ")
        out!.write("new one to be created automatically, and thus a new BBxWebSession is created as well.</p>")
        out!.write("<p>Below is the return value for <code>isNew()</code> which will return true if the session was ")
        out!.write("just created, and false if it was not.</p>")
        
        out!.write("<code><ul><li>boolean isNew() = "+Boolean.toString(isNew!)+"</li></ul></code>")
        
        REM An HTML form to add invalidate Session parameter to the URL
        out!.write("Try invalidating the session below to see how all other session values change:")
        out!.write("<form method='GET' enctype='multipart/form-data' action='"+request!.getServletPath()+"'>")
        out!.write("<input type='hidden' name='invalidateSession' value='true'>")
        out!.write("<input type='submit' value='Press'> to <b>invalidate</b> the BBxWebSession!")
        out!.write("</form>")
    METHODEND
    
    REM Demonstration of getAttributeNames(), getAttribute(name$), setAttribute(name$, value!), and removeAttribute(name$) in BBxWebResponse
    METHOD PUBLIC VOID sessionAttributeDemo(BBxServletContext context!)
        REM  Get the request and response objects from the BBxServletContext
        request! = context!.getRequest()
        response! = context!.getResponse()
        
        REM Get ServletOutputStream from BBxServletResponse
        out! = response!.getOutputStream()
        
        REM Get the BBxWebSession
        session! = request!.getSession()
        
        REM Check URL for setSessionAttrName and setSessionAttrValue parameters
        setAttrName! = request!.getParameter("setSessionAttrName")
        setAttrVal! = request!.getParameter("setSessionAttrVal")
        
        REM Set the attribute name and value in the BBxWebSession
        IF setAttrName!.length() > 0 THEN
            session!.setAttribute(setAttrName!, setAttrVal!)
        FI
        
        REM Check URL for removeSessionAttr parameter
        removeSessionAttribute! = request!.getParameter("removeSessionAttr")
        
        REM Remove the specified session attribute if one exists
        IF removeSessionAttribute!.length() > 0 THEN
            session!.removeAttribute(removeSessionAttribute!)
        FI
        
        REM Get BBjVector of session attribute names
        attributeNames! = session!.getAttributeNames()
        
        REM Get attribute values for each attribute name
        getAttributeString$ = ""
        IF attributeNames!.size() > 0 THEN
            FOR N=0 TO attributeNames!.size()-1
                attrName$ = attributeNames!.getItem(N)
                
                REM get attribute with name
                attribute! = session!.getAttribute(attrName$)
                
                REM Add attribute to output string
                getAttributeString$ = getAttributeString$ + "<li>getAttribute(<em>"+attrName$+"</em>) = "+String.valueOf(CAST(Object, attribute!))+"</li>"
            NEXT N
        FI
        
        out!.write("<h3>Session Attributes</h3>")
        out!.write("<p>Session attributes can be used to store information about a client that will persist for as ")
        out!.write("long as the JSESSIONID cookie is valid.  Below are the return values for getAttributeNames() and ")
        out!.write("getSessionAttribute(name$)</p>")
        out!.write("<code><ul>")
        out!.write("<li>BBjVector getAttributeNames() = "+String.valueOf(CAST(Object, attributeNames!))+"</li>")
        out!.write("<li>Object getAttribute(String name$) : <ul>"+getAttributeString$+"</ul></li>")
        out!.write("</ul></code>")
        
        
        REM An HTML form to add attributes to the session
        out!.write("Enter the name and value of a request attribute to add:")
        out!.write("<form method='GET' enctype='multipart/form-data' action='"+request!.getServletPath()+"'>")
        out!.write("Name: <input type='text' name='setSessionAttrName'> Value: <input type='text' name='setSessionAttrVal'> ")
        out!.write("<input type='submit' value='Press'> to <b>set</b> an attribute in BBxWebSession to the value!")
        out!.write("</form>")
        
        REM An HTML form to remove attributes from the session
        out!.write("Enter the name of a request attribute to remove:")
        out!.write("<form method='GET' enctype='multipart/form-data' action='"+request!.getServletPath()+"'>")
        out!.write("Name: <input type='text' name='removeSessionAttr'> ")
        out!.write("<input type='submit' value='Press'> to <b>remove</b> the specified attribute from the BBxWebSession!")
        out!.write("</form>")
    METHODEND
    
    REM Demonstration of getMaxInactiveInterval and setMaxInactiveInterval
    METHOD PUBLIC VOID maxInactiveIntervalDemo(BBxServletContext context!)
        REM  Get the request and response objects from the BBxServletContext
        request! = context!.getRequest()
        response! = context!.getResponse()
        
        REM Get ServletOutputStream from BBxServletResponse
        out! = response!.getOutputStream()
        
        REM Get the BBxWebSession
        session! = request!.getSession()
        
        REM Check URL parameters for setMaxInactiveInterval
        setMaxInactiveInterval! = request!.getParameter("setMaxInactiveInterval")
        IF setMaxInactiveInterval!.length() > 0 THEN
            
            REM Set the max inactive interval to the specified value
            session!.setMaxInactiveInterval(Integer.parseInt(setMaxInactiveInterval!))    
            
        FI
        
        REM Get the value for getMaxInactiveInterval
        maxInactiveInterval! = session!.getMaxInactiveInterval()
        
        out!.write("<h3>Max Inactive Interval</h3>")
        out!.write("<p>The max inactive interval of a BBxWebSession is the maximum amount of inactive time in seconds ")
        out!.write("that can pass before the BBxWebSession invalidates itself. The default value is -1 which causes ")
        out!.write("the BBxWebSession to never become invalid. To change this value, the ")
        out!.write("<code>setMaxInactiveInterval(int seconds)</code> method can be used. Below is the current value ")
        out!.write("for getMaxInactiveInterval().</p>")
        
        out!.write("<code><ul><li>int getMaxInactiveInterval() = "+String.valueOf(maxInactiveInterval!)+"</li></ul></code>")
        
        REM An HTML form to add setMaxInactiveInterval to URL
        out!.write("Enter the number of seconds to set the max inactive interval to:")
        out!.write("<form method='GET' enctype='multipart/form-data' action='"+request!.getServletPath()+"'>")
        out!.write("Max Inactive Interval: <input type='text' name='setMaxInactiveInterval'> ")
        out!.write("<input type='submit' value='Press'> to <b>set the max inactive interval</b> to the specified value!")
        out!.write("</form>")
    METHODEND
    
    REM Demonstraton of various informational methods in BBxWebSession
    METHOD PUBLIC VOID sessionInfoMethodsDemo(BBxServletContext context!)
        REM  Get the request and response objects from the BBxServletContext
        request! = context!.getRequest()
        response! = context!.getResponse()
        
        REM Get ServletOutputStream from BBxServletResponse
        out! = response!.getOutputStream()
        
        REM Get the BBxWebSession
        session! = request!.getSession()
        
        REM Get values from informational methods
        creationTime! = session!.getCreationTime()
        id! = session!.getId()
        lastAccessedTime! = session!.getLastAccessedTime()
        
        out!.write("<h3>BBxWebSession Info</h3>")
        out!.write("<p>BBxWebSession also has a number of informational methods that can be seen below.</p>")
        out!.write("<code><ul>")
        out!.write("<li>long getCreationTime() = "+String.valueOf(creationTime!)+"</li>")
        out!.write("<li>String getId() = "+id!+"</li>")
        out!.write("<li>long getLastAccessedTime() = "+String.valueOf(lastAccessedTime!)+"</li>")
        out!.write("</code></ul>")
        
    METHODEND
CLASSEND

ClosedVersion History

  • BBj 21.00: BBxServlet introduced.

See Also

BBjAPI

BBxServlet

BBxServlet Tutorial

BBxWebSession

See the BBj Object Diagram for an illustration of the relationship between BBj Objects.