Writing a Web Application
Articles and Tips: article
01 Sep 2000
This article explains the concepts you need to know to write a web application and outlines the steps for writing a simple web application that runs on WebSphere Application Server for NetWare. Specifically, this article explains the following:
Understanding how web application concepts relate to Java-based web application servers
Applying the Model/View/Controller (MVC) pattern to the web application domain
Using command beans to handle business logic tasks
Using format beans to simplify complex presentation code
Understanding technologies that provide a framework for Novell's Directory-Enabled Network Infrastructure Model (DENIM)
Finding the right edition of WebSphere Application Server for your company
Using IBM development tools to create a web application
Applications Available Today
WEB APPLICATION CONCEPTS
A typical web application starts when a user clicks a link in an HTML page and causes the browser to instigate an HTTP request/response transaction with a web server, CGI program, and web application server. For the purposes of this article, I will refer to the web server, CGI program, and web application server as the targeted server.
The targeted server receives the HTTP request and dispatches the request and its associated data (such as headers and query parameters) to the desired resource residing on the targeted server. In the simplest case, the targeted server responds with a static HTML page, which the browser interprets and presents to the user. In more complex web applications, the targeted server must perform other functions such as the following:
Execute business logic
Retrieve data according to query parameters
Authenticate and authorize users
Reestablish a session from a former request
Exchange data with legacy systems
Build the HTML response page dynamically to be passed back to the browser
The Java 2 Platform, Enterprise Edition defines a set of architectures, technologies, and frameworks that simplify and standardize distributed web application programming using the Java programming language. Java-based web application servers incorporate some of these architectures, technologies, and frameworks such as servlets, Java Server Pages (JSP), and Enterprise JavaBeans (EJB) to provide a common programming platform for web application programmers. WebSphere Application Server for NetWare is a Java-based web application server, thus enabling you to use these technologies to build web applications.
THE MVC PATTERN
Servlets are server-side Java programs that extend a web server to handle HTTP requests and responses. A servlet's request-handling methods are expected to extract information from request parameters, perform business logic, and then send back HTML responses to a client.
JSP extend HTML with a set of custom HTML tags that allow developers to embed Java code in an HTML page. A JSP is converted into a Java servlet and compiled the first time it is requested. Subsequent requests are simply routed through the cached instance. This process allows content to be added to a page dynamically and sent to the client.
You can use the MVC pattern to implement an architecture that uses servlets to handle HTTP requests and perform business logic and uses JSP to handle the HTML presentation. (See Figure 1.) The components of this pattern are explained below:
Figure 1: The interaction between the elements involved in a Java-based web application
Model. The model components expose the business logic for a particular web application or service.
View. The view components build HTML pages that a web application returns to a user in response to HTTP requests.
Controller. The controller components or services drive the model and view components.
Using an MVC pattern to build web applications decouples the development process, thereby allowing HTML authors to build web pages and Java programmers to work on business logic. Novell's MVC Beans for eBusiness takes this development process one step further by separating business logic into Java components called command beans and separating the complex user interface into Java components called format beans. An HTTP transaction that uses command and format beans is processed in the following way:
The HTTP request is handled by a target servlet.
The servlet instantiates and calls methods belonging to one or more command beans to perform the business logic of the request.
The command beans that are implementing the business logic access relational databases, connectors, directory services, file systems, and so on while performing their specific actions.
The servlet invokes a JSP after storing command bean instances in the applicable context object for use by the JSP.
The content is dynamically generated by the JSP using a combination of HTML and the output properties of the command beans that are retrieved from the context object. The HTML page is then sent to the client. This process may leverage formatting beans, other JSP, and static resources such as HTML files and image files.
A command bean is a JavaBean that encapsulates a single business logic task. To use a command bean, you complete the following steps:
Create the command bean.
Initialize the state of the command bean by setting specific input properties of the bean.
Instigate the command bean to execute its specific business logic task by calling its perform method and then getting the values of the bean's output properties. (The perform method is explained below.)
This programming model helps isolate changes in the web application by factoring the business logic into fine-grained components that are easily managed and less reliant on other components. This model also allows easier distribution of processing using serialization to send the command bean over protocols, such as Internet Inter-ORB Protocol (IIOP) or HTTP, to a remote server. The remote server then executes the command bean and sends back the results.
The primary interface for the command bean is the Command interface, which consists of four methods: isReadyToCallPerform, perform, executeCommand (used if executing remotely), and reset.
A command bean can be in one of the following states during its life cycle:
New. The command bean is in the new state immediately after it is created. In this state, the isReadyToCallPerform method will return false, and the output properties should not be retrieved.
Initialized. The command bean is in the initialized state after all required input properties have been set. In this state, the isReadyToCallPerform method will return true, but the output properties should not be retrieved.
Executed. The command bean is in this state after the perform method has been called. In this state, the isReadyToCallPerform method will return true, and the output properties can be retrieved.
Command beans that execute locally (on the same Java Virtual Machine [JVM] as the web application) implement the Command interface. If a command bean is to be executed remotely on another server, the command bean implements the TargetableCommand interface. An extension of the Command interface, the TargetableCommand interface allows for remote execution. Regardless of whether the command bean executes locally or remotely, the JSP or servlet executes the command bean in the following way:
The servlet instantiates the command bean and then sets the command bean's input properties. Next, the servlet calls the perform method on the command bean. If the command bean executes locally, step 2 begins. Otherwise, these additional steps occur: A. The perform method determines the target environment for the command bean. B. The perform method calls the executeCommand method on the CommandTarget object passing itself (the command bean) as an input argument. This step may entail serializing the command bean and sending it via some protocol (such as IIOP) to the target server for execution. C. After potentially de-serializing the command bean, the command target calls the command bean's executeCommand method, which encapsulates the business logic.
The perform method, if successful, returns with its output properties set to the results of the underlying business logic task.
Control flows to the JSP, which is now free to query the output properties of the command bean.
Format beans support JSP and help format various types of dynamic content. In fact, you can use one or more format beans to handle complex data formatting.
Formatting is usually specific to the characteristics of the user, such as the locale of the user. For this reason, each format bean class method comes in at least two forms: one that takes the servlet request object as a parameter and one that does not. When the request object is provided, the method will extract information from the request object to customize the formatting operation. When the request object is not provided, the method will use default values or server-wide values such as the default locale.
Most of the format beans produce locale-specific results. By convention, the format beans determine the locale from the request context value "JSPLocale" which must be a java.util.Locale value. If "JSPLocale" is not set in the request context or the request context is not available, the system's default locale will be used.
Each of the format classes also allows the locale to be configured into a format bean. In this case, the configured locale takes precedent over the JSPLocale value, if this value is specified.
A FRAMEWORK FOR DENIM
The Novell Web Application Servers team has designed an architecture consisting of JavaBeans, EJBs, JSP, servlets, and auxiliary Java classes to form a complete web application framework for Novell's DENIM platform. Part of this framework consists of the MVC Beans for eBusiness, a suite of command and format beans that uses the MVC pattern to expose NetWare services to web applications.
By abstracting functionality required by many applications, software frameworks provide a common design and a partial implementation for a set of similar problems. Developers then provide application-specific functionality. This approach to software development provides a number of benefits including unbeatable design-to-market time frames, centralized bug fixes and feature enhancements, and compatible application infrastructures.
Domain frameworks capture expertise for particular problems such as network management, inventory control, and point-of-sale. Novell plans to provide a number of domain frameworks built around the MVC Beans for eBusiness and other components, using common data interchange interfaces such as XML and WML. Novell plans to begin distributing these domain frameworks (such as a web portal framework, a storefront framework, an NDS eDirectory management framework, and a server-health management framework) at the end of this year.
WebSphere Application Server for NetWare provides the infrastructure you need to deliver powerful web applications on the NetWare platform. Novell offers two editions of WebSphere Application Server for NetWare:
Standard Edition, which allows you to create dynamic web sites based on JSP and Java servlets. This edition ships with NetWare 5.1 and 5.
Advanced Edition, which allows you to use EJBs and middleware connectors. (For more information about this edition, visit www.novell.com/products/websphere/advancededition.)
WebSphere Application Server for NetWare includes an administrative console that allows you to manage the web application life cycle. The administrative console allows you to perform tasks such as creating web applications, setting options for loading local and remote servlets and initialization parameters, specifying servlet aliases, creating servlet chains and filters, and monitoring resources used by WebSphere Application Server.
You can also configure resources such as HTML pages, servlets, file system directories, and JSP into single units that WebSphere Application Server manages as web applications. These web applications can have their own relative URL (called a web path), their own classpaths for servlet execution, and their own document directories for HTML and JSP documents. After you create a web application in the administrative console, you can group this application with other web applications or other resources and secure it using WebSphere's extensible security model. WebSphere's security model uses NDS eDirectory via Lightweight Directory Access Protocol (LDAP).
IBM's VisualAge for Java is one of the industry's favorite visual, integrated development environments (IDE) for Java. VisualAge for Java provides support for building, testing, and deploying 100 percent pure Java applications, JavaBean components, servlets, and applets.
Applications developed with WebSphere Studio and VisualAge for Java can be debugged either from within the IDE or remotely on the web application server.
To demonstrate how easy creating your own web applications is, the following sections explain how to create a simple web application that can access NDS eDirectory to retrieve a list of NDS trees on the network. To create this application, you will use WebSphere Studio, VisualAge for Java, WebSphere Application Server for NetWare, and Novell's MVC Beans for eBusiness.
WebSphere Administrative Console
In the WebSphere Administrative Console, select the Tasks tab, and expand the Configuration node.
Select the Configure a Web Application node, and click the green button on the button bar.
Name the web application, and select the Enable File Servlet: checkbox and the Serve Servlets by Classname: checkbox. Also, click the Enable JSP 1.0 radio button. Click Next.
From the Select a Servlet Engine page, expand the Nodes tree until the servletEngine node appears. Select the servletEngine node, and click Next. When the next page appears, click Next, and when the next page appears, click Finished.
When the Command "Web Application.create" Completed Successfully dialog box appears, click OK.
Select the Topology tab, and expand the node that matches your host name until you see your new web application node, which should be a child node of the servletEngine node.
Right-click your web application node, and select Restart Web App.
When the Command "webappname.setRefreshConfig" Completed Successfully dialog box appears, click OK.
Create a new project using the File->New Project... menu item.
In the Publishing View, make sure the Test publishing stage is active, right-click the Test node, and insert a server. Name the server the full Domain Naming System (DNS) name of your host server (such as >myserver.mydomain.com).
Right-click the new server, and select the File System Publish radio button. Define the publishing targets as follows:A. Modify the HTML publishing target to match the web directory of your web application. B. Modify the servlet publishing target to match the servlets directory of your web application.
Right-click the servlet node in the file view, and select insert file. Select the Use Existing tab. Browse for the EDirCMD.jar file found on your server in the<vol>:\java\beans directory.
Expand the servlet node in the file view, right-click the EDirCMD.jar node, and select Properties. Select the Publishing tab, and uncheck the Set Publishable check box.
Select the top node in the file view, and then select the Tools->Wizards->JavaBean Wizard menu item. From the JavaBean: list, select the com.novell.web.command.beans.edir.ListEDirTrees JavaBean, and click Next.
Select the Create an Input Page and Create a Results Page checkboxes, and click Next. When the next page appears, click Next because you will not be setting any input properties from your input page.
The Results page appears. Select the treeNames check box, and click Next.
From the Methods page, check the perform() check box, and then click Next. When the Session page appears, click Next, and then click Finish.
At this point, the output page (Bean1Results.jsp) contains code to display the treeNames output property as a Java string. The treeNames output property is an array of strings, so you must modify this line of code to display the names in the manner that best suits your needs. To do this, right-click the Bean1Results.jsp node, select the Edit with->ScriptBuilder menu item, and edit the file as needed. Save the file, right-click the Bean1Results.jsp node, and select the Check In menu item.
Right-click the top node in the file view, and select the Publish Whole Project menu item. Select the OK and/or Yes buttons until the publishing phase finishes.
Start VisualAge for Java. Create a new project that matches the name of your WebSphere Studio project.
In VisualAge for Java, select the Window->Options menu item. Select the Remote Access to Tool API node. Click the Start Remote Access to Tool API button, and then click OK.
In Websphere Studio, expand the servlet node, and select Project->VisualAge for Java->Send to VisualAge menu item. The VisualAge dialog box appears; select the project that matches the WebSphere Studio project.
VisualAge for Java
After exporting your servlet from WebSphere Studio to VisualAge for Java, right-click the project that matches the name of your WebSphere Studio project, and select the Import... menu item. Click the Jar file radio button, and then click Next. Browse for the njcl.jar file on your server in the<vol>:\java\lib directory. Click Finish. If you encounter any problems relating to other packages that have not been imported (particularly Swing classes), import the jar files containing these classes as well.
Select the Import... menu item. Click the Jar file radio button, and then click Next. Browse for the EdirCmd.jar file on your server in the<vol>:\java\beans directory. Click Finish. When the Modify Palette dialog box appears, create a new category, name it, select the ListEDirTrees bean and any other beans that you would like to use from the Available Beans list.
Expand the project until you find the servlet that was created in WebSphere Studio. Select the method of the servlet that you want to edit. Edit as needed, and save the project.
In WebSphere Studio, expand the servlet node, select the Project->VisualAge for Java->Update from VisualAge menu item. Right-click the Bean1class node and select the Check In menu item. Right-click the Bean1.java node, and select the Compile menu item. Right-click the Bean1.java node, and select the Publish Selected Files menu item.
MVC Beans for eBusiness are distributed with WebSphere Application Server for NetWare and NetWare 5.1 and are also available on Novell's developer web site (>developer.novell.com). You will also find documentation, sample code, and usable applications, such as the following: an application that uses JSP and format beans to manage NDS objects in a tree-view from a web browser, an application that uses JSP and command beans to authenticate a user and to perform operations on an NDS tree in proxy for the user, and an application that uses JSP and format beans to view and direct a server console.
Many other applications are available within the MVC Beans for eBusiness. You can customize and extend these applications as needed.
Using WebSphere Application Server for NetWare and tools, such as Novell's MVC Beans for eBusiness, simplifies the otherwise daunting task of web application development. By factoring NetWare services into manageable components that work within the WebSphere Application Server environment, you can develop and deploy powerful web applications using rapid application development (RAD) techniques within a comprehensive set of tools provided by IBM and Novell.
For more information about developing to WebSphere Application Server for NetWare, visit the following web sites:
Jeff Hanson is a senior software engineer and team lead over the WebSphere Components team at Novell.
* Originally published in Novell Connection Magazine
The origin of this information may be internal or external to Novell. While Novell makes all reasonable efforts to verify this information, Novell does not make explicit or implied claims to its validity.