...
Table of Contents |
---|
maxLevel | 2 |
---|
type | list |
---|
separator | pipe |
---|
|
Web based Client
The following steps will help you in creating a Web based Client for a JAX-WS Web Service which is already deployed on the server.
Create a Dynamic Web Project to consume the Web Service
...
- Now all the stubs should have been created and placed in C:\WSDL directory. We will be needing only the class ConverterPortType for accessing Web Service.
- Copy the file ConverterPortType files Converter.java and ConverterPortType.java and place them in the appropriate folder according to its package declaration into the projecttheir package declaration
Let us walk through the steps that we just completed
- Converter.java - This file extends the javax.xml.ws.Service and provides method to create a service and retrieve port which is the Service Endpoint Interface for the Web Service deployed.
- ConverterPortType.java - This is the same file that is used to deploy the Web Service. This is the Service Endpoint Interface that is required to access the Web Methods exposed by the Service.
- jaxws-tools.bat wsimport - This is the utility in Geronimo which takes the help of jaxws-tools provided by Sun to create client stubs
Developing the Web based Client
- 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 |
---|
title | index.jsp |
---|
borderStyle | solid |
---|
|
<%@ 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="result.jsp">Amount(in Dollars): <input type="text"
name="amount"> <input type="submit" value="Submit"></form>
<br>
</center>
</body>
</html>
|
- Right click again and add another jsp named result.jsp
- Add the following code to result.jsp
Code Block |
---|
title | result.jsp |
---|
borderStyle | solid |
---|
|
<%@ 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 = conv.dollarToRupees(dollars);
BigDecimal euros = conv.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>
|
This concludes the development section of our web based client.
Deploying and Testing the Web Client
Deploy
- Right now our server status might be reporting that we need to republish.
- Right click on the Apache Geronimo Server Runtime present in the servers view and select Publish
- Wait for some time till the server status changes to Synchronized
Testing
- Right click the index.jsp present under WebContent directory of our project and select Run As->Run On Server
- In the popup, check the check box Always use this server when running the project and then click Finish
- Now Eclipse will try to open the jsp in a web browser which shows you a form to enter amount in Dollars.
- Enter any amount and press submit, the jsp should display the result that is returned by the web service.
Info |
---|
|
Note that we accessed the webservice in the jsp by creating a service, and then retrieving the Converter port from the WSDL document. |