反射机制构建web应用(Demo)
2009-04-17 19:55
274 查看
构建处理事务的Servlet,并部署web.xml中,引用路径/handle
package com.reflect.handle;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HandlerServlet extends HttpServlet{
protected void service(HttpServletRequest request, HttpServletResponse response){
try{
request.setCharacterEncoding("GB2312");
response.setContentType("text/html; charset=GB2312");
PrintWriter out = response.getWriter();
String className = request.getParameter("class").trim();
String classMethod = request.getParameter("method").trim();
String param = request.getParameter("param").trim();
Class c = Class.forName(className);
Object targetObj = c.newInstance();
Class[] param1={String.class};
Method setNameMethod = c.getMethod(classMethod, param1);
Object[] argObj = {className};
Object k = setNameMethod.invoke(targetObj, argObj);
out.print(k);
}
catch(Exception ex){
out.print(ex.getMessage());
}
}
}
业务类1
package com.reflect;
public class Test1{
public String Test(String kk){
return "com.reflect.Test1:Test()"+kk;
}
}
业务类2
package com.reflect;
public class Test 2{
public String Test(String kk){
return "com.reflect.Test2:Test()"+kk;
}
}
访问:
http://localhost:8080/handle?class=com.reflect.Test1&&method=Test&¶m=Qiu
http://localhost:8080/handle?class=com.reflect.Test2&&method=Test&¶m=Qiu
原文出处:http://blog.csdn.net/qiulei563/archive/2009/03/22/4015143.aspx
package com.reflect.handle;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HandlerServlet extends HttpServlet{
protected void service(HttpServletRequest request, HttpServletResponse response){
try{
request.setCharacterEncoding("GB2312");
response.setContentType("text/html; charset=GB2312");
PrintWriter out = response.getWriter();
String className = request.getParameter("class").trim();
String classMethod = request.getParameter("method").trim();
String param = request.getParameter("param").trim();
Class c = Class.forName(className);
Object targetObj = c.newInstance();
Class[] param1={String.class};
Method setNameMethod = c.getMethod(classMethod, param1);
Object[] argObj = {className};
Object k = setNameMethod.invoke(targetObj, argObj);
out.print(k);
}
catch(Exception ex){
out.print(ex.getMessage());
}
}
}
业务类1
package com.reflect;
public class Test1{
public String Test(String kk){
return "com.reflect.Test1:Test()"+kk;
}
}
业务类2
package com.reflect;
public class Test 2{
public String Test(String kk){
return "com.reflect.Test2:Test()"+kk;
}
}
访问:
http://localhost:8080/handle?class=com.reflect.Test1&&method=Test&¶m=Qiu
http://localhost:8080/handle?class=com.reflect.Test2&&method=Test&¶m=Qiu
原文出处:http://blog.csdn.net/qiulei563/archive/2009/03/22/4015143.aspx
相关文章推荐
- 反射机制构建web应用(Demo)
- C#中使用反射机制得到类型实例应用Demo
- 利用Django构建web应用及其部署
- WSE3.0构建Web服务安全(1):WSE3.0安全机制与实例开发
- 【Python】python 反射机制在实际的应用场景讲解
- Android Web App官方文档翻译第三章:WebView构建应用
- 使用 HTML5 WebSocket 构建实时 Web 应用
- JAVA 应用 III - 反射机制
- Android Web App官方文档翻译第三章:WebView构建应用
- java类型安全枚举与反射机制相结合的应用 作者:封宇
- Spring+SpringMVC+Mybatis(SSM)+Maven构建web项目(demo)步骤
- 【Web缓存机制概述】3 – 如何构建可缓存站点
- 使用Asp.net MVC, Jquery, Jquery UI构建基于Ajax的RIA应用.(更新Demo下载)
- 10个具体项目生动精彩讲述JavaScript;超级Web应用,构建不再困难
- HTML5本地存储之Web SQL+LocalStorage+ApplicationCache以及indexed+LocalStorage+ApplicationCache构建的离线应用实例代码
- 用Java缓存机制创建更快的Web应用----基本用法
- 二、spring Boot构建的Web应用中,基于MySQL数据库的几种数据库连接方式进行介绍
- Spring构建Web应用部署至Tomcat开发过程中的数据编码问题
- 构建dubbo服务消费者web应用的war包
- 利用gitlab、maven、docker自动构建web应用