Welcome!

PowerBuilder Authors: Chris Pollach, Yeshim Deniz, Jayaram Krishnaswamy, Kevin Benedict, Avi Rosenthal

Related Topics: PowerBuilder

PowerBuilder: Article

Building A JSP-JaguarCTS-PowerBuilder Bridge Part 2

Building A JSP-JaguarCTS-PowerBuilder Bridge Part 2

In the last issue of PBDJ (Vol. 8, issue 4) we prepared the ground for our JSP-JaguarCTS-PB bridge. In this article we code our n_hepekpbdj component and deploy it to JaguarCTS. This component will be capable of handling an HTML String or ResultSet. We then create a JSP page that calls this component, then list some of the possibilities for a JSP-Jaguar-CTS-PB bridge based on our experiences in building one for our Hepek Dual Architecture Framework.

Coding n_hepekpbdj
Last time we created an n_hepekpbdj component using the Jaguar Component Wizard; now it's time to create functionality. Open your hepekpbdj.pbl and start editing n_hepekpbdj, but first we need to add the following instance variables:

Protected ErrorLogging i_ErrorLogging
// Note: the datastore must be private in order to pass Jaguar validation
Private datastore ids_data
Add the code from Listing 1 to activate and deactivate the events for n_hepekpbdj. In activate event we create an error-logging service and a DataStore, and in deactivate event we destroy them and disconnect from the database. Our n_hepekpbdj component will be a stateless component that supports pooling.

Now we add four functions (see Listing 2): two public, of_html_action() and of_get_ resultset(), and two private, of_db_connect() and of_prepare_datastore(). As you can see from Listing 2, of_html_action() and of_get_resultset() are fairly straightforward. As arguments, both functions receive data objects, a connection cache name, and RDBMSs to connect to. This enables us to connect to any database connection cache defined on JaguarCTS and to manipulate data from any DataWindow deployed to JaguarCTS.

In addition, of_html_action() accepts action, context, and browser arguments. These parameters come from our Web client. The action argument defines which button the user pressed, the context contains information that has changed, and the browser identifies the user browser from which the call was made. We'll see how to get these parameters when we write a JSP page that will talk to n_hepekpbdj.

In both functions we first attempt to connect to the database by calling of_db_connect(). If the connection was successful, we initialize the transaction object and data object for the DataStore by calling of_prepare_data-store(). Note that the DataStore is created in the activate event of n_hepekpbdj, which occurs prior to the code in our functions. The activate event happens when the component is activated (pulled from the pool or created). In the case of of_html_action(), once we've connected to the database and prepared the DataStore, we set the browser, if any, and perform SetHTMLAction() using the action and context parameters that were sent to us from the JSP page. Sometimes this function returns a -5, which is undocumented in PowerBuilder. This usually happens if the action and context are reversed or are somehow defected in the marshaling process. In the case of of_get_result_set(), we perform a retrieve, then create and return a ResultSet to a client.

Creating a DataWindow
Now that we have our component we create a freeform DataWindow (see Figure 1).

Make sure your d_product DataWindow has the following SQL Syntax:

SELECT "product"."id",
"product"."name",
"product"."description",
"product"."prod_size",
"product"."color",
"product"."quantity",
"product"."unit_price"
FROM "product"

As you'll see, the order of the selected columns is important when we use ResultSet. Add the text field "Product Detail" to the header band and make sure it's underlined. Later in the article we'll show you a trick with Cascading Style Sheets related to this property. Add six buttons to the footer band similar to those shown in Figure 1. For those six buttons define the action as follows:

  • Back:PagePrior (5)
  • Insert:InsertRow (12)
  • Update:Update (13)
  • Retrieve:Retrieve (2)
  • Delete:DeleteRow(10)
  • Next:PageNext(4)
Now we need to set a couple of properties for our DataWindow and we'll be ready to deploy to JaguarCTS. Right-click on the DataWindow and select properties. On the General Tab make sure that HTML DataWindow is checked. On HTML Generation Tab set the following properties: Rows Per Page property is set to 1 in order for the DataWindow to display only one row at the time.

All the check boxes between Rows Per Page and Browser are checked as well. These properties tell HTMLGenerator to create all the necessary JavaScript.

Select a Browser property from the dropdown browser installed on your local machine. We need to set this for testing purposes.

Set Object Name property to say dw_hepek. Later, we'll use this property in JSP to get context and action arguments.

The Self Link property indicates where the form action will happen, so set it to HepekPBDJ.jsp. This property tells a DataWindow that's translated in form on the Web what page to call when one of the DataWindow buttons is clicked.

Once you set all the properties, save your DataWindow as d_product. Retrieve the data and then preview the DataWindow in your browser by selecting Design->HTML Preview. Everything should look dandy. Close the DataWindow, then open up the p_hepekpbdj_jag_deploy project and deploy it to JaguarCTS.

Creating and Compiling Java Stubs
Now that we've deployed our component to JaguarCTS, we need to create Java stubs. A Java stubs exposes methods to a Java client that are specified in the CORBA object's interface, which is defined using the Interface Definition Language (IDL). An IDL compiler translates the CORBA object definitions into a Java programming language according to the CORBA-Java language mappings. Java stubs, which can be thought of as client side, have their counterparts, called skeletons, on the server side. Java stubs and skeletons communicate with each other over IIOP using the Object Request Broker (ORB). The communication architecture that occurs between the JSP client and JaguarCTS is shown in Figure 2.

To create stubs, launch the Jaguar Manager. Open up the Packages folder and select HepekPBDJ package. Right-click and select Generate Stub/Skeleton and a new window opens. If you're using JaguarCTS 3.6.1 and your display is 800x600, you won't be able to see a whole window. You'll need to set the desktop area to 1024x768 in order to see the whole window.

Make sure the Generate Stubs check box at the top left corner is checked as well as Generate Java Stubs, and the dropdown box to the right should say CORBA. Select the Generate Java Files radio button, leave the Java Code Base as is, and check Compile Java Stubs. For those using JaguarCTS version 3.5, there's no option to compile stubs. You'll have to do it manually, which we'll show how to do later.

For the Java version you should have JDK 1.2. Once you're done, click generate and JaguarCTS will generate; if you're using JaguarCTS 3.6.1, it'll compile your stubs. JaguarCTS 3.5 users will have to compile the stubs themselves. Open MS-DOS and type:

cd %jaguar%\html\classes\HepekPBDJ
This is a directory in which JaguarCTS places Java stubs. To compile them, type:
javac *.java
Now you're all set as far as JaguarCTS and PowerBuilder are concerned.

Writing HepekPBDJ.jsp
Before we start writing the JSP page, we need to create a directory for it under Tomcat. We'll create, you guessed it, HepekPBDJ directory. Under %TOMCAT_HOME%\webapps create directory HepekPBDJ. If Tomcat is running, you'll need to restart it in order for Tomcat to recognize the new directory. Now we're ready to write our HepekPBDJ.jsp page. Instead of retyping it simply copy the HepekPBDJ.jsp file found on the PBDJ Web site. To enable you to follow the flow of a JSP page, we included comments and have numbered the sections in HepekPBDJ.jsp. We mentioned that we would show you a trick: include the HepekPBDJStyle.css stylesheet, also found on the PBDJ Web site. This stylesheet makes the underlining of Product Details text or any other column that's underlined look good.

In section one we start a JSP page in which we have a number of import statements. These import statements are analogous to the #include< file.h> statement in C or C++. For the last import statement we included the "HepekPBDJ.*" package that contains our n_hepekpbdj component/class.

In section two we've declared an assigned value to the variable ls_datawindow. As we noted in the comments you can pass the name of the DataWindow as an argument to this page. This allows you to Web-enable any DataWindow that you deployed on JaguarCTS. Of course, you'd then have to extend of_html_action() and of_get_result_set() to accept arguments so that you can work with DataWindows with arguments. This feature as well as Web linkage, query, and PDF reporting of any DataWindow is fully implemented in our Hepek Dual Architecture Framework. In section three we declare both a Tabular and a Java standard JDBC ResultSet.

JaguarCTS returns a Tabular ResultSet, the Jaguar IDL data type, and we'll convert it to a standard Java JDBC ResultSet. In section four we obtain the "mode" parameter from the request object. Note: In JSP there are nine implicit objects and request object (javax.Servlet.http.HttpServletRequest) is one of them. Table 1 provides a summary and a brief description of JSP implicit objects.

In section five we obtain the browser that the request came from. Remember, we use this value in n_hepekpbdj to set the browser property in the DataObject. Section six obtains action and context parameters, which are passed on to JaguarCTS. Based on these two parameters, we'll perform a Retrieve, Update, Insert, and more. Note that we obtain these two values from dw_hepek_action and dw_hepek_context parameters. The names of these variables are probably familiar, since they're based on what you enter for the "Object Name" property on the HTML Generation tab of your DataWindow in the DataWindow painter. If you enter "dummy" for "Object Name," to get action in a JSP you would write:

ls_dwAction = (String)request.getParameter("dummy_action"
)
In section seven we declare a variable of our component type, HepekPBDJ.n_hepekpbdj. In section eight we prepare properties that will enable us to create a session with JaguarCTS. Make sure your IIOP listener settings for a Host Name in Jaguar Manager match those in your JSP page. For example, if you installed the JaguarCTS server on a machine with the name MYJAGUAR and IP address 127.0.3.3, by default, when you install JaguarCTS on this machine the Host Name property for the IIOP listener will be MYJAGUAR. If you use an IP address in your JSP page, as we do, to look up the server, you'll be invoking two sessions on JaguarCTS. The reason for this is that Sybase has a cache of sockets in the client runtime. So when you perform a lookup, the object server gets confused because what's returned (machine name MYJAGUAR) and what's cached (IP address) is not the same. Since what is cached and what has been returned doesn't match, a new connection to JaguarCTS is opened, giving you two connections to the same server. Furthermore, the connections that failed to match will remain open until you restart JaguarCTS. They'll be piling up and eventually crash your server. Therefore you should make sure that the server's listeners and the lookup are the same.

In section nine we start looking up and resolving the objects' names on JaguarCTS, and once everything is done we create an instance of the class of the type HepekPBDJ.n_hepekpbdj. We could write an article on this, but for now that's all we need to know.

In section 10 we check to see if we're retrieving an HTML string or ResultSet and then call the appropriate function. Section 11 displays an HTML string if that's what we were retrieving. In sections 12 and 13 we cast/convert Jaguar's Tabular ResultSet into a JDBC ResultSet and build a table using a FOR loop. The Tabular ResultSet specifies a result set in a special table-based form. We cast/convert it into a normal JDBC ResultSet object so we can manipulate it in the usual way. In section 14 we obtain data from the ResultSet. Instead of passing a column number to getter methods of ResultSet, we can also pass a column name. Since we use column numbers it's important that we know the order of our SELECT statement.

Three - Two - One- GO
Now we're ready to test our craftwork. Make sure that JaguarCTS and Tomcat are running and then open your "favorite" Microsoft Internet Explorer. If you changed the HTTP listener on Tomcat, as described in the previous sections, visit http://yourhost:8888/HepekPBDJ/HepekPBDJ.jsp. If you modified JaguarCTS HTTP listener, visit http://yourhost:8080/HepekPBDJ/HepekPBDJ.jsp. In either case you should see something similar to Figure 3 in your browser.

Try out the DataWindow and see what happens when you start pressing buttons. However, if you're inserting a new row make sure that one of the fields on the form has focus. If you don't, a row will be inserted at the end and you won't see it since we're displaying only one row at the time. Let's see what happens if we want to display using ResultSet. Just add "?mode=RS" to your current URL. Your new URL will be http://yourhost:8080/HepekPBDJ/HepekPBDJ.jsp?mode=RS. You should see something like Figure 4 in your browser.

HepekAction.jsp
The exercises that we showed you in this article and the previous one are based on our experiences in creating JSP extensions for our Hepek Dual Architecture Framework. We created only partial functionality while Hepek DAF provides full support for JSP. In addition to a component on JaguarCTS that JSP talks to, Hepek DAF includes one JSP page, HepekAction.jsp. The Hepek Application Generator generates HepekAction.jsp so all you need to do is incorporate it. This page is all you need to build a complex site.

The HepekPBDJ.jsp that we showed you is partially based on HepekAction.jsp. Also the n_hepekpbdj JaguarCTS component that we created is based on the Hepek DAF Jaguar Manager component. When these are used together, they provide a number of services to the Web client. Hepek DAF provides a true linkage service with the use of dynamic frames. A query service similar to the PowerBuilder DataWindow Query Mode is also provided.

The PDF Reporting service is supported and we're currently putting final touches to it. The popup calendar using JavaScript also comes with Hepek DAF. Simply put, technologies like the one used in Hepek DAF, HepekAction.jsp, and, of course, the mighty JaguarCTS enable you as a PowerBuilder developer to create a formidable Web site with little or no knowledge of JSP. Figure 5 shows the Hepek DAF linkage and calendar service in action, and Figure 6 shows the Hepek DAF Web Query service.

The Web page in Figure 7 is a true BugMgr application developed with Hepek DAF, JaguarCTS, PowerBuilder, and JSP. Believe it or not, the Task List on the right-hand side of the screen is a PowerBuilder DataWindow. We simply obtained a static IP address, and we expect to start hosting our own Web site with JaguarCTS as an application server. This should happen within a month or two; you'll be able to look at these examples as well as PowerBuilder examples and download a trial version of Hepek DAF directly from our Web site. In the meantime, you can try the client/server version of Hepek from www.hepek.com.

Conclusion
We just showed you a small part of what can be done using the JSP-JaguarCTS-Power- Builder bridge. In this article we used a number of different technologies and tools. As a developer you should be aware and familiar with all of them. The life of a developer is not black and white anymore. You must learn to use all colors that are offered in the developers' palette in order to "paint" top-notch applications.

References

  1. Fields, D.K., and Kolb, M.A. (2000). Web Development with JavaServer Pages. Manning.
  2. Java technology: www.javasoft.com
  3. Marty Hall's Web site: www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html

More Stories By Tarik Makota

Tarik Makota has been a programmer for the past four years. Currently he is a project leader working in the Department of Youth and Community Development for the City of New York.

More Stories By Nermin Tanovic

Nermin Tanovic, a senior technology specialist at Bowne Technology Enterprise, a major financial printing company in New York, is the author of the Hepek PowerBuilder Framework. His web site is
www.hepek.com.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.