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

...

  • Right Click the jaxws-saaj-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. <br>
<form action="index.jsp">Amount: <input type="text" name="amount">
<input type="submit" value="Submit"></form>
<br>
<jsp:include page="ConverterHandler"></jsp:include></center>
</body>
</html>

  • Right click again and add a Servlet named ConverterHandler



  • Add the following code to ConverterHandler.java
Code Block
borderStylesolid
titleConverterHandler.javaborderStylesolid

package abc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

import org.w3c.dom.Node;

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 {
		String dollars = request.getParameter("amount");
		if (dollars != null && dollars.trim().length() > 0) {
			String rupees = null, euros = null;
			try {
				rupees = returnResult(createSOAPMessage("dollarToRupees",
						dollars));
				euros = returnResult(createSOAPMessage("rupeesToEuro", rupees));
			} catch (SOAPException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			PrintWriter out = response.getWriter();
			out.println("<br><br><br>");
			out.println(dollars + " Dollars equals to " + rupees + " Rupees");
			out.println("<br>");
			out.println(rupees + " Rupees equals to " + euros + " Euros");
		}
	}

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

	public SOAPMessage createSOAPMessage(String operation, String arg)
			throws SOAPException {
		String urn = "http://jaxws.samples.geronimo.apache.org";
		MessageFactory messageFactory;
		SOAPMessage message = null;
		messageFactory = MessageFactory.newInstance();
		message = messageFactory.createMessage();
		SOAPPart soapPart = message.getSOAPPart();
		SOAPEnvelope envelope = soapPart.getEnvelope();
		SOAPBody body = envelope.getBody();
		SOAPElement bodyElement = body.addChildElement(envelope.createName(
				operation, "ns1", "urn:" + urn));
		bodyElement.addChildElement("arg0").addTextNode(arg);
		message.saveChanges();
		return message;
	}

	public String returnResult(SOAPMessage message) throws SOAPException {
		String destination = "http://localhost:8080/jaxws-converter/converter";
		SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory
				.newInstance();
		SOAPConnection connection = soapConnFactory.createConnection();
		SOAPMessage reply = connection.call(message, destination);
		SOAPPart soapPart = reply.getSOAPPart();
		SOAPEnvelope envelope = soapPart.getEnvelope();
		SOAPBody body = envelope.getBody();
		Iterator iter = body.getChildElements();
		Node resultOuter = ((Node) iter.next()).getFirstChild();
		Node result = resultOuter.getFirstChild();
		connection.close();
		return result.getNodeValue();
	}
}

...