src cod down http://www.blogjava.net/Files/zhaochengming/servletInvoke.rar
<1>先定义一些解析的方法,这里是处理接到请求后的处理
package com.invoke.process;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface InvokeAnno {
/**
* 调用的函数名称
*/
String methodName();
/**
* 调用的类
*/
Class invodeClass();
/**
* 调用的参数类型
*/
Class[] paramClass();
/**
* 返回类型
*/
Class returnClass() default Object.class;
}
package com.invoke.process;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
public class InvokeBussiness {
public static Object query(InvokeEnum enu,Object obj) {
try {
Field field = InvokeEnum.class.getField(enu.toString());
InvokeAnno sa = field.getAnnotation(InvokeAnno.class);
Class[] cs = sa.paramClass();
Class invodeClass = sa.invodeClass();
Method method = invodeClass.getMethod(sa.methodName(),cs);
if (cs.length == 1) {
obj = method.invoke(invodeClass.newInstance(), obj);
} else if (cs.length == 0) {
obj = method.invoke(invodeClass.newInstance());
} else {
obj = method.invoke(invodeClass.newInstance(), ((List)obj).toArray());
}
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
}
package com.invoke.process;
import java.util.List;
import com.invoke.bussiness.ValidateUser;
public enum InvokeEnum {
/**
* 验证用户名密码
*/
@InvokeAnno(invodeClass=ValidateUser.class, methodName="isRightUser",
paramClass={String.class,String.class},returnClass = boolean.class)
ValidateUser,
@InvokeAnno(invodeClass=ValidateUser.class, methodName="queryAllUser",
paramClass={},returnClass = List.class)
GetAllUser,
}
<2>服务端的业务
package com.invoke.bussiness;
import java.util.ArrayList;
import java.util.List;
/**
* 验证用户信息
* @author
*
*/
public class ValidateUser {
public boolean isRightUser(String name, String pwd) {
//经过验证,这里假设一个
if (name.equals("admin") && pwd.equals("123456"))
return true;
return false;
}
/**
* 返回字符串
* 用户名,密码,角色,部门,电话
* @return
*/
public List queryAllUser() {
//假设查询的用户列表
List<String> list = new ArrayList<String>();
list.add("admin1,123456,组织管理员,技术,13917811682");
list.add("admin2,123456,组织管理员,技术,13917811682");
list.add("admin3,123456,组织管理员,技术,13917811682");
list.add("admin4,123456,组织管理员,技术,13917811682");
list.add("admin5,123456,组织管理员,技术,13917811682");
return list;
}
}
<3>服务端的Servlet
package com.invoke;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.invoke.process.InvokeBussiness;
import com.invoke.process.InvokeEnum;
public class InvokeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
response.setContentType("application/octest-stream");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(baos);
List reqList;
try {
reqList = (List) ois.readObject();
// 开始分析传入的List
String enu = (String) reqList.get(1);
Object params = reqList.get(2);
out.writeObject(InvokeBussiness.query(InvokeEnum.valueOf(enu), params));
} catch (Exception e) {
e.printStackTrace();
}
byte buf[] = baos.toByteArray();
response.setContentLength(buf.length);
ServletOutputStream servletout = response.getOutputStream();
servletout.write(buf);
servletout.close();
}
}
<4>客户端
package com.client;
import java.util.List;
public interface IRemote {
/**
* 调用远处的Servlet
* @param listObj 传入的参数对象
* 其中 listObj.get(0) 为 Servlet地址url 如 : http://192.168.253.254:8080/ibm/IbmServlet
* listObj.get(1)为参数命令 ,如 GetAllUser,ValidateUser等,为事先约定好的命令
* listObj.get(2)为调用的参数集合,是一个List对象
* @return Object
* @throws Exception
*/
public Object inokeServlet(List listObj) throws Exception;
}
package com.client;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
public class Client implements IRemote {
public Object inokeServlet(List listObj) throws Exception {
URL url = new URL((String)listObj.get(0));
URLConnection con = url.openConnection();
con.setUseCaches(true);
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-type", "application/octest-stream");
con.setRequestProperty("Content-length", "" + -1);
ObjectOutputStream dataout = new ObjectOutputStream(con
.getOutputStream());
dataout.writeObject(listObj);
dataout.flush();
dataout.close();
ObjectInputStream in = new ObjectInputStream(con.getInputStream());
Object obj = in.readObject();
in.close();
return obj;
}
public boolean validateUser(String userName, String pwd) {
List<Object> list = new ArrayList<Object>();
list.add("http://localhost:8080/servletInvoke/InvokeServlet");
list.add("ValidateUser");
List<String> param = new ArrayList<String>();
param.add(userName);
param.add(pwd);
list.add(param);
try {
Boolean bol = (Boolean)inokeServlet(list);
if (bol)
System.out.println("验证成功!");
else
System.out.println("验证失败,清检查你的用户名密码!");
return bol;
} catch (Exception e) {
e.printStackTrace();
System.out.println("调用服务器失败!");
}
return false;
}
public List getAllUser() {
List<Object> list = new ArrayList<Object>();
list.add("http://localhost:8080/servletInvoke/InvokeServlet");
list.add("GetAllUser");
List<String> param = new ArrayList<String>();
list.add(param);
try {
List res = (List)inokeServlet(list);
return res;
} catch (Exception e) {
e.printStackTrace();
System.out.println("获得用户列表时候,调用服务器失败!");
}
return null;
}
public static void main(String args[]) {
Client client = new Client();
client.validateUser("admin", "123456");
List list = client.getAllUser();
for (int i = 0; i < list.size(); i++)
System.out.println(list.get(i));
}
}
</script>