HTML Output from Web Application

Một phần của tài liệu web development with sas by example, 2nd edition (2006) (Trang 265 - 272)

If the Apache Web server was installed prior to the WIK configuration, the HTTPD server is set up to automatically forward requests to the servlet container, so it is not necessary to specify the Web server port. In this case, since Tomcat is running as the only Web server, the required server address is http://hunding:8080; port 8080 is the default port for this Web container.

The command to run the SAS Stored Process Web Application is very similar to that used with SAS/IntrNet for the same function:

SAS/Intrnet Application Broker: /cgi-bin/broker Stored Process Web Application: /SASStoredProcess/do

As is usual for HTML, the run-time parameters follow the question mark character (?) and are separated by ampersands (&). The first parameter, _program, is required and specifies the folder and the procedure name /BBU Examples/Shoe Sales by Region. Since the desired report is to be run solely for the Asia region, the second parameter is region=Asia.1

As this example illustrates, it is not necessary to write any Java code at all in order to use the SAS Stored Process Web Application. The only requirements are some SAS programming skills. As one developer noted at a recent user group conference, the SAS Stored Process Web Application is “the Son of IntrNet.” The section that follows, on the other hand, is intended for developers who are comfortable with Java and JSPs and who would like to write Web applications using the Stored Process Service API.

1Note that quotes around the value of an HTML parameter must be URL-encoded; see http://httpd.apache.org/info/

css- security/encoding_examples.html for a general discussion of how to specify URLs using entities.

Chapter 11 Building Web Applications with SAS and Java 251

Using the Java Foundation Services Stored Process Service

A JavaServer Page or servlet that accesses the Stored Process Service API can be developed using any Java interactive development environment (IDE) such as Eclipse (see “About Us” at

http://www.eclipse.org/org/) or even a text editor such as Notepad or TextPad from Helios Software Solutions (http://www.textpad.com/). The following example uses webAF software, since it provides powerful features for debugging and testing Java code.2

Managing Configuration Issues

The Stored Process Service can be used to build Web applications that are deployed to a servlet container. Consequently, the software requirements for the Stored Process Service are similar to those for the SAS Stored Process Web Application. The SAS Web Infrastructure Kit 1.0 must be installed on the Web server in a qualified servlet container such as Tomcat 4.1. In order to compile the Java code on the client, SAS Foundation Services 1.1 must be installed there as well.

One way to deploy a Web application using SAS Foundation Services is to use the SAS Remote Services Application, which is installed as part of the SAS Web Infrastructure Kit. The file C:\Program Files\SAS\Web\Portal2.0.1\configure_wik.bat configures the Remote Services Application on the server; it should add a link on the Start X Programs X SAS X 9.1 menu to the startup file \SAS\9.1\Lev1\web\Deployments\RemoteServices\

WEB-INF\StartRemoteServices.bat. The SAS Remote Services Application starts a listener on port 5099 by default. At present, there is no way to install this as a Windows service, so it should be restarted after each server reboot.

Finally, the file C:\Tomcat4.1\conf\catalina.policy contains the permissions for Java programs to run within the servlet container. As noted in the following section, it is necessary to modify this file to add explicit permissions for each Web application deployed to the server.

Creating a SAS Stored Process Web Application with Foundation Services

Currently the easiest way to create a new SAS Stored Process Web Application is with webAF software, although as previously noted it can be created in any Java IDE. In the latter case, make sure to modify the classpath to include the required Java archive files, installed by default in

\sas\SASFoundationServices\1.1\jars. SAS AppDev Studio 3.1 software also provides a built in servlet container, running on port 8082 on the local host, which can be used for testing the resulting application, as shown in the following procedure.

The steps for creating the sample application in webAF software are as follows:

1. Open webAF software and create a new Web application. For this example, call the application SASFoundationServices.

2. At Step 2 of the New Project Wizard, choose Blank Web Application.

3. At Step 3, unselect the check box for SAS Taglib and Tbeans (Version 3), as these will not be needed for this example.

4. At Step 4, select the initial content as Servlet. Name the servlet SimpleServlet.

2 Note that SAS AppDev Studio 3.2 will not include support for webAF software but instead uses the Eclipse open-source product as the development environment. Nonetheless, the discussion of how to create a Stored Process Service should be correct in outline.

5. At Steps 5 through 8, accept the defaults by clicking Next. This will create the new HTTP servlet.

6. In the webAF code window, replace the automatically generated SimpleServlet.java program with the code shown in Example 11.3.

Example 11.3 Sample Stored Process Service Application3

package servlets;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import com.sas.services.discovery.LocalDiscoveryServiceInterface;

import com.sas.services.discovery.DiscoveryService;

import com.sas.services.discovery.ServiceTemplate;

import com.sas.services.user.UserServiceInterface;

import com.sas.services.user.UserContextInterface;

import com.sas.services.session.SessionServiceInterface;

import com.sas.services.session.SessionContextInterface;

import com.sas.services.storedprocess.StoredProcessServiceFactory;

import com.sas.services.storedprocess.StoredProcessServiceInterface;

import com.sas.services.storedprocess.StoredProcessInterface;

import com.sas.services.storedprocess.ExecutionInterface;

import com.sas.services.connection.BridgeServer;

import com.sas.services.connection.Server;

import com.sas.services.connection.ConnectionFactoryInterface;

import com.sas.services.connection.ConnectionFactoryManager;

import com.sas.services.connection.ConnectionInterface;

import com.sas.services.connection.ConnectionFactoryConfiguration;

import com.sas.services.connection.ManualConnectionFactoryConfiguration;

import com.sas.services.deployment.MetadataSourceInterface;

import com.sas.services.deployment.OMRMetadataSource;

import com.sas.services.deployment.ServiceLoader;

public class SimpleServlet extends HttpServlet {

public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException {

PrintWriter out=response.getWriter();

response.setContentType(“text/html”);

// run-time values for Metadata server connection parameters String host=”hunding”;

String port = “8561”;

String userName = “sasdemo”;

String password = “password”;

String repository = “Foundation”;

String softwareComponent = “Remote Services”;

String serviceComponent = “BIP Remote Services OMR”;

// run-time values for Stored Process server connection int bridgeport = 8611;

String file=”c:\\Documents and Settings\\sas\\My Documents”;

3 Thanks to David Barron at SAS for figuring out this example.

Chapter 11 Building Web Applications with SAS and Java 253

String pgm=”shoes.sas”;

try {

// connect to Metadata server on port 8561 to discover Services LocalDiscoveryServiceInterface discoveryService =

DiscoveryService.defaultInstance();

MetadataSourceInterface metadataSource =

new OMRMetadataSource(

host,port,userName,password,repository, softwareComponent,serviceComponent);

ServiceLoader.lookupRemoteDiscoveryServices(

metadataSource, discoveryService);

// create user context

ServiceTemplate stp = new ServiceTemplate(

new Class[] {UserServiceInterface.class} );

UserServiceInterface userService =

(UserServiceInterface)

discoveryService.findService(stp);

UserContextInterface user =

userService.newUser(userName,password,”DefaultAuth”);

// create session context

stp = new ServiceTemplate( new Class[]

{SessionServiceInterface.class} );

SessionServiceInterface sessionService =

(SessionServiceInterface)

discoveryService.findService(stp);

SessionContextInterface sessionContext =

sessionService.newSessionContext(user);

// create stored process service

StoredProcessServiceFactory spFactory = new StoredProcessServiceFactory();

StoredProcessServiceInterface spServiceInterface = spFactory.getStoredProcessService();

StoredProcessInterface spi =

spServiceInterface.newStoredProcess(

sessionContext,

StoredProcessInterface.SERVER_TYPE_STOREDPROCESS, StoredProcessInterface.RESULT_TYPE_STREAM);

// send messages to stored process spi.setSourceFromFile(file,pgm);

spi.setParameterValue(“region”,”Canada”);

spi.addInputStream(“_WEBOUT”);

// connect to Stored Process Server on // load balancing port 8611

BridgeServer server = new BridgeServer(

Server.CLSID_SASSTP,host,bridgeport);

ConnectionFactoryConfiguration cxfConfig =

new ManualConnectionFactoryConfiguration(server);

ConnectionFactoryInterface cxf =

ConnectionFactoryManager.

getConnectionFactory(cxfConfig);

ConnectionInterface ci =

cxf.getConnection(userName,password);

// run stored process

ExecutionInterface ei = spi.execute(false,null,false,ci);

// display results

InputStream is = ei.getInputStream(“_WEBOUT”);

BufferedReader br = new BufferedReader(

new InputStreamReader(is));

String temp = “”;

while((temp = br.readLine()) != null) {

out.println(temp);

}

}

catch (Exception ex) { out.println(

“<html><body>” + “SAS encountered an error: “ + ex.getLocalizedMessage() + “</body></html>”);

} }

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request, response);

} }

This example is by no means a complete introduction to using the Java Foundation Services. The complete API is documented in the SAS Integration Technologies: Developer’s Guide under

“SAS Foundation Services” in the section on “Java Clients.” The documentation also includes a link to the Foundation Services API for the Java packages currently available from SAS. The following brief overview is provided simply as a guide to the workings of this particular example.

For this example, the run-time values are supplied as constants. In a production application, they would more properly be stored as a resource bundle and accessed as session properties. Note that the values for the stored process assume that the program shoes.sas (described in the preceding section on the SAS Stored Process Web Application) has been registered as a stored process with SAS Management Console.

This program uses two OMA servers. First, a connection is made to the metadata server to find the stored process registry. Then a second connection is made to the stored process server in order to create the Web output.

The first connection takes place in three steps:

1. Get the default discovery service.

2. Specify the metadata source.

3. Use the Service Loader to look up the remote discovery service on the host.

Once the remote services are discovered, the user context and the session context must be created in order for the stored process service to run. A stored process service can now be instantiated using the defined session context and the stored process server. Methods are available in the interface to specify the location of the SAS program on the server, pass parameters such as region=Canada, and to add the default _webout destination.

Chapter 11 Building Web Applications with SAS and Java 255

In practice, the region parameter would not be hard-coded into the servlet but would be passed from the HTTP request object. Note that with this code, it is not possible to specify a parameter to the stored process on the URL; it must be specified using the object’s setParameterValue

method.

The second connection is then made to the load-balancing server, configured using SAS

Management Console on the default port 8611. The program uses the Connection Factory class to connect to the server, as illustrated in Chapter 10, “Using the SAS Open Metadata Architecture with the Integrated Object Model.”

To run the stored process, the execute method of the object is called with four arguments:

ƒ boolean synchronous

ƒ ExecutionStatusListenerInterface listener

ƒ boolean createAlert

ƒ Object connection

Since this stored process generates streaming output, the generated HTML can be captured by reading from the file _webout by an ordinary BufferedReader. It can then be redirected to the servlet reponse object.

Note that in order for this application to run as a Tomcat servlet, the Foundation Services JAR files should be copied to the webapps/WEB-INF/lib directory under the SAS AppDev Studio project directory. It is a “feature” of Tomcat 4.1 that all of the required classes must be available in each of the application home directories; it is not sufficient for them to be available from the Java classpath environment variable. The SAS AppDev Studio software installation process should have copied these files to the correct location, but they can be copied over manually from the \sas\SASFoundationServices\1.1\jars folder if necessary.

To test the sample application in webAF, the following steps are required:

1. Select Build (F7) from the Build toolbar.

2. From the Tools menu, select Start Java Web Server.

3. Click Execute in browser from the Build toolbar.

4. If all goes well, the Web page shown in Display 11.13 should be displayed as http://localhost:8082/SASFoundationServices/SimpleServlet.

Display 11.13 Sample Stored Process Service Output

Deploying a SAS Stored Process Web Application

As previously noted, this application is running on the local servlet container provided with SAS AppDev Studio 3.1 software on the client. In order to move the application to a remote host, a Web Application Archive (WAR) file is necessary. The procedure for moving an application to another server is described in Chapter 9. To review, the webAF Package Wizard provides an easy way to create a deployment file from a project.4

When you select the wizard from the Tools X Wizards menu, you should see something like the following:

4 You can also use the WAR tool in Eclipse or the Ant build tool to make WAR files.

Chapter 11 Building Web Applications with SAS and Java 257

Một phần của tài liệu web development with sas by example, 2nd edition (2006) (Trang 265 - 272)

Tải bản đầy đủ (PDF)

(361 trang)