⑴ java怎么远程调用webservice接口
Java通过WSDL文件来调用webservice:
注意,以下的代码并没有经过真正的测试,只是说明这些情况,不同版本的Axis相差很大,大家最好以apache网站上的例子为准,这里仅仅用于说明其基本用法。
1,直接AXIS调用远程的web service
这种方法比较适合那些高手,他们能直接看懂XML格式的WSDL文件,我自己是看不懂的,尤其我不是专门搞这行的,即使一段时间看懂,后来也就忘记了。直接调用模式如下:
import java.util.Date;
import java.text.DateFormat;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import java.lang.Integer;
import javax.xml.rpc.ParameterMode;
public class caClient {
public static void main(String[] args) {
try {
String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";
//直接引用远程的wsdl文件
//以下都是套路
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName("addUser");//WSDL里面描述的接口名称
call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,
javax.xml.rpc.ParameterMode.IN);//接口的参数
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型
String temp = "测试人员";
String result = (String)call.invoke(new Object[]{temp});
//给方法传递参数,并且调用方法
System.out.println("result is "+result);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}
2,直接SOAP调用远程的webservice
这种模式我从来没有见过,也没有试过,但是网络上有人贴出来,我也转过来
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;
import java.io.*;
import java.net.*;
import java.util.Vector;
public class caService{
public static String getService(String user) {
URL url = null;
try {
url=new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");
} catch (MalformedURLException mue) {
return mue.getMessage();
}
// This is the main SOAP object
Call soapCall = new Call();
// Use SOAP encoding
soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
// This is the remote object we're asking for the price
soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");
// This is the name of the method on the above object
soapCall.setMethodName("getUser");
// We need to send the ISBN number as an input parameter to the method
Vector soapParams = new Vector();
// name, type, value, encoding style
Parameter isbnParam = new Parameter("userName", String.class, user, null);
soapParams.addElement(isbnParam);
soapCall.setParams(soapParams);
try {
// Invoke the remote method on the object
Response soapResponse = soapCall.invoke(url,"");
// Check to see if there is an error, return "N/A"
if (soapResponse.generatedFault()) {
Fault fault = soapResponse.getFault();
String f = fault.getFaultString();
return f;
} else {
// read result
Parameter soapResult = soapResponse.getReturnValue ();
// get a string from the result
return soapResult.getValue().toString();
}
} catch (SOAPException se) {
return se.getMessage();
}
}
}
3,使用wsdl2java把WSDL文件转成本地类,然后像本地类一样使用,即可。
这是像我这种懒人最喜欢的方式,仍然以前面的global weather report为例。
首先 java org.apache.axis.wsdl.WSDL2Java http://www.webservicex.net/globalweather.asmx.WSDL
原本的网址是http://www.webservicex.net/globalweather.asmx?WSDL,中间个各问号,但是Linux下面它不能解析,所以去掉问号,改为点号。
那么就会出现4个文件:
GlobalWeather.java GlobalWeatherLocator.java GlobalWeatherSoap.java GlobalWeatherSoapStub.java
其中GlobalWeatherSoap.java是我们最为关心的接口文件,如果你对RMI等SOAP实现的具体细节不感兴趣,那么你只需要看接口文件即可,在使用的时候,引入这个接口即可,就好像使用本地类一样。
⑵ EJB远程接口调用到底是什么意思
我来谈谈自己的看法。
--
由于一个企业级的应用,他比较大,然后EJB是部署在某个服务器上,内提供一个远程调容用的接口,内部一套你设计的流程来组织数据,反馈给调用的地方。
如何调用呢?
一般是WEB APP,直接调用它的一个借口类,EJB必须发布成JAR包,包含在WEB-APP里面。调用的时候,才能够找到EJB中对应的方法这些。
--
EJB组件式独立部署的。
做几个小东西,严格的按照流程,发布到不同的服务器上,体会下吧。
⑶ 编写servlet 过滤器时,下面哪个接口用于调用过滤器
javax.servlet.Filter
实现来这个接口,这个接口有三源个方法。
void init(FilterConfig var1) 过滤器初始化时调用的方法
void doFilter(ServletRequest var1, ServletResponse var2, FilterChain var3) 过滤器执行时调用的方法。
void destroy() 过滤器被销毁时调用的方法。
⑷ 客户机访问服务器是访问远程接口的方法,还是直接访问远程接口的实例方法
Java 远程处理
Java远程方法调用(RMI)提供了Java程序语言的远程通讯功能,这种特性使客户机上运行的程序可以调用远程服务器上的对象,使Java编程人员能够在网络环境中分布操作。
创建一个简单的Java分布式远程方法调用程序可以按以下几个步骤操作,
一、定义远程接口:
在 Java 中,远程对象是实现远程接口的类的实例, 远程接口声明每个要远程调用的方法。在需要创建一个远程对象的时候,我们通过传递一个接口来隐藏基层的实施细节,客户通过接口句柄发送消息即可。
远程接口具有如下特点:
1) 远程接口必须为public属性。如果不这样,除非客户端与远程接口在同一个包内,否则 当试图装入实现该远程接口的远程对象时,调用会得到错误结果。
2) 远程接口必须扩展接口java.rmi.Remote。
3) 除与应用程序本身特定的例外之外,远程接口中的每个方法都必须在自己的throws从句中 声明java.rmi.RemoteException。(或 RemoteException 的父类)。
4) 作为参数或返回值传递的一个远程对象(不管是直接,还是本地对象中嵌入)必须声明为远 程接口,而不应声明为实施类。
下面是远程接口的定义
[java] view plain
package test;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.math.BigInteger;
public interface Fib extends Remote {
public int getFib(int n) throws RemoteException;
// public BigInteger getFib(BigInteger n) throws RemoteException;
}
二、实现远程接口:
远程对象实现类必须扩展远程对象java.rmi.UnicastRemoteObject类,并实现所定义的远程接口。远程对象的实现类中包含实现每个远程接口所指定的远程方法的代码。这个类也可以含有附加的方法,但客户只能使用远程接口中的方法。因为客户是指向接口的一个句柄,而不是它的哪个类。必须为远程对象定义构造函数,即使只准备定义一个默认构造函数,用它调用基础类构造函数。因为基础类构造函数可能会抛出 java.rmi.RemoteException,所以即使别无它用必须抛出java.rmi.RemoteException例外。
以下是远程对象实现类的声明:
[java] view plain
package test;
import java.math.BigInteger;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class FibImp extends UnicastRemoteObject implements Fib {
public FibImp() throws RemoteException {
super();
}
public int getFib(int n) throws RemoteException {
return n+2;
}
}
三、编写服务器类:
包含 main 方法的类可以是实现类自身,也可以完全是另一个类。下面通过RmiSampleServer 来创建一个远程对象的实例,并通过java.rmi.registry.LocateRegistry类的createRegistry 方法从指定端口号启动注册服务程序,也可以通过执行 rmiregistry 命令启动注册服务程序,注册服务程序的缺省运行端口为 1099。必须将远程对象名字绑定到对远程对象的引用上: Naming.rebind("//localhost:8808/SAMPLE-SERVER" , Server);
以下是服务器类的声明:
[java] view plain
package test;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
public class FibonacciServer {
/**
* @param args
*/
public static void main(String[] args) {
try {
LocateRegistry.createRegistry(8804);
FibImp f = new FibImp();
// 注册到 registry 中
Naming.rebind("//localhost:8804/SAMPLE-SERVER", f);
System.out.println("fib server ready");
} catch (RemoteException re) {
System.out.println("Exception in FibonacciImpl.main: " + re);
} catch (MalformedURLException e) {
System.out.println("MalformedURLException " + e);
}
}
}
四、编写使用远程服务的客户机类:
客户机类的主要功能有两个,一是通过Naming.lookup方法来构造注册服务程序 stub 程序实例,二是调用服务器远程对象上的远程方法。
以下是客户端类的声明:
[java] view plain
package testClient;
import test.Fib;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
public class FibClient {
/**
* @param args
*/
public static void main(String[] args) {
String url = "//localhost:8804/SAMPLE-SERVER";
try {
Fib calc = (Fib) Naming.lookup(url);
for (int i = 0; i < 10; ++i) {
int f = calc.getFib(i);
System.out.println(f);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
}
}
}
⑸ 远程访问服务器的接口
局域网复接口主要是用于制远程访问服务器和局域网进行连接,因局域网类型也是多种多样的,所以这也就决定了远程访问服务器的局域网接口类型也可能是多样的。不同的网络有不同的接口类型,常见的以太网接口主要有AUI、BNC和RJ-45接口,还有FDDI、ATM、光纤接口,这些网络都有相应的网络接口,下面是主要的几种局域网接口。
(1)AUI端口
(2)RJ-45端口
(3)SC端口
⑹ Java如何通过URL调用远程接口并读取返回信
下面代码可供你参考:
String ticket = "";//登录凭证
String url_str = "http://www.sina.com.cn?ticket=";//获取用户认证的帐号URL
String ticket_url = url_str + ticket;
URL url = new URL(ticket_url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
int code = connection.getResponseCode();
if (code == 404) {
throw new Exception("认证无效,找不到此次认证的会话信息!");
}
if (code == 500) {
throw new Exception("认证服务器发生内部错误!");
}
if (code != 200) {
throw new Exception("发生其它错误,认证服务器返回 " + code);
}
InputStream is = connection.getInputStream();
byte[] response = new byte[is.available()];
is.read(response);
is.close();
if (response == null || response.length == 0) {
throw new Exception("认证无效,找不到此次认证的会话信息!");
}
String userId = new String(response, "GBK");
System.out.println(userId);
⑺ 如何使用httpclient远程调用接口 获取数据
java发一个http请求过去,带上参数就可以了啊,跟我们在浏览器上访问资源是一样的 只是它返回的是json格式的数据而已
给你两个方法吧:
public static String do_post(String url, List name_value_pair) throws IOException {
String body = "{}";
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httpost = new HttpPost(url);
httpost.setEntity(new UrlEncodedFormEntity(name_value_pair, StandardCharsets.UTF_8));
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
} finally {
httpclient.getConnectionManager().shutdown();
}
return body;
}
public static String do_get(String url) throws ClientProtocolException, IOException {
String body = "{}";
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
} finally {
httpclient.getConnectionManager().shutdown();
}
return body;
}
⑻ 编写servlet过滤器时,哪个接口用于调用过滤器链中下一个过滤器
void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)这个方法有3个参数,调用过滤器链中内的下一个容过滤器用的是第三个参数
filterChain.doFilter(request, response);
⑼ java远程调用接口的原理和范例,谢谢
Java 远程处理
Java远程方法调用(RMI)提供了Java程序语言的远程通讯功能,这种特性使客户机上运行的程序可以调用远程服务器上的对象,使Java编程人员能够在网络环境中分布操作。
创建一个简单的Java分布式远程方法调用程序可以按以下几个步骤操作,
一、定义远程接口:
在 Java 中,远程对象是实现远程接口的类的实例, 远程接口声明每个要远程调用的方法。在需要创建一个远程对象的时候,我们通过传递一个接口来隐藏基层的实施细节,客户通过接口句柄发送消息即可。
远程接口具有如下特点:
1) 远程接口必须为public属性。如果不这样,除非客户端与远程接口在同一个包内,否则 当试图装入实现该远程接口的远程对象时,调用会得到错误结果。
2) 远程接口必须扩展接口java.rmi.Remote。
3) 除与应用程序本身特定的例外之外,远程接口中的每个方法都必须在自己的throws从句中 声明java.rmi.RemoteException。(或 RemoteException 的父类)。
4) 作为参数或返回值传递的一个远程对象(不管是直接,还是本地对象中嵌入)必须声明为远 程接口,而不应声明为实施类。
下面是远程接口的定义
[java] view plain
package test;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.math.BigInteger;
public interface Fib extends Remote {
public int getFib(int n) throws RemoteException;
// public BigInteger getFib(BigInteger n) throws RemoteException;
}
二、实现远程接口:
远程对象实现类必须扩展远程对象java.rmi.UnicastRemoteObject类,并实现所定义的远程接口。远程对象的实现类中包含实现每个远程接口所指定的远程方法的代码。这个类也可以含有附加的方法,但客户只能使用远程接口中的方法。因为客户是指向接口的一个句柄,而不是它的哪个类。必须为远程对象定义构造函数,即使只准备定义一个默认构造函数,用它调用基础类构造函数。因为基础类构造函数可能会抛出 java.rmi.RemoteException,所以即使别无它用必须抛出java.rmi.RemoteException例外。
以下是远程对象实现类的声明:
[java] view plain
package test;
import java.math.BigInteger;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class FibImp extends UnicastRemoteObject implements Fib {
public FibImp() throws RemoteException {
super();
}
public int getFib(int n) throws RemoteException {
return n+2;
}
}
三、编写服务器类:
包含 main 方法的类可以是实现类自身,也可以完全是另一个类。下面通过RmiSampleServer 来创建一个远程对象的实例,并通过java.rmi.registry.LocateRegistry类的createRegistry 方法从指定端口号启动注册服务程序,也可以通过执行 rmiregistry 命令启动注册服务程序,注册服务程序的缺省运行端口为 1099。必须将远程对象名字绑定到对远程对象的引用上: Naming.rebind("//localhost:8808/SAMPLE-SERVER" , Server);
以下是服务器类的声明:
[java] view plain
package test;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
public class FibonacciServer {
/**
* @param args
*/
public static void main(String[] args) {
try {
LocateRegistry.createRegistry(8804);
FibImp f = new FibImp();
// 注册到 registry 中
Naming.rebind("//localhost:8804/SAMPLE-SERVER", f);
System.out.println("fib server ready");
} catch (RemoteException re) {
System.out.println("Exception in FibonacciImpl.main: " + re);
} catch (MalformedURLException e) {
System.out.println("MalformedURLException " + e);
}
}
}
四、编写使用远程服务的客户机类:
客户机类的主要功能有两个,一是通过Naming.lookup方法来构造注册服务程序 stub 程序实例,二是调用服务器远程对象上的远程方法。
以下是客户端类的声明:
[java] view plain
package testClient;
import test.Fib;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
public class FibClient {
/**
* @param args
*/
public static void main(String[] args) {
String url = "//localhost:8804/SAMPLE-SERVER";
try {
Fib calc = (Fib) Naming.lookup(url);
for (int i = 0; i < 10; ++i) {
int f = calc.getFib(i);
System.out.println(f);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
}
}
}