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 application that ships in the Novell Vibe source code, which you can quickly deploy and implement in your Vibe installation. When it is executed as a remote application from a Vibe page, this application uses a Web services operation to greet the Vibe user by name.
To download the Vibe 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.
/remoteapp/src/org/kablink/teaming/samples/remoteapp/web/HelloWorldServlet.java
The servlet-definition file: The servlet-definition file establishes the hello-world application as a Tomcat servlet.
/remoteapp/war/WEB-INF/web.xml
The JSP file: The JSP file is the content of the response for the application.
/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 Vibe.
/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 the following file:
/remoteapp/src/org/kablink/teaming/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(TeamingServiceV1SoapBindingStub 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/TeamingServiceV1"; . . . // Get ready for web services calls to the Teaming. TeamingServiceV1SoapServiceLocator locator = new TeamingServiceV1SoapServiceLocator(); locator.setTeamingServiceV1EndpointAddress(TEAMING_SERVICE_ADDRESS); TeamingServiceV1SoapBindingStub stub = (TeamingServiceV1SoapBindingStub) locator.getTeamingServiceV1(); // 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/TeamingServiceV1 endpoint. See Section 1.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>org.kablink.teaming.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 3.4.1, Registering a Remote Application, for information about how this definition maps to the URL you specify when registering a remote application with Vibe.)
To review the JSP file used to generate the output for the application, locate the following file:
/remoteapp/war/WEB-INF/jsp/hello_world/view.jsp
The file has the following content:
<%@ 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. Vibe structures the page, and remote applications provide HTML for a segment within that page.