| By Millard F. Brown | Article Rating: |
|
| October 1, 2001 12:00 AM EDT | Reads: |
324 |
PowerBuilder 9 will introduce faithful PB developers to the ability to build and use JavaServer Pages (JSPs) in their PowerBuilder Web applications. This is a giant step forward in Sybase's continuing march toward industry-leading openness. All of PowerBuilder's capabilities, including the graphical development environment, the system tree, powerful SCC integration, rich Web Targets and object models, wizards for rapid development, and integrated EAServer development, can now be applied to JSP Web Target development.
Why JSP?
JavaServer Pages are part of the J2EE 1.2 Specification. What is a JSP? Simply put, JSPs are a powerful way of incorporating dynamic content into a Web page. You, as a developer, insert simple tags into an ordinary HTML page and place Java code within the tags that will execute on the server. Thus the powerful and widely used Java language is directly available within your Web pages. You can instantiate classes, invoke JavaBeans and Enterprise JavaBeans (EJBs), access application server components, utilize databases, and execute logic and flow control within your Web page. In short, JSPs give you a powerful page server capability with dynamic server-side scripting.
Wait a minute! "Page server capability with dynamic server-side scripting?" This sounds like Dynamo. That's true. JSPs provide capabilities that are very similar to those of Dynamo. Why use JSPs then? JSPs operate within the J2EE environment. JSPs also use Java as their language, rather than YAPL (Yet Another Programming Language) such as DynaScript. At the moment, the choice is yours whether to use JSP or Dynamo as your page server. However, Dynamo has not been enhanced significantly for some time. And, since Sybase has added support for JSP and seems to be focused on that technology for Web applications, it would be hard to justify developing new applications based on PowerDynamo.
JSPs - How Do They Work?
JSPs run in a "JSP container" that communicates with your Web server. The JSP container receives a request, executes server-side code within the page, and formats a response that's sent back to the server, and, ultimately, to the requesting client (browser). Remember, the Java code embedded in a JSP is executed on the server before the page is returned to the client. What's returned to the client is straight HTML, composed, controlled, and formatted by way of the server-side Java code. Internally, JSPs are servlets that run within the JSP container. The simplest of JSPs is nothing more than an HTML page.
JSP Containers: EAServer and Tomcat
PowerBuilder 9 will allow you to create JSPs and directly deploy them as a Web application to either Tomcat or EAServer JSP containers. Choosing the JSP container is part of the deployment descriptor that you associate with your JSP target. You'll see how all of this fits together later in this article.
What's in a JSP?
First, a JSP contains static marked-up text (HTML). In addition, you can place scripting elements (denoted by special tags), standard tags (such as <jsp:useBean >) that allow you to perform common actions, and custom tags as defined in the custom tag libraries (taglibs) that you associate with the JSP.
Now the Disclaimer
This article is based on PowerBuilder 9 beta 3. Some of the capabilities are incomplete, and I expect quite a few changes in configuration and usage by the time the product is released for general availability.
Five Steps to JSP Enlightenment
What follows is a five-step program for getting familiar with JSP development in PowerBuilder. I'll use EAServer as both the Web server and the JSP container. Check the documentation that comes with PowerBuilder and EAServer for details on using other Web servers and for setting up Tomcat. Please note that the code for these examples can be found at the PBDJ Web site at www.sys-con.com/pbdj/sourcec.cfm.
Step 1: Setting Things Up - EAServer and PB
You'll need Windows NT4, SP6a, or Windows 2000 to use PowerBuilder 9. Win9x will not be supported. Also plan on using a fast machine with lots of memory (I like 1GHz or better and 512MB minimum).
Beta 3 of PowerBuilder 9 expands into two images, CD1 and CD2. The first "CD" is the common install for PB9 and related components, and the second is for EAServer Developer 4.1.2. Before you begin installation though, you'll need to have Internet Explorer version 6 on your system. PowerBuilder 9 Web Targets require IE6. You can upgrade to IE6 free of charge from the Microsoft Web site.
Once you have IE6 set up, install EAServer 4.1.2 from the second CD image. JSP targets will not deploy correctly to earlier versions of EAServer.
Note: You should do a complete uninstall, including a sweep through your registry to remove references to earlier versions of EAServer.
Next, install PowerBuilder and related products from the CD1 image. Be sure to include Web Targets in your installation. If you don't see them as an option, check that you have completed the installation of IE6, including any required system restarts. After the installation is complete, restart the system, then check your CLASSPATH and PATH environment variables.
CLASSPATH is an important consideration for the proper operation of EAServer and PB JSP targets. Earlier versions of JDK, PowerBuilder, PowerJ, EAServer, and other products will have left a hodgepodge of folders and .jar and .zip files in your CLASSPATH. Edit CLASSPATH carefully to remove all references to earlier versions of software. JDK 1.3 is necessary for proper deployment and execution of your JSPs, so make sure your CLASSPATH references are for JDK 1.3.
Note: If you intend to use only PB and EAServer on your machine, you can remove CLASSPATH entirely. Both EAServer and PowerBuilder maintain their own CLASSPATH exclusive of the system CLASSPATH. Also be sure to resolve any duplications, errors, or conflicts between the SYSTEM and USER environment variables.
Your PATH variable must also be accorded some TLC. Once again, edit it carefully since applications seem to regard PATH as their own folder motel: "Folders check in, but they don't check out!" Remove obsolete references and conflicts and add the following to your PATH:
- %JAVA_HOME%\jre\bin\classic
- %JDK_LATEST%\bin
- %JDK_LATEST%\dll
JAVA_HOME needs to be present as a system or user environment variable. It should point to the root of your JDK 1.3 installation (such as C:\Program Files\Sybase\Shared\Sun\jdk\jdk1.3). Set JDK_LATEST to point to the same location.
After you've configured your CLASSPATH, PATH, and other environment variables, restart the system. This will reestablish your PATH and unload any remnants of JDK that may have been using your prior CLASSPATH.
Start EAServer and observe the screen as it's loading. You should see that EAServer is using JDK 1.3 and there are no startup errors. EAServer is ready when it says "Accepting Connections."
Finally, you'll need to configure PowerBuilder with profiles for the database and EAServer. First, establish a database profile for EAS Demo DB V4. The ODBC DSN for this database should already be present on your system. If it isn't, create it. Next, create an EAServer Profile for your machine. Choose "Tools | EAServer Profile..." from the menu and click the "Add" button. Figure 1 shows the PowerBuilder dialog for the "Edit EAServer Profile." Enter your machine name for the Profile Name and Server Name, use "jagadmin" for the Login Name, and 9000 for the port. The default password for "jagadmin" is blank. Once you've created the necessary profiles, you're (finally) ready to create, deploy, and test a JSP.
Step 2: A Simple JSP
Now the fun begins. Start by creating a new workspace for this tutorial. I called mine "p3_pbdj". As we work through this tutorial, we'll create several targets within this workspace. The first target we'll create is a JSP Web Target.
A Web Target is used in PowerBuilder to create a Web application. A Web application is a collection of files that can be deployed to a server and executed from a client browser. The unit of deployment for a Web application is a WAR file (Web application ARchive). Even though PB will deploy directly to your JSP container and application server, it will still create and use a WAR file as the mechanism.
Right-click on the workspace and select "New...". The PowerBuilder "New" dialog will appear with the "Target" tab selected. Choose "JSP Target" and click "OK". This will start the JSP Target wizard. Work through the JSP Target wizard, making the following choices:
Within the system tree you'll now see your JSP target. Click on the "+" sign to expand it. You'll see two folders, META-INF and WEB-INF, that are part of the structure of a Web application. Under the WEB-INF folder will be a file called "web.xml" that contains much of the deployment and configuration information for the Web application. Don't edit this file directly; however, examine it to see how the information is organized.
Now that you have a JSP target, it's time to create your first JSP. The simplest JSP is made up of ordinary HTML. And the simplest HTML is a straightforward "Hello World" page. Start the New Web Page wizard. First, right-click on the target, then select "New" from the RMB menu. This will display the New dialog with the "Web" tab selected. Select Web Page from the dialog and click "OK". Work through the wizard, filling in the dialogs as follows:
Note that you can switch among the page, source, and preview views, just like you did with HTML Web Targets in PowerBuilder 8. You can also right-click on the page and source views and select "Browse with..." to view your page in a browser. However, to see your JSP served up by the EAServer Web server, you'll need to deploy the Web application. Right-click on the target and select "Deploy". You should end up with something like Figure 2 in the output view at the bottom of your PB screen.
Notice that you can see two lines that signify possible errors or warnings. The first is after the target is built. It should show the target name followed by "0 error(s), 0 warning(s)." At the very bottom of the output view is the second line that gives the results of the deployment step. This line should show the target name, followed by "Deployed - 0 error(s), 0 warning(s)." If you see any errors or warnings, check your code and configuration for possible problems. As a bit of a head start, here are a couple of likely problems and their solutions:
Note: Sybase has indicated that they will have PowerBuilder 9 beta 4 and higher install its own JDK that will be used only by PowerBuilder. This will help eliminate the two errors listed.
Now open your browser and navigate to the new JSP. It can be found in the Web app with the name given to your target, so your URL will look something like http://Server3:8080/p3_jsp/helloworld.jsp.
Don't be too impatient. The first time you request your JSP, EAServer will need to load it (and a raft of other Java classes) and compile it. The second time you call this page, it will return almost immediately.
Bring up Jaguar Manager and connect to your server. Expand the tree to your server and "Installed Web Applications". You should see "p3_jsp" as one of the applications; click on it. Notice that no components show for this Web application. Where's your JSP? The answer is the previously mentioned "web.xml". This file needs to have an entry for each JSP as a servlet tag. Remember, JSPs are treated like servlets in the Web application. To get the JSPs to show up in Jaguar Manager, do the following before you deploy from PB (see Figure 3):
Step 3: Using the Object Model and Flow Control
Here's a simple example that uses a form to pass a username and password to a second JSP. The second JSP uses the object model to get the parameter values from the request. Then a simple test (username = password) is performed to authenticate the user and respond with an appropriate message. First, build the Login page:
Your page view should look like Figure 4, and the resulting code is shown in Listing 1. Note that we're still working with plain HTML. We'll insert Java into our next page to authenticate the user and decide which message to send back.
Next, create the page to perform the authentication. Use the New Web Page wizard to create a page called "dologin.jsp". Now we need to add Java code to the page to extract the passed parameters, test them, and return a message to the user. First add the declarations:
String user;
String pwd;
The script stops after the opening bracket of the "if" statement, which is intentional. We'll insert simple HTML inline with the Java we're using for flow control.
You've added some text that will display in the browser, an HTML tag to break to the next line, and more Java - this time using the <%= ... %> tags - to evaluate (and display) the value of the variable user. Then more flow control, a bit more text, a tiny bit more Java to end the "if" block, and you're done. The point here is the free mix between HTML and Java within the page. What is ultimately sent back to the browser is only the HTML that's selected by the enclosing Java - one message (login accepted) if username and password are equal, a different one (login rejected) if they're not.
It's important to remember that the JSPs that you are working on are essentially the user interface part of your application. Significant business logic should always be placed in components, JavaBeans, or EJBs. Don't let the simplicity and ease of working with JSPs lull you into bad coding habits.
Finally, deploy and test your JSPs. Request the login.jsp page from your browser, then fill in a username and password and click the "Login" button to submit the form. If you typed the same thing for username and password, you should get back a page that says your login was accepted. If you typed different things, your login will be rejected.
If you get an error message indicating a server problem, examine the logs found in the EAServer\bin folder: httprequest.log, httpservlet.log, and jaguar.log. The messages in these log files seem cryptic, but they will help you discover what is causing your error.
Step 4: Now for Something Completely Different - Calling a PowerBuilder Component from a JSP
Of course, you'll eventually need to execute some business logic that you've deployed as a component into EAServer. Here's one way to do that:
When you request getname.jsp from your browser, your name should be displayed on the returned page. Again, if you see errors instead, review the log files to determine the cause of the errors.
Step 5: Using the HTML DataWindow in Your JSPs
Our last step shows how to use JSPs to access HTML DataWindows. This is a very simplistic example, but it shows the basics. You'll be creating a DataWindow and building a JSP to access it.
Browse to the html_dw.jsp page. If all went well, the DataWindow and associated data will be displayed within your browser. If you see errors, consult the logs.
Note: PowerBuilder 9 includes a custom tag library (taglib) that you can use instead of the DTC to add an HTML DataWindow to your JSP.
Summary
PowerBuilder 9 now includes a rich and powerful capability for authoring JavaServer Pages. This allows you to build dynamic data-driven Web applications and deploy them to J2EE 1.2 servers such as Tomcat and EAServer. In this article, I've shown how to create simple JSPs, and how to use the capabilities of the JSP object model to access implicit objects. We've used Java to control the response that's sent to the user and used Java code to access EAServer components and HTML DataWindows. Not bad for a day's work.
Published October 1, 2001 Reads 324
Copyright © 2001 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Millard F. Brown
Millard F. Brown is vice president of Power3, LLC, a company providing consulting and training services for the enterprise. He has beend developing PowerBuilder applications since PowerBuilder 2 and is the co-author of two new PowerBuilder 9 books: PowerBuilder 9: Advanced Client/Server Development and PowerBuilder 9: Internet and Distributed Application Development.
- Why SOA Needs Cloud Computing - Part 1
- Cloud Expo and The End of Tech Recession
- The Transition to Cloud Computing: What Does It Mean For You?
- A Rules Engine Built in PowerBuilder
- Sybase Named “Silver Sponsor” of iPhone Developer Summit
- How PowerBuilder Got Its Groove Back
- The Cloud Has Cross-Border Ambitions
- Ulitzer Names The World's 30 Most Influential Virtualization Bloggers
- Ulitzer Named "New Media" Partner of Greatly Anticipated iStrategy Event in Berlin
- Risks and Enterprise Mobility?
- Steps for Success in Enterprise Mobility?
- Are Mobile Luddites Resisting Mobility?
- The Difference Between Web Hosting and Cloud Computing
- Sybase CTO to Speak at 4th International Cloud Computing Expo
- Why SOA Needs Cloud Computing - Part 1
- Cloud Expo and The End of Tech Recession
- The Transition to Cloud Computing: What Does It Mean For You?
- Five Reasons to Choose a Private Cloud
- Seeding The Cloud: The Future of Data Management
- The Threat Behind the Firewall
- Economy Drives Adoption of Virtual Lab Technology
- Tips for Efficient PaaS Application Design
- A Rules Engine Built in PowerBuilder
- Sybase Named “Silver Sponsor” of iPhone Developer Summit
- Where Are RIA Technologies Headed in 2008?
- PowerBuilder History - How Did It Evolve?
- The Top 250 Players in the Cloud Computing Ecosystem
- Custom Common Dialogs Using SetWindowsHookEx
- DDDW Tips and Tricks
- OLE - Extending the Capabilities of PowerBuilder
- DataWindow.NET How To: Data Entry Form
- Book Excerpt: Sybase Adaptive Server Anywhere
- Sybase ASE 12.5 Performance and Tuning
- Working with SOA & Web Services in PowerBuilder
- Office 2003 Toolbar: A New Look For Your Old PowerBuilder App
- Dynamically Creating DataWindow Objects
































