How to Create an HTTP Session

Sessions can be created using the SessionID option in the sa_set_http_option system procedure. The session ID can be defined by any non-null string.

Session state management is supported by URLs and cookies. HTTP sessions can be accessed using HTTP cookies, or through the URL of a GET request or from within the body of a POST (x-www-form-urlencoded) request. For example, the following URL utilizes the XYZ database connection when it executes:

http://localhost/sa_svc?SESSIONID=XYZ

The request is processed as a standard session-less request if an XYZ database connection does not exist.

Example

The following code illustrates how to create a RAW web service that creates and deletes sessions. A connection scope variable named request_count is incremented each time an HTTP request is made while specifying a valid SessionID.

CREATE SERVICE mysession
    TYPE 'RAW'
    AUTHORIZATION OFF
    USER DBA
    AS CALL mysession_proc();

CREATE PROCEDURE mysession_proc()
BEGIN
    DECLARE body LONG VARCHAR;
    DECLARE hostname LONG VARCHAR;
    DECLARE svcname LONG VARCHAR;
    DECLARE sesid LONG VARCHAR;

    CALL sa_set_http_header ( 'Content-Type', 'text/html' );
    SELECT CONNECTION_PROPERTY('SessionID') INTO sesid;
    SELECT CONNECTION_PROPERTY('HttpServiceName') INTO svcname;
    SELECT HTTP_HEADER( 'Host' ) INTO hostname;
    IF HTTP_VARIABLE('delete') IS NOT NULL THEN
        CALL sa_set_http_option( 'SessionID', NULL );
        SET body = '<html><body>Deleted ' || sesid
            || '</BR><a href="http://' || hostname || '/' || svcname || '">Start Again</a>';
        SELECT body;
    END IF;
    IF sesid = '' THEN
        SET sesid = set_session_url();
        CREATE VARIABLE request_count INT;
        SET request_count = 0;

        SET body = '<html><body> Created session ID ' || sesid
            || '</br><a href="http://' || hostname || '/' || svcname 
            || '?SessionID=' || sesid || '"> Enter into Session</a>';
    ELSE
        SELECT CONNECTION_PROPERTY('SessionID') INTO sesid;
        SET request_count = request_count +1;      
        SET body = '<html><body>Session ' || sesid || '</br>'
            || 'created ' || CONNECTION_PROPERTY('SessionCreateTime') || '</br>'
            || 'last access ' || CONNECTION_PROPERTY('SessionLastTime') || '</br>'
            || 'connection ID ' || CONNECTION_PROPERTY('Number') || '</br>'
            || '<h3>REQUEST COUNT is '|| request_count || '</h3><hr></br>'
            || '<a href="http://' || hostname || '/' || svcname 
            || '?SessionID=' || sesid || '">Enter into Session</a></br>'
            || '<a href="http://' || hostname || '/' || svcname 
            || '?SessionID=' || sesid || '&delete">Delete Session</a>';
    END IF;

    SELECT body;
END;