
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:
Using export you can store the data (strings) as a cluster in database tables and you can transfer them back again using import.
Class CL_BSP_SERVER_SIDE_COOKIE provides methods for setting, getting, deleting, and managing cookies on the server.
Server-side cookies are persistent data, similar to the usual client-side cookies. However, while on the client-side, there are restrictions that limit the size of cookies to 4 kilobytes per cookie, the number of cookies to 300 in total and 20 per server or domain, server-side cookies are subject to no such restrictions.
Server-side cookies consist of large datasets that are customized and stored on the server. They are not transferred between client and server.
In the specified event handlers you use the following ABAP expressions and class methods:
OnManipulation
export (benötigte Seite + Controller + Seitenattribute) to buffer XSTRING
CL_BSP_SERVER_SIDE_COOKIE->Methode für Ablage(XSTRING).
OnRequest
CL_BSP_SERVER_SIDE_COOKIE->Methode für Retrieval(XSTRING).
import (benötigte Seite + Controller + Seitenattribute) from buffer XSTRING.
The steps listed here are explained in more detail in the example below.
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 Attribute
This example contains some important page attributes that are to be defined as persistent. The list of page attributes looks like:
|
Attribute Name |
Auto |
Typing Kind |
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. 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 syntax is:
export Objekt1 from F ... Objektn 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.
* Die Benutzerverwaltung kann z.B. den ABAP-Systemnamen verwenden
NAME = SY-UNAME.
* Das Server-seitige Cookie wird geholt
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 is:
import Objekt1 = F ... Objektn = 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.
You will find this example in the system in BSP application it00 (package SBSP_TEST) on page misc_page_persistence.htm.