THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
...
- Right click on Java Resources: src and select New->Package.
- Name the package as org.apache.geronimo.samples.jws. Select Finish.
- Right click on the new package and Select New->Interface.
- Name the interface as Calculator. Select Finish.
- Add the following code to the Calculator interface class
Code Block title Calculator.class borderStyle solid package org.apache.geronimo.samples.jws; import javax.jws.WebMethod; import javax.jws.WebParamWebService; import javax.jws.WebResultWebMethod; import javax.jws.WebServiceWebParam; import javax.xml.ws.RequestWrapper; import javax.xml.ws.ResponseWrapper; @WebService(name="CalculatorPortType", portName="CalculatorPort", targetNamespace = "http://jws.samples.geronimo.apache.org") public interface Calculator { /** * @param value1 * @param value2@WebMethod * @return * returns int * */ @WebMethod(operationName = "add") @WebResult public int add(@WebParam(name = "returnvalue1",) targetNamespace = "http://jws.samples.geronimo.apache.org") @RequestWrapper(localName = "add", targetNamespace = "http://jws.samples.geronimo.apache.org", className = "org.apache.geronimo.samples.jws.Add") @ResponseWrapper(localName = "addResponse", targetNamespace = "http://jws.samples.geronimo.apache.org", className = "org.apache.geronimo.samples.jws.AddResponse") public int add(@WebParam(name = "value1", targetNamespace = "http://jws.samples.geronimo.apache.org") int value1, int value1, @WebParam(name = "value2", targetNamespace = "http://jws.samples.geronimo.apache.org") int value2); }
Warning title Warning add explanation for the code
- Right click on the package org.apache.geronimo.samples.jws and select
New->Class. - Name the class CalculatorService
- Accept all the defaults and Select Finish.
- Add the following code to CalculatorService.class
Code Block title CalculatorService.class borderStyle solid package org.apache.geronimo.samples.jws; import javax.annotation.Resource; import javax.jws.WebService; import javax.xml.ws.WebServiceContext; @WebService(serviceName = "Calculator", portName="CalculatorPort", endpointInterface = "org.apache.geronimo.samples.jws.Calculator", targetNamespace = "http://jws.samples.geronimo.apache.org", wsdlLocation = "WEB-INF/CalculatorService.wsdl") public class CalculatorService implements Calculator { @Resource private WebServiceContext context; /** * @return * returns javax.xml.ws.WebServiceContext */ public WebServiceContext getContext() { return @Resource private WebServiceContext context; } /** * @param value1 * @param value2 * @return * returns int */ public int add(int value1, int value2) { System.out.println("User Principal: " + context.getUserPrincipal()); System.out.println("value1: " + value1 + " value2: " + value2); return value1 + value2; } }
This completes the development of Web Services implementation code.
...
- Right Click on the Web Content folder and Select New->JSP.
- Name the jsp as index.jsp and Select Finish.
- Add the following code to index.jsp
Code Block title index.jsp borderStyle solid <%@ page import="java.net.URL,javax.xml.namespace.QName,javax.xml.ws.Service,org .apache.geronimo.samples.jws.Calculator"%> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>Calculator</title> </head> <body> <form action="result.jsp"> Please enter 2 whole numbers to add: <input type="text" name="value1"> + <input type="text" name="value2"> <input type="submit" value="="> </form> </body> </html>
- Right Click on the Web Content folder and Select New->JSP.
- Name the jsp as result.jsp and Select Finish.
- Add the following code to jsp.
This finishes the development of Web client.Code Block title result.jsp borderStyle solid <%@ page import="java.net.URL,javax.xml.namespace.QName,javax.xml.ws.Service,org .apache.geronimo.samples.jws.Calculator"%> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>Calculator Result</title> </head> <% int value1 = 0; int value2 = 0; int sum = 0; try { System.out.println( request.getParameter( "value1" ) + " " + request.getParameter( "value2" ) ); value1 = Integer.parseInt( request.getParameter( "value1" ) ); value2 = Integer.parseInt( request.getParameter( "value2" ) ); URL url = new URL("http://localhost:8080/jaxws-calculator- 1.0/calculator?wsdl"); QName qname = new QName("http://jws.samples.geronimo.apache.org", "Calculator"); Service service = Service.create(url, qname); Calculator calc = (Calculator)service.getPort(Calculator.class); sum = calc.add(value1, value2); } catch ( Exception e ) { e.printStackTrace(); } %> <body> The result is: <%=value1%>+<%=value2%>=<%=sum%> <br> <a href="index.jsp">Back</a> </body> </html>