⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 soapclientservlet.java

📁 JAVA Servlet2.3外文书籍源码
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;

public class SoapClientServlet extends HttpServlet {
	public void doPost (HttpServletRequest request,
                      HttpServletResponse response)
      throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("</head>");
    out.println("<body>");

    out.println("Exchange rate for Japan is: " +
                getExchangeRate("JP") +
                " Yen/Dollar");
    
    out.println("</body>");
    out.println("</html>");
    out.close();
  }

  private String getExchangeRate(String country) {
    String rateString;

    try{
      URL url = new URL( "http://localhost:8080/soap/servlet/rpcrouter" );
      String encodingStyleURI = Constants.NS_URI_SOAP_ENC;
      URLConnection connection = url.openConnection();
      // Build the call.
      Call call = new Call();
      call.setTargetObjectURI("SoapExchangeRate");
      call.setMethodName("getExchangeRate");
      Vector params = new Vector();
      call.setEncodingStyleURI(encodingStyleURI);
      params.addElement(new Parameter("country", String.class, country, null));
      call.setParams (params);
      Response resp = call.invoke (url, "");
      if (resp.generatedFault()) {
        Fault fault = resp.getFault();
        rateString = fault.getFaultString();
      } else {
        Parameter result = resp.getReturnValue();
        rateString = ((Double)result.getValue()).toString();
      }  
    } catch(Exception e) {
      rateString = e.getMessage();
    }
    return rateString;
  }

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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -