Servlet and JSP Integration  Locate

Overview  Locate

WSO2 SOA Enablement Server for Java can be integrated with servlets by porting WSO2 SOA Enablement Server to any supported application server containing a servlet container. This is necessary because the WSO2 SOA Enablement Server is primarily a Web Service container, and has no native support for servlets.

Self-contained WSO2 SOA Enablement Server client web applications can also be ported to an application server, with the J2EEIntegrate tool.

For our example of integration, we will use the Tomcat Servlet Container version 4.1.29 and UNIX syntax. Porting to other platforms is virtually identical.

Let us assume you have installed WSO2 SOA Enablement Server to the path WASP_HOME and the Tomcat Servlet Container to the path TOMCAT_HOME and you have already built HelloService and are running it at http://localhost:6060/demo/basic/HelloService. The service is part of the demo in the WASP_HOME/demos/hello directory. In order to build, deploy and run the service, read the demo documentation.

Invoking a Web Service from Servlets Using a WSO2 SOA Enablement Server Client  Locate

We want to create a simple servlet calling a Web service and providing the interface for calling the service from a web browser. We want to be able to call a Web service running at the specified endpoint. In this example we will call the HelloService from the WSO2 SOA Enablement Server demos running on a standalone WSO2 SOA Enablement Server.

We will show the integration in three easy steps.

Step One: Creating The Servlet  Locate

Before we can create the servlet, we need to know the service interface. We can generate one from the WSDL file obtained from the server the service is running on:

$WASP_HOME/bin/WSDL2Java -l java --url http://localhost:6060/demo/basic/HelloService/wsdl

It will look like Example 276.

The interface of the HelloService can be found in the file WASP_HOME/src/example/applicationIntegration/servlet/HelloService.java.

Example 276. HelloWorld Service Interface

// Copyright WSO2 Inc. All rights reserved.
// Use is subject to license terms.
public interface HelloService {
    java.lang.String hello(java.lang.String p0);
}

You can see the source code for WASPHelloWorldClient in Example 277. Note that we do the service lookup in every servlet request. This is for clarity and simplicity of the example code but it should be optimized in a real-world application.

Source code of WSO2 SOA Enablement Server HelloWorld Client servlet can be found in the file src/example/servlet/WASPHelloWorldClient.java.

Example 277. WSO2 SOA Enablement Server HelloWorld Client Servlet

// Copyright WSO2 Inc. All rights reserved.
// Use is subject to license terms.
import org.systinet.wasp.webservice.LookupException;
import org.systinet.wasp.webservice.Registry;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Example servlet showing calling of WASP HelloWorld service
 */
public class WASPHelloWorldClient extends HttpServlet {
    public void doGet(
        HttpServletRequest request,
        HttpServletResponse response) throws IOException, 
        ServletException {
        // HTML prologue
        response.setContentType("text/html");

        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");

        String title = "WASP HelloWorld Client";
        out.println("<title>" + title + "</title>");
        out.println("</head>");
        out.println("<body bgcolor=\"white\">");
        out.println("<h3>" + title + "</h3>");

        // get the servlet parameters or use default values
        String reqParam = request.getParameter("reqParam");

        if (reqParam == null) {
            reqParam = "Hello service!";
        }

        String serviceURI = request.getParameter("serviceURI");

        if ((serviceURI == null) || "".equals(serviceURI)) {
            serviceURI = "http://localhost:6060/demo/basic/HelloService/wsdl";
        }

        // prepare the service call, display parameters and results
        out.println("Results of this request:<br>");
        out.println("Service URI = <i>" + serviceURI + "</i><br>");
        out.println("String sent to service = <i>" + reqParam + "</i><br>");

        // --------- WASP Web service call code begins ---------
        try {
            // lookup service
            HelloService helloService =
                (HelloService) Registry.lookup(serviceURI, HelloService.class);

            // call service and print out a response message
            out.println("String got from the service = <i>"
                + helloService.hello(reqParam) + "</i><br>");
        } catch (LookupException e) {
            e.printStackTrace(out);
            out.println("Could not lookup HelloWorld service at URI "
                + serviceURI);
        }

        // --------- WASP Web service call code ends ---------
        // HTML form to set the call parameters, HTML epilogue
        out.println("<P>");
        out.print("<form action=\"\" method=POST>");
        out.println("HelloWorld service URI:");
        out.println("<input type=text size=40 name=serviceURI value=\""
            + serviceURI + "\">");
        out.println("<br>");
        out.println("String for HelloWorld service:");
        out.println("<input type=text size=40 name=reqParam value=\""
            + reqParam + "\">");
        out.println("<br>");
        out.println("<input type=submit>");
        out.println("</form>");
        out.println("</body>");
        out.println("</html>");
    }

    public void doPost(
        HttpServletRequest request,
        HttpServletResponse response) throws IOException, 
        ServletException {
        doGet(request, response);
    }

    synchronized public void init(ServletConfig config)
        throws ServletException {
        super.init(config);

        String waspLocation = config.getInitParameter("wasp.location");

        if (waspLocation != null) {
            System.setProperty("wasp.location", waspLocation);
        }

        String waspConfigLocation =
            config.getInitParameter("wasp.config.location");

        if (waspLocation != null) {
            System.setProperty("wasp.config.location", waspConfigLocation);
        }
    }
}

We have both source files, HelloService.java and WASPHelloWorldClient.java, in the working directory. So we can compile the servlet:

javac -classpath $WASP_HOME/lib/wasp.jar:$TOMCAT_HOME/common/lib/servlet.jar HelloService.java WASPHelloWorldClient.java

Step Two: Deploying The Servlet  Locate

We assume that your servlet is to be in $TOMCAT_HOME/webapps/wsclient. Let's create the directory structure of your web application:

mkdir $TOMCAT_HOME/webapps/wsclient/WEB-INF/classes

mkdir $TOMCAT_HOME/webapps/wsclient/WEB-INF/lib

To make the servlet accessible, you have to move the class files to the proper directory:

mkdir $TOMCAT_HOME/webapps/wsclient/WEB-INF/classes

mv HelloService.class $TOMCAT_HOME/webapps/wsclient/WEB-INF/classes

mv WASPHelloWorldClient.class $TOMCAT_HOME/webapps/wsclient/WEB-INF/classes

The servlet depends on a number of JAR files from the directory WASP_HOME/lib. Copy them into the directory $TOMCAT_HOME/webapps/wsclient/WEB-INF/lib.

cd $WASP_HOME/lib

cp builtin_serialization.jar jaxm.jar jaxrpc.jar jetty.jar log4j.jar saaj.jar wasp.jar wsdl_api.jar $TOMCAT_HOME/webapps/wsclient/WEB-INF/lib

Copy the file with your web application configuration, edit it and replace WASP_HOME with the WSO2 SOA Enablement Server installation directory name.

cp $WASP_HOME/src/example/applicationIntegration/servlet/web.xml $TOMCAT_HOME/webapps/helloclient/WEB-INF

See Example 278.

Example 278. Sample Web Application Configuration

<web-app>
    <display-name>WS Client</display-name>
    <servlet> 
        <servlet-name>WS Client</servlet-name>
        <servlet-class>WASPHelloWorldClient</servlet-class>
        <init-param>
            <param-name>wasp.location</param-name>
            <param-value>WASP_HOME</param-value>
        </init-param> 
        <init-param>
            <param-name>wasp.config.location</param-name>
            <param-value>conf/clientconf.xml</param-value>
        </init-param> 
    </servlet>
    <servlet-mapping> 
        <servlet-name>WS Client</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping> 
</web-app>

To apply the changes you just made, you must restart Tomcat:

$TOMCAT_HOME/bin/catalina stop

$TOMCAT_HOME/bin/catalina start

Step Three: Running The Servlet  Locate

Now it is time to test the service by pointing your browser to the servlet's URL : http://localhost:8080/wsclient. The result of the browser request will look like Figure 27, WSO2 SOA Enablement Server HelloWorld Client Servlet.

Figure 27. WSO2 SOA Enablement Server HelloWorld Client Servlet

WSO2 SOA Enablement Server HelloWorld Client Servlet

WSO2 SOA Enablement Server Client Within a JSP Page  Locate

In this example, we will make similar assumptions as those given in the Overview. Make sure you are running HelloService at http://localhost:6060/demo/basic/HelloService.

We want to create a simple JSP page calling a Web service and displaying the results. We want to be able to call a Web service running on our local server in addition to services on any arbitrary server. In this example, we will call the HelloService from the WSO2 SOA Enablement Server demos.

Step One: Creating The JSP Page  Locate

Before we can create a JSP page for calling a Web service, we have to know the interface of the service. We can generate one from the WSDL file obtained from the server the service is running on with the following command:

$WASP_HOME/bin/WSDL2Java -l java -i demo --url http://localhost:6060/demo/basic/HelloService/wsdl

This will create a file named HelloService.java, in the subdirectory demo/. It will look like Example 279.

This interface of HelloService can be found in WASP_HOME/src/example/applicationIntegration/JSP/HelloService.java file.

Example 279. HelloWorld Service Interface

// Copyright WSO2 Inc. All rights reserved.
// Use is subject to license terms.
package demo;

public interface HelloService {
    java.lang.String hello(java.lang.String p0);
}

You can see the source code of the helloclient.jsp JSP page in Example 280. Note that we do the WSO2 SOA Enablement Server initialization and service lookup in every JSP request. This is for clarity and simplicity of the example code but it should be optimized in a real-world application. You may also note a strong similarity to the servlet code from the previous section.

The source code of WSO2 SOA Enablement Server HelloWorld client JSP page can be found in WASP_HOME/src/example/applicationIntegration/JSP/helloclient.jsp.

[Important]Important

You should not perform Web service lookup on every HTTP request made on the JSP page. Web service lookup is a very expensive operation in terms of memory consumption and CPU usage. Therefore it is necessary to cache the client proxy created on the first request to the servlet. This is illustrated in the JSP example.

Example 280. WSO2 SOA Enablement Server HelloWorld Client JSP Page

<!-- Copyright WSO2 Inc. All rights reserved. -->
<!-- Use is subject to license terms. -->
<!-- Example JSP showing calling of WASP HelloWorld service -->

<%@page contentType="text/html"%>

<html>
<head>

    <title>WASP HelloWorld Client</title>

</head>

<body bgcolor="white">

<%@ page import="demo.HelloWorldService,
  org.systinet.wasp.Wasp,
  org.systinet.wasp.webservice.Registry,
  org.systinet.wasp.webservice.LookupException" %>

<h3>WASP HelloWorld Client</h3>

<%!
  // cache for the hello service client stub
  static HelloWorldService helloService = null;
%>

<%
  // construct the base path
  String basePath = request.getServletPath();
  int slashIndex = basePath.lastIndexOf('/');
  basePath = slashIndex != -1 ? basePath.substring(0, slashIndex+1) : "";
  String waspLocation = request.getRealPath(basePath) + "WEB-INF";
  Wasp.init(waspLocation);

  // get the servlet parameters or use default values
  String reqParam = request.getParameter("reqParam");
  if (reqParam == null)
    reqParam = "Hello service!";
  String serviceURI = request.getParameter("serviceURI");
  if (serviceURI == null || "".equals(serviceURI))
    serviceURI = "http://localhost:6060/HelloWorldService/";
%>

<!-- prepare the service call, display parameters and results -->

Results of this request:<br>

       Service URI = <i> <%out.println(serviceURI);%> </i><br>
       String sent to service = <i> <%out.println(reqParam);%> </i><br>
       <%

       // --------- WASP Web service call code begins ---------

       try {

           // lookup service
           if (helloService == null)
           {
             helloService = (HelloWorldService)
                 Registry.lookup(serviceURI, HelloWorldService.class);
           }
           // call service and print out a response message

           out.println("String got from the service = <i>" +
           helloService.hello(reqParam) + "</i><br>");

        } catch (LookupException e) {

           out.println("Could not lookup HelloWorld service at URI " +
            serviceURI+ ": " + e.getMessage());
        }

// --------- WASP Web service call code ends ---------

%>

<!-- HTML form to set the call parameters, HTML epilogue -->

<P>
<form action="" method=POST>
HelloWorld service URI: 
<input type=text size=40 name=serviceURI value="<% out.println(serviceURI); %>">
<br>
String for HelloWorld service: 
<input type=text size=40 name=reqParam value="<% out.println(reqParam); %>">
<br>
<input type=submit>
</form>
</body>
</html>
Step Three: Publishing The JSP Page  Locate

To make the JSP page accessible:

  1. Create the following directory hierarchy in the Tomcat webapps directory:

    mkdir $TOMCAT_HOME/webapps/helloclient/WEB-INF

    mkdir $TOMCAT_HOME/webapps/helloclient/WEB-INF/classes

    mkdir $TOMCAT_HOME/webapps/helloclient/WEB-INF/lib

  2. The servlet depends on a number of jar files from the directory WASP_HOME/lib. Copy them into the directory $TOMCAT_HOME/webapps/helloclient/WEB-INF/lib.

    cd $WASP_HOME/lib

    cp builtin_serialization.jar jaxm.jar jaxrpc.jar log4j.jar saaj.jar wasp.jar wsdl_api.jar $TOMCAT_HOME/webapps/helloclient/WEB-INF/lib

    [Note]Note

    You can find more information about necessary jars in the chapter Manual Porting.

  3. Create a minimal web-application configuration file for Tomcat (You can just copy the one created for this example.):

    cp $WASP_HOME/src/example/applicationIntegration/JSP/web.xml $TOMCAT_HOME/webapps/helloclient/WEB-INF

  4. Now to the JSP files themselves. When the JSP page is opened for the first time, Tomcat pre-processes it, creates servlet source code and compiles the servlet. It needs to have access to files imported by the JSP source. The only file here not contained in a .jar package is the HelloService interface. We need to compile it and place it to the directory where it can be found by Tomcat:

    javac demo/HelloService.java

    cp demo/HelloService.class $TOMCAT_HOME/webapps/helloclient/WEB-INF/classes

  5. And finally move the JSP page code to the proper place:

    cp $WASP_HOME/src/example/applicationIntegration/Jsp/helloclient.jsp $TOMCAT_HOME/webapps/helloclient/helloclient.jsp

  6. Edit $TOMCAT_HOME/webapps/helloclient/helloclient.jsp and replace WASP_HOME with the WSO2 SOA Enablement Server for Java installation directory name.

Testing The JSP Page  Locate

To make the JSP page accessible we need to start the Tomcat server:

$TOMCAT_HOME/bin/catalina.sh start

To test the JSP page, point your browser to its URL: http://localhost:8080/helloclient/helloclient.jsp.

The result of the browser request will look like Figure 28.

Figure 28. WSO2 SOA Enablement Server HelloWorld Client JSP

WSO2 SOA Enablement Server HelloWorld Client JSP