Complete Contents
Introduction
Chapter 1 About Netscape Application Server Extensions
Chapter 2 About the Netscape Extension Builder
Chapter 3 Introduction to Netscape's Interface Definition Language
Chapter 4 Designing a Netscape Extension
Chapter 5 Generating Output Files
Chapter 6 Completing Method Stubs
Chapter 7 Using Template Streaming
Chapter 8 Managing State and Session Information
Chapter 9 Using Object Pools
Chapter 10 Compiling the Extension Source Code
Chapter 11 Deploying and Managing a Netscape Extension
Chapter 12 Example Extension: HelloWorld
Appendix A C++ Helper Functions
Appendix B Java Helper Static Methods
Appendix C Java Class Decorations
Appendix D Reserved Words
Appendix E The ConnManager.cpp File
Glossary
Previous Next Contents Index


Example Extension: HelloWorld

This section of Developer's Guide explains the steps that were performed to create the sample extension HelloWorld and application.

The following topics are included in this chapter:


About the Example
Netscape Extension Builder includes a sample directory that contains a C++ extension named HelloWorld and a Java servlet that uses the extension. The steps to follow can be grouped into the following project phases:


Design the Extension
Designing the sample extension consists of four main tasks:

  1. Open Netscape Extension Builder Designer.
  2. Create an access module.
  3. Create a service module.
  4. Generate IDL files.
Open Netscape Extension Builder Designer

  1. On Windows NT, double-click the Netscape Extension Builder icon or run the neb command. On Solaris, run the neb command. The neb command executable is in the bin directory of your Netscape Extension Builder installation directory.
  2. The Netscape Extension Builder Designer start-up window appears.

  3. Name the extension HelloWorld.
Create an Access Module

  1. In the tree view, highlight the iSample access module.
  2. Name the module ihello.
  3. In the access module, define an interface named IHelloWorld.
  4. In the interface, define a method named helloThere.
  5. In the method, define a parameter named pHelloMessage. Define it as an Out parameter of type String.
Create a Service Module

  1. In the tree view, highlight the service module.
  2. Name the module chello. Leave the values for service module decorations as they are.
  3. In the service module, define a coclass named CHelloWorld.
  4. Change the value of the Manager Class decoration to Yes. This marks CHelloWorld as the manager class for the chello service.
  5. Leave the other values for CHelloWorld decorations as they are.
  6. In the CHelloWorld coclass, implement the IHelloWorld interface.
  7. In the IHelloWorld interface, decorate the parameter of the helloThere method. Set the value of the pHelloMessage parameter to 256. This sets the maximum buffer size for the out string parameter.
Generate IDL Files
Select the Generate IDL command to produce IDL files and makefiles.


Complete the Extension
Note. Stop the Netscape Application Server before compiling the extension and then re-start it before testing the application. Extensions are loaded as you restart Netscape Application Server.

  1. Generate source code by running the gmake command from the top-level directory of the extension.
Windows
gmake -f GNUmakefile.nt

Solaris
gmake -f GNUmakefile.sun

  1. Go to the cpp\chello\ directory and locate the stub file named CHelloWorld.cpp. This file corresponds to the class you created.
  2. Edit CHelloWorld.cpp by completing the stub for the helloThere method.
  3. Note: in the sample extension provided, the cpp\chello\CHelloWorld.cpp file already contains the completed stub. To view the stub file before it is completed, compare the sample code in cpp\gen.chello\CHelloWorld.cpp.

  4. Rerun the gmake utility from the cpp\chello directory. In general, you only need to rerun gmake from directories in which you've edited files.
  5. Register the extension:
  6. kreg < Hello.gxr

    Registering an extension makes it available to C++ and Java applications.


Build the Application

  1. Locate the callHello.java application in the sample\hello\servlet directory.
  2. Edit the application to use the accessors for the extension. When you are done, the application should look like the following:
  3. package HelloTest;

    import java.lang.*;

    import java.util.*;

    import java.io.*;

    import javax.servlet.*;

    import javax.servlet.http.*;

    import com.netscape.server.servlet.extension.*;

    import com.kivasoft.applogic.*;

    import helloExtension.*; // extension classes

    public class callHello extends HttpServlet {

    /**

    * Call the hello extension

    */

    public void service(HttpServletRequest req, HttpServletResponse res)

    throws ServletException, IOException

    {

    // access servlet's underlying AppLogic instance

    HttpServletRequest2 req2;

    req2 = (HttpServletRequest2) req; // legal cast within NAS

    AppLogic al = req2.getAppLogic();

    com.kivasoft.IContext alContext;

    alContext = al.context;

    // Attempt to get to the helloExtension through the accessor

    IHelloWorld hello=access_chello.getchello(alContext, null, al);

    // Let's build the result string

    String result;

    // Go to the extension to get the greeting

    if (hello != null) {

    result = hello.helloThere();

    }

    else {

    result = "ERROR! Can't get access to the Hello extension.";

    }

    req.setAttribute("strHelloMsg", result);

    res.setContentType("text/html");

    RequestDispatcher dispatcher;

    dispatcher = getServletContext().getRequestDispatcher("HelloTest/
    hello.jsp");

    dispatcher.include(req, res);

    return;

    }

    }

  4. After you finish editing the application code, register the application:
  5. kreg < appinfo.gxr

Note. You can also register the application by running servletreg on appInfo.ntv.

  1. Copy hello.jsp into the <NAS_ROOTDIR>\APPS\helloTest directory.
  2. Copy appInfo.ntv and servInfo.ntv into the <NAS_ROOTDIR>\APPS\helloTest\ntv directory.

Test the Application
Before testing the application, verify that Netscape Enterprise Server (Web server) and Netscape Application Server are running. To test the application:

  1. From your web browser, load a test page that uses the application. Point your browser to http://<my_host_name>/extensions, and select the Hello example.
  2. Test the application from the web page.
  3. You have completed all tasks for the code walkthrough.

 

Copyright © 2000 Sun Microsystems, Inc. Some preexisting portions Copyright © 2000 Netscape Communications Corp. All rights reserved.