Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

  • Open a command prompt and point it to the bin directory of server installation.

  • We will use the jaxws-tools utility provided in the server's bin directory to generate client stubs.

  • Use the following command to generate the necessary stubs that are needed to consume the web service.
Code Block
borderStylesolid
titleClient Stubs GenerationborderStylesolid
<%SERVEER_INSTALLATION_BIN_DIR%>:\ jaxws-tools.bat wsimport -s C:\WSDL converter/converter?wsdl">http://localhost:8080jaxws-converter/converter?wsdl

...

  • Right-click the jaxws-converterclient, and Select New --> JSP

  • Name the JSP as index.jsp and click Finish

  • Add the following code to the index.jsp
Code Block
borderStylesolid
titleindex.jspborderStylesolid

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Converter</title>
<meta content="text/html; CHARSET=iso-8859-1" http-equiv="Content-Type">
</head>

<body>
<center>
<h3>This from invokes a Web Service.</h3>
<br>
Please type an amount and click submit to see the result.
<form action="index.jsp">Amount(in Dollars): <input type="text"
	name="amount"> <input type="submit" value="Submit"></form>
<br><br>
<jsp:include page="result.jsp"></jsp:include>
</center>
</body>
</html>

  • Right click again and add another JSP named result.jsp

  • Add the following code to result.jsp
Code Block
borderStylesolid
titleresult.jspborderStylesolid

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@page import="java.math.BigDecimal"%>
<%@ page import="org.apache.geronimo.samples.jaxws.Converter"%>
<%@ page import="org.apache.geronimo.samples.jaxws.ConverterPortType"%>
<html>
<head>
<title>Converter</title>
<meta content="text/html; CHARSET=iso-8859-1" http-equiv="Content-Type">
</head>
<body>
<%
	String amount = request.getParameter("amount");

	if (amount != null && amount.trim().length() > 0) {

		out.println("<center>");

		try {
			BigDecimal dollars = new BigDecimal(amount);
                        Converter conv = new Converter();
			ConverterPortType port = conv.getConverterPort();
			BigDecimal rupees = port.dollarToRupees(dollars);
			BigDecimal euros = port.rupeesToEuro(rupees);

			out.println(dollars + " Dollars equals to " + rupees
					+ " Rupees");
			out.println("<br>");
			out.println(rupees + " Rupees equals to " + euros
					+ " Euros");

		} catch (Exception e) {
			out.println("Error: " + e.getMessage());
		}

		out.println("<center>");
	}
%>
</body>
</html>

...

  • In our previous result.jsp we accessed the Web Service by instantiating the Service class. This can slightly overhead the client application.

  • We can also use JNDI Naming service to directly access our web service without instantiating any Service class.

  • We need to add a service-ref element to web.xml to give necessary information about WSDL file location, Service name etc .,
Code Block
borderStylesolid
titleModified web.xmlborderStylesolid

<service-ref>
	<service-ref-name>services/Converter</service-ref-name>
	<service-interface>
		org.apache.geronimo.samples.jaxws.Converter
	</service-interface>
	<wsdl-file>
		http://localhost:8080/jaxws-converter/converter?wsdl
	</wsdl-file>
</service-ref>

  • Let us walkthrough the elements that we added to web.xml

    • service-ref-name - This is the name by which JNDI identifies our deployed service. The location that these services are located are java:comp/env/<service-ref-name>
    • service-interface - This is the interface which client uses to access the web service and create necessary stubs under covers. This interface should compulsorily extend either javax.xml.ws.Service or javax.xml.rpc.Service. Observer that this is not the Service-Endpoint Interface that we used to deploy the service.
    • wsdl-file - The location of WSDL file specified the location of deployed web service. Suppose the web service is deployed at *http://localhost:8080/jaxws-converter/converter*, then the WSDL file is located at *http://localhost:8080/jaxws-converter/converter?wsdl*

  • The changes required in accessing the service are as follows:
Code Block
borderStyle
borderStylesolid
titleModified result.jspsolid
........
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.naming.Context" %>
........
Context ctx= new InitialContext();
Object obj = ctx.lookup("java:comp/env/services/Converter");
Converter service= (Converter) obj;
ConverterPortType port = service.getConverterPort();
BigDecimal rupees =port.dollarToRupees(dollars);
BigDecimal euros = port.rupeesToEuro(rupees);
........

...

  • Create a Java Project

    • Select File --> New --> Project

    • In the popup window select Java --> Java Project and then click Next

    • Name the project as jaxws-converterpojoclient and click Finish in the New Java Project dialog.

  • Copy the files Converter.java and ConverterPortType.java into the appropriate package.

  • Add another class with the name ConverterClient.java in the same package.

  • Add the following code to the ConverterClient.java
Code Block
borderStyle
borderStylesolid
titleConverterClient.javasolid

package org.apache.geronimo.samples.jaxws;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;

public class ConverterClient {
	public static void main(String args[]) {
		try {
			BufferedReader dataIn = new BufferedReader(new InputStreamReader(
					System.in));
			System.out.println("enter a amount to convert:");
			String number = new String();
			number = dataIn.readLine();
			BigDecimal amount = new BigDecimal(number);
			Converter conv = new Converter();
			ConverterPortType port = conv.getConverterPort();
			BigDecimal result = port.dollarToRupees(amount);
			System.out.println(amount + " dollars equals to " + result
					+ " rupees");
			BigDecimal result1 = port.rupeesToEuro(result);
			System.out.println(result + " rupees equals to " + result1
					+ " euros");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}


...

Panel
borderColor#ccc
bgColor#FFFFCE
titleBGColor#F7D6C1
borderStylesolid
titleExternal JARS Required(Axis2)borderStylesolid

axis2-adb-1.3.jar - <GERONIMO_INSTALL_DIR>/repository/org/apache/axis2/axis2-adb/1.3/axis2-adb-1.3.jar
axis2-java2wsdl-1.3.jar - <GERONIMO_INSTALL_DIR>\repository\org\apache\axis2\axis2-java2wsdl\1.3\axis2-java2wsdl-1.3.jar
axis2-jaxws-1.3.jar - <GERONIMO_INSTALL_DIR>\repository\org\apache\axis2\axis2-jaxws\1.3\axis2-jaxws-1.3.jar
axis2-kernel-1.3.jar - <GERONIMO_INSTALL_DIR>\repository\org\apache\axis2\axis2-kernel\1.3\axis2-kernel-1.3.jar
axis2-metadata-1.3.jar - <GERONIMO_INSTALL_DIR>\repository\org\apache\axis2\axis2-metadata\1.3\axis2-metadata-1.3.jar
axis2-saaj-1.3.jar - <GERONIMO_INSTALL_DIR>\repository\org\apache\axis2\axis2-saaj\1.3\axis2-saaj-1.3.jar
axiom-api-1.2.5.jar - <GERONIMO_INSTALL_DIR>\repository\org\apache\ws\commons\axiom\axiom-api\1.2.5\axiom-api-1.2.5.jar
axiom-dom-1.2.5.jar - <GERONIMO_INSTALL_DIR>\repository\org\apache\ws\commons\axiom\axiom-dom\1.2.5\axiom-dom-1.2.5.jar
axiom-impl-1.2.5.jar - <GERONIMO_INSTALL_DIR>\repository\org\apache\ws\commons\axiom\axiom-impl\1.2.5\axiom-impl-1.2.5.jar
XmlSchema-1.3.1.jar - <GERONIMO_INSTALL_DIR>\repository\org\apache\ws\commons\schema\XmlSchema\1.3.1\XmlSchema-1.3.1.jar
neethi-2.0.jar - <GERONIMO_INSTALL_DIR>\repository\org\apache\neethi\neethi\2.0\neethi-2.0.jar
wsdl4j-1.6.1.jar - <GERONIMO_INSTALL_DIR>\repository\wsdl4j\wsdl4j\1.6.1\wsdl4j-1.6.1.jar
xml-resolver-1.1.jar - <GERONIMO_INSTALL_DIR>\repository\xml-resolver\xml-resolver\1.1\xml-resolver-1.1.jar
xmlbeans-2.3.0.jar - <GERONIMO_INSTALL_DIR>\repository\org\apache\xmlbeans\xmlbeans\2.3.0\xmlbeans-2.3.0.jar
commons-codec-1.3.jar - <GERONIMO_INSTALL_DIR>\repository\commons-codec\commons-codec\1.3\commons-codec-1.3.jar
commons-httpclient-3.0.1.jar - <GERONIMO_INSTALL_DIR>\repository\commons-httpclient\commons-httpclient\3.0.1\commons-httpclient-3.0.1.jar
wstx-asl-3.2.1.jar - <GERONIMO_INSTALL_DIR>\repository\woodstox\wstx-asl\3.2.1\wstx-asl-3.2.1.jar

Also add Server Runtime Library to class path (i.e., via clicking on Add Library...) which reduces the effort for adding some more jars.

Note that these jars are used when JAX-WS Engine is configured as Axis2. If JAX-WS engine is configured as CXF the jars may differ slightly.

...