Show TOC

Registering Runtime Views Using ServletsLocate this document in the navigation structure

Use

You can register the custom runtime views you have created using a servlet that is executed each time the service starts.

The following procedure describes how you can do this using the SAP NetWeaver Developer Studio (NWDS).

Prerequisites
  • You have created a content component or a contextual panel component. For more information, see Creating Custom Runtime Views Using the GP API .
  • The versions of the Java Guided Procedures (GP) you use and your development track must match.
Procedure
  1. Open the Development Infrastructure perspective and create a development component (DC) of type J2EE → EnterpriseApplication.
  2. Create a second DC of type J2EE → Web Module.
  3. Add the Web module to the enterprise application:
    1. Open the J2EE perspective and in the Project Explorer, select the enterprise application component from step 1.
    2. Navigate to the Modules folder of the application and from the context menu choose Add/Remove.
    3. From the Available J2EE Modules list, select the Web module component from step 2 and choose OK.
  4. In the Development Infrastructure perspective, select the Web module DC and add the following build-, deploy- and runtime dependencies:
    • GP-CORE  → caf/eu/gp/api (external)
    • ENGFACADE →   tc/bl/exception/lib (default)
    • GP-CORE  → caf/eu/gp/api/wd (ContentComponent)
  5. Repeat the step above for the enterprise application DC.
  6. To create a servlet, open the context menu of the Web module DC and choose New  → Servlet…
    1. Provide a servlet name and package.
    2. Set the type to HTTP Servlet.
    3. Enable the init() and destroy() servlet methods.
  7. In the new servlet class, replace the automatically-generated code with the one listed below:

Example Servlet Code
packagecom.testcustomer;
 
importjava.io.IOException;
importjava.util.Enumeration;
importjavax.servlet.ServletConfig;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
 
importcom.sap.caf.eu.gp.rtview.api.GPRTViewFactory;
importcom.sap.caf.eu.gp.rtview.api.IGPRTView;
importcom.sap.caf.eu.gp.rtview.api.IGPRTViewManager;
importcom.sap.caf.eu.gp.wdcomponent.api.GPWDComponentFactory;
importcom.sap.caf.eu.gp.wdcomponent.api.IGPWDComponent;
importcom.sap.caf.eu.gp.wdcomponent.api.IGPWDComponentManager;
importcom.sap.localization.ResourceAccessor;
 
publicclass RegisterView extends HttpServlet {
   // a unique identifier for view
   final static String TYPE_NAME = "TestView";
   /**
    * init() is called once at the instantiation of the servlet
    * We register our new view here.
    */
   public void init(ServletConfig cfg) throws ServletException {
      super.init(cfg);
      boolean isRegistered = false;
  
      String viewId = TYPE_NAME;
      IGPRTViewManager vManager = GPRTViewFactory.getRTViewManager();
      try {
         // check if view is already registered:
         Enumeration rViews = vManager.getRTViews();
         while (rViews.hasMoreElements()){
            IGPRTView view = (IGPRTView)rViews.nextElement();
            if (view.getRTViewId().equals(viewId)){
               isRegistered = true;
               break;
            }
         }
         // if it is not registered
         if (isRegistered == false) {
            // instantiate the appropriate managers
            IGPWDComponentManager mgr = GPWDComponentFactory.getWDComponentManager();
           // create and register a Web Dynpro Component
            ResourceAccessor resourceAccessor = null;
            IGPWDComponent contentComponent = mgr.createWDComponent(
                    "MyOwnContentPane",
                     "com.sap.caf.eu.gp.runtime.view.CContentPane",
                    "sap.com/caf~eu~gp~runtime~view",
                    resourceAccessor,
                     "FrameTitle");
            // create and register a runtime view configuration
            IGPRTView viewConfiguration = vManager.createRTView(
                     viewId,
                     contentComponent,
                     resourceAccessor,
                     "PaneTitle",
                     "PaneDescription",
                     IGPRTView.MANDATORY_GROUP_PROCESS_STRUCTURE,
                     false);
            vManager.registerRTView(viewConfiguration); 
         }
      } catch (Exception e) {
         e.printStackTrace();         
      }
   }
   /**
    * destroy() is called when the instance of the servlet is destroyed
    * We unregister our new view here.
    */
   public void destroy() {
      super.destroy();
 
      String viewId = TYPE_NAME;
      IGPRTViewManager vManager = GPRTViewFactory.getRTViewManager();
      try {
         // unregister the runtime view
         vManager.unregisterRTView(viewId);
      } catch (Exception e) {
         e.printStackTrace();
      }    
   }
}
  1. In the Project Explorer, expand the node of the Web module DC and select WebContent → WEB-INF → web.xml.
  2. From the context menu, choose Open and open the Web Objects tab page:
    1. Expand the Servlets node and select the servlet you created previously.
    2. In the load on startup field, enter 1 .

      As a result, the servlet is executed every time the service starts.

  3. Save, build and deploy your enterprise application.

More Information

Registering Web Dynpro Components and Runtime Views