!--a11y-->
Application Class of a BSP Application 
Overview
A BSP application can include a multitude of different development objects. One of these objects is the application class of the BSP application.
The application class is a regular ABAP Objects class. As such, the application class can include any methods, attributes, and events the developers wants.
The application class is usually used to store data and make it available across BSP pages. This data is stored as attributes. In addition, the application class encapsulates BSP application logic in methods. This allows several BSP applications to use the same application class and provide one business application that contains different interfaces, such as for various devices, without having to replicate the business or application logic.
This also means the global
Object application can be used in the BSP application to access the attributes and methods of the application class.You do not have to use an application class in your BSP application. It is an optional way for you to structure your BSP application.
You use the Web Application Builder in transaction SE80 to assign an application class to a BSP application.


A simple example of where an application class could be useful would be a class for controlling dialog logic and maintaining data consistency in a BSP application for shopping. This application class would include a shopping basket (internal table or other object) as an attribute. There would also be methods for changing and processing the shopping basket, such as to add, change, or delete articles. Methods for determining prices, creating offers, and posting orders would also be helpful.
For an example of the use of an application class, see
In many cases, the application class is only used to encapsulate existing application functions, such as from SAP Customer Relationship Management (SAP CRM), and then access the function through BAPI interfaces. Using an application class to encapsulate the functions ensures that the BSP application functions are stored in a central location (in the application class) and that both implementation and distribution are transparent (local method call but remote BAPI call internally). If a customer or developer wants to change or adapt a BSP application or use the application for an additional device, they have the full functions of the original BSP application available in the interface of the application class.
Runtime Behavior
Any ABAP Objects class can potentially be used as an application class of a BSP application. However, the BSP runtime environment must treat the class as a singleton, that is, a class for which there is only one instance per session.
The lifetime of an application class depends on the state model of the BSP application. A BSP application can be stateful or stateless.
Stateful BSP Application
In
Stateful BSP Applications the only instance of the application class, the application object, is generated at the first request sent to the BSP application. The object is then available for the entire lifetime of the session. The end lifetime of the application object ends when the session ends.In stateful mode, the application class provides local buffering for data sets that are difficult to determine.
Stateless BSP Application
In
Stateless BSP Applications, the application context (roll area) is only available for the lifetime of a single request and is released at the end of the request. When the application context is released, all data and objects held by the session on the application server are also released. This includes the application object.This means the lifetime of the application object starts when the request is received and ends when a response is sent. The application objects is not available across several pages. Each page and each request interacts with a different instance of the application class.
In stateless mode, the application object cannot hold data across requests. For stateless applications, application classes are usually used to store the business logic in methods, but do not buffer data.
Accessing the Application Object
To access the application object, you use a typed object reference that is stored as the parameter
application in all event handlers of a BSP. Of course, you must ensure, however, that the parameter only exists if an application class is defined for a BSP application.Requirements for an Application Class
The only requirement for an application is that the constructor is parameter-less. If not, the application class cannot be generically instantiated by the BSP runtime environment.
Otherwise there are no other restrictions. You do need to ensure that the internal implementation of methods is chosen correctly, depending on the state mode where the class is implemented. For stateless applications, for example, it would be useless to implement expensive data gathering routines as these would be lost after every request. Instead, just get the exact data you need at that time. In stateful applications, you can implement an initialization phase where you get a large amount of data at one time, which can improve performance.
Application Events: The IF_BSP_APPLICATION_EVENTS Interface
In stateless BSP applications, an application often needs centralized control at certain times in the lifetime of the application. The BSP model provides this function for the application class in the predefined interface
IF_BSP_APPLICATION_EVENTS.When an application class implements the optional interface
IF_BSP_APPLICATION_EVENTS, the BSP runtime environment calls the interface methods at the relevant times. The following describes the methods and times:|
IF_BSP_APPLICATION_EVENTS~ON_START |
This method is called by the BSP runtime environment when the corresponding BSP application is first started at the start of the BSP session. This applies to both stateless and stateful applications. Typically, this time point is used to carry out authorization checks that apply to the entire application, or for preliminary data retrieval (in stateful applications). |
|
IF_BSP_APPLICATION_EVENTS~ON_STOP |
This method is called by the BSP runtime environment when the corresponding BSP application is explicitly ended. This applies to both stateless and stateful applications.
Please note that this time point is not available after every request in stateless BSP applications. In addition this time is not evaluated if the session is implicitly terminated by a timeout. Consequently, in this method it is only possible to execute optional operations that are not critical. Typically, this is a good time for cleanup operations such as deleting browser cookies or server-side cookies, if the application generated them. |
|
IF_BSP_APPLICATION_EVENTS~ON_REQUEST |
This method is called by the BSP runtime environment for every incoming request to a BSP before the BSP is given control (in the OnRequest event handler).This time can be used by the application class, for example, to restore attributes that were rescued in client- or server-side cookies in a previous request. |
|
IF_BSP_APPLICATION_EVENTS~ON_RESPONSE |
This method is called by the BSP runtime for every outgoing response of a BSP after the BSP has been processed (after the OnManipulationevent handler).This time can be used by a stateless application class for tasks such as rescuing attributes in client-side or server-side cookies. |
See also:
You can find details of interface
IF_BSP_APPLICATION_EVENTS in the reference documentation: Interface IF_BSP_APPLICATION_EVENTSApplication Basis Class CL_BSP_APPLICATION
If an application class does not already have a super class, it can be derived from the predefined base class
CL_BSP_APPLICATION. This class provides methods that are typically required by a BSP application for embedding in a Web environment. This is how information about the current BSP application (such as session timeout, current URL of BSP application, state mode, and so on) can be called or set.As the application object is an
application attribute in every BSP event handler, the methods of the CL_BSP_APPLICATION class are also available with the corresponding inheritance. This makes it easy to provide the relevant functionality to lower application levels using a single object reference.See also:
You can find details of basis class
CL_BSP_APPLICATION in the reference documentation: Class CL_BSP_APPLICATION