This section demonstrates how to create a remote application using the “hello world” application as an example.
The “hello world” program is common code written by people just beginning to learn a programming language or paradigm. Usually, upon execution, a hello-world application writes a greeting to standard output.
This section presents a hello-world program that ships in the Novell Teaming source code, which you can deploy and implement in your Teaming installation quickly. When executed as a remote application from a Teaming page, this application uses a Web services operation to greet the Teaming user by name.
To download the Teaming code base, visit the Open Source Community page. After installing the code base, you can locate this example code here:
/ssf/samples/remoteapp
There are four important files in the /remoteapp hierarchy:
The class file: This file contains the source code for the remote application and is located here:
/remoteapp/src/com/sitescape/team/samples/remoteapp/web/HelloWorldServlet.java
The servlet-definition file: The servlet-definition file establishes the hello-world application as a Tomcat servlet and is located here:
/remoteapp/war/WEB-INF/web.xml
The JSP file: The JSP file is the content of the response for the application and is located here:
/remoteapp/war/WEB-INF/jsp/hello_world/view.jsp
The build file: The build file deploys the application in Tomcat, making it available for use as a remote application in Teaming, and is located here:
/remoteapp/build.xml
The subsections that follow provide more detailed explanation about three of these files:
To review the Java source code used to implement this application, locate this file:
/remoteapp/src/com/sitescape/team/samples/remoteapp/web/HelloWorldServlet.java
Although this is not a complete description of all of the code contained in the example, the next few paragraphs explain some of the key parts of the HelloWorldServlet class defined in the HelloWorldServlet.java file.
The following line in the code creates the bean to contain the user’s first and last name (ss_userTitle), which is to be used by a JSP:
private static final String PARAMETER_NAME_USER_TITLE = "ss_userTitle";
Toward the bottom of the file, the class defines the following method, which makes a Web services call, obtains the user object, and applies the getTitle method to the object, placing the user’s first and last name in a string:
private String getUserTitle(TeamingServiceSoapBindingStub stub, String accessToken, Long userId) throws ServiceException, DocumentException, RemoteException { User user = stub.profile_getUser(accessToken, userId, false); return user.getTitle(); }
The Web services call was set up in the doPost method, which then calls the getUserTitle method shown in the last code example. Consider this code from the doPost method:
private static final String TEAMING_SERVICE_ADDRESS = "http://localhost:8080/ssr/token/ws/TeamingService"; . . . // Get ready for web services calls to the Teaming. TeamingServiceSoapServiceLocator locator = new TeamingServiceSoapServiceLocator(); locator.setTeamingServiceEndpointAddress(TEAMING_SERVICE_ADDRESS); TeamingServiceSoapBindingStub stub = (TeamingServiceSoapBindingStub) locator.getTeamingService(); // Get the title of the user by making a web services call. String userTitle = getUserTitle(stub, accessToken, Long.valueOf(userId));
When using Web services in the context of a remote application, you must use the /ssr/token/ws/TeamingService endpoint. See Section 4.4, Server Endpoints, for more information about specifying server endpoints for Web service calls.
Finally, the doPost code specifies the location of a JSP, which is used to generate the response:
String jsp = "/WEB-INF/jsp/hello_world/view.jsp"; RequestDispatcher rd = req.getRequestDispatcher(jsp);
To review the XML used to define the “hello world” servlet for Tomcat, locate this file:
/remoteapp/war/WEB-INF/web.xml
The XML file contains these lines:
<servlet> <servlet-name>helloWorld</servlet-name> <servlet-class>com.sitescape.team.samples.remoteapp.web.HelloWorldServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>helloWorld</servlet-name> <url-pattern>/helloWorld/*</url-pattern> </servlet-mapping>
The servlet tag defines the class code to be executed when someone specifies /helloWorld in the URL. The servlet-mapping tag establishes /helloWorld portion of the URL. (See Section 10.3.1, Registering a Remote Application, for information about how this definition maps to the URL you specify when registering a remote application with Teaming.)
To review the JSP file used to generate the output for the application, locate this file:
/remoteapp/war/WEB-INF/jsp/hello_world/view.jsp
This is the content of the JSP file:
<%@ page isELIgnored="false" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <c:set var="title" value="${ss_userTitle}"/> <c:if test="${empty title}"><c:set var="title" value="world"/></c:if> <strong>Hello ${title}!</strong><br>
The JSP tests to see if the ss_userTitle bean is empty, and, if it is, substitutes the string Hello world! for Hello userTitle!
Because the remote application is designed to provide a portion of an HTML page, the JSP file does not include HTML tags that structure the page, such as the html, title, head, and body tags. Teaming structures the page, and remote applications provide HTML for a segment within that page.