...
- Right click again and add a Servlet named ConverterHandler
- Add the following code to ConverterHandler.java
Code Block | ||||
---|---|---|---|---|
| ||||
package org.apache.geronimo.samples.jaxws.rest; import java.io.*; import javax.servlet.ServletException; import javax.servlet.http.*; import javax.xml.parsers.*; import javax.xml.xpath.*; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.*; import org.w3c.dom.*; import org.xml.sax.SAXException; public class ConverterHandler extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { static final long serialVersionUID = 1L; public ConverterHandler() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); String query = request.getParameter("query"); if (query != null && query.length() > 0) { out.println("<center>"); out.println("<br><br><br>"); String requestquery = "http://localhost:8080/jaxws-rest-converter/converter?amount=" + query; HttpClient client = new HttpClient(); GetMethod method = new GetMethod(requestquery); int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } InputStream rstream = null; rstream = method.getResponseBodyAsStream(); Document queryresponse = null; try { queryresponse = DocumentBuilderFactory.newInstance() .newDocumentBuilder().parse(rstream); } catch (SAXException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); NodeList nodes = null; try { nodes = (NodeList) xPath.evaluate("/return", queryresponse, XPathConstants.NODESET); } catch (XPathExpressionException e) { e.printStackTrace(); } int nodeCount = nodes.getLength(); for (int i = 0; i < nodeCount; i++) { // Get each xpath expression as a string String rupees = null; String euros = null; try { rupees = (String) xPath.evaluate("dollarToRupeesResponse", nodes.item(i), XPathConstants.STRING); euros = (String) xPath.evaluate("rupeesToEurosResponse", nodes.item(i), XPathConstants.STRING); } catch (XPathExpressionException e) { e.printStackTrace(); } out.println(query + " Dollars equals to " + rupees + " Rupees"); out.println("<br>"); out.println(rupees + " Rupees equals to " + euros + " Euros"); } out.println("</center>"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Has to be Completed } } |
...