BBjCookie::setMaxAge

Description

In BBj 12.00 and higher, this method sets the maximum age of the cookie in seconds.

A positive value indicates that the cookie will expire after that many seconds have passed. Note that the value is the maximum age when the cookie will expire, not the cookie's current age.

A negative value means that the cookie is not stored persistently and will be deleted when the Web browser exits. A zero value causes the cookie to be deleted.

Syntax

Return Value Method
void setMaxAge(int expiry)

Parameters

Parameter Description
expiry An integer specifying the maximum age of the cookie in seconds; if negative, means the cookie is not stored; if zero, deletes the cookie.

Return Value

None.

Example

CLASS PUBLIC CookieSettingExampleServlet 
    method public void service(BBxServletContext context!) 
       
        request! = context!.getRequest()
        response! = context!.getResponse()
        session! = request!.getSession()

        s! = response!.getOutputStream()
        
REM     Initialize the settings going into the cookie        
        domain! = request!.getServerName()
        maxAge! = 1000
        path! = request!.getRequestURI()
        isSecure! = request!.isSecure()
        value! = "Example Value"
        comment! = "This is an example cookie"
        
REM     Create the cookie
        cookie! = response!.createCookie("SampleCookie")
        cookie!.setDomain(domain!)
        cookie!.setMaxAge(maxAge!)
        cookie!.setPath(path!)
        cookie!.setSecure(isSecure!)
        cookie!.setHttpOnly(!isSecure!)
        cookie!.setValue(value!)
        cookie!.setComment(comment!)
        
REM     Add cookie to response to be set on client        
        response!.addCookie(cookie!)
        
        
        s!.write("<html><body>")
        s!.write("<h1>The following cookie was successfully added</h1>")
        s!.write("The BBjCookie '" + cookie!.getName() + "' ")
        s!.write("has the value '" + cookie!.getValue() + "' ")
        
        domain! = cookie!.getDomain()
        IF domain! = NULL() THEN
            domain! = "null"
        ENDIF
        
        path! = cookie!.getPath()
        IF path! = NULL() THEN
            path! = "null"
        ENDIF
        
        s!.write("for the domain " + domain! + " ")
        s!.write("at path " + path! + " ")
        s!.write("will expire in " + Integer.toString(cookie!.getMaxAge()) + " seconds ")
        
        IF cookie!.getSecure() THEN
            s!.write(" for HTTPS protocol ")
        ENDIF

        IF cookie!.isHttpOnly() THEN
            s!.write(" only for HTTP protocol ")
        ENDIF
        
        s!.write("is version " + Integer.toString(cookie!.getVersion()) + " ")
        
        IF cookie!.getComment() <> NULL() THEN
            s!.write("comment '" + cookie!.getComment() + "' ")
        ENDIF
        s!.write("</body></html>")
    METHODEND
CLASSEND

See Also

BBjAPI

BBxServletContext

BBxServletResponse

BBxServletRequest

BBjCookie