Server-Side Cookies and Data Persistency
Use
You can use server-side cookies to make data persistent, especially page attributes.
To do this you use simple ABAP statements in the event handlers
OnManipulation and OnRequest, combined with methods of the class for server-side cookies:Server-side cookies are persistent data, similar to the usual client-side cookies. Whereas the size of cookies on the client is restricted to four kilobytes per cookie, 300 cookies in total, and 30 per server or domain, server-side cookies do not have any size or quantity restrictions.

Server-side cookies consist of large datasets that are customized and stored on the server. They are not transferred between client and server.
Functions
In the specified event handlers you use the following ABAP expressions and class methods:
The steps listed here are explained in more detail in the example below.
Example
In the following example you can see how, in a BSP, simple ABAP statements are used to group the page parameters (strings) using
export in one object. The cookie is then stored with method SET_SERVER_COOKIE of class CL_BSP_SERVER_SIDE_COOKIE. Similarly the cookie can be fetched again with GET_SERVER_COOKIE and returned with import.Page Attributes
This example contains some important page attributes that are to be defined as persistent. The list of page attributes looks like:
Page Attributes
|
Attribute Name |
Auto |
Typing Type |
Reference Type |
Description |
|
LAST_STRING_ADDED |
type |
STRING |
Last string added by user |
|
|
NEXT_STRING |
x |
type |
STRING |
Next string |
|
STRINGS |
type |
STRING_TABLE |
Table of strings |
Not all page attributes should be persistent- only the strings added by users. They are displayed in a table on the interface. Therefore, in this BSP application only the strings
LAST_STRING_ADDED and STRINGS are to be made persistent.Event Handler
In this BSP application the following event handlers are important:
This event handler is always triggered with inbound requests to restore the persistent data.
This is the last event handler before the response is sent. In this event handler the new values of the page attributes are stored in the form of server-side cookies.
OnManipulation is described first of all to explain the storage concept.
OnManipulation
In this event handler the values of specific page attributes are saved in the server-side cookie.
Both parameters are saved on the server and then combined in one object. The user details are written to the page data using the ABAP statement
export.
The
export object1 from F ... Objectn from F to data buffer F.
A data cluster is stored in data buffer
f, which must be of type XSTRING. All page attributes that are relevant here are described in a byte sequence of variable length (XSTRING).
In this BSP we use the following code to receive
XSTRING:data: PAGE_DATA type XSTRING.
data: NAME type STRING.
export LAST_STRING_ADDED from LAST_STRING_ADDED
STRINGS from STRINGS
to data buffer PAGE_DATA.
Next we have to make the server-side cookie
XSTRING persistent:NAME = SY-UNAME.
call method CL_BSP_SERVER_SIDE_COOKIE=>SET_SERVER_COOKIE
exporting
NAME = 'my_page_data'
APPLICATION_NAME = RUNTIME->APPLICATION_NAME
APPLICATION_NAMESPACE = RUNTIME -> APPLICATION_NAMESPACE
USERNAME = NAME
SESSION_ID = 'same_for_all'
DATA_NAME = 'page_data'
DATA_VALUE = PAGE_DATA
EXPIRY_TIME_REL = 3600
.
The export parameters mean:
|
Parameters |
Meaning |
|
NAME |
Any name can be chosen for the cookie |
|
APPLICATION_NAME APPLICATION_NAMESPACE USERNAME SESSION_ID |
Parameters which refer to the cookie and uniquely identify it. These are the name of the BSP application, the namespace of the BSP application, the user name and the session ID. |
|
DATA_NAME DATA_VALUE |
Name and value of the data; these parameters must match. |
|
EXPIRY_TIME_REL |
Details of relative expiry time. |
OnRequest
In this event handler the internal data structures of the requests are restored. The persistent page attributes are returned by the server-side cookie. So there are many parallels with the event handler OnManipulation.
The server-side cookie is fetched by calling method
GET_SERVER_COOKIE of class CL_BSP_SERVER_SIDE_COOKIE. The meaning of the parameters is essentially the same as for the parameters for the event handler OnManipulation (see above); only that here DATA_VALUE is a changing parameter.data: PAGE_DATA type XSTRING.
data: NAME type STRING.
* User administration can, for example, use the ABAP system name
NAME = SY-UNAME.
* The server-side cookie is fetched
call method CL_BSP_SERVER_SIDE_COOKIE=>GET_SERVER_COOKIE
exporting
NAME = 'my_page_data'
APPLICATION_NAME = RUNTIME->APPLICATION_NAME
APPLICATION_NAMESPACE = RUNTIME -> APPLICATION_NAMESPACE
USERNAME = NAME
SESSION_ID = 'same_for_all'
DATA_NAME = 'page_data'
CHANGING
DATA_VALUE = PAGE_DATA
.
Provided that the data values do actually have values and therefore are not empty, the persistent page attributes are now restored by the server-side cookie. This is implemented by the ABAP statement
import.
The syntax of
import Object1 = F ... Objectn = F from data buffer F.
The data object (or several data objects) are imported from the specified data buffer
F, which must be of type XSTRING. All data that has been stored using export ... to data buffer (see above) in the data buffer F and listed here, is imported. The system checks that the structure for export and import corresponds.In our example the call to restore the persistent cookies is:
if PAGE_DATA is not initial.
import LAST_STRING_ADDED = LAST_STRING_ADDED
STRINGS = STRINGS
from data buffer PAGE_DATA.
endif.
See also:
You will find this example in the system in BSP application
it00 (package SBSP_TEST) on page misc_page_persistence.htm.