一个servlet响应多个请求实现方式--反射
2016-09-04 20:35
423 查看
只需写一个servlet,作为一个中转站。
根据传来的参数className反射获取对应的类字节码,methodName反射获取对应的方法。然后调用。
需要调用的时候:用参数指定类名 方法名
http://localhost:8080/OnlineOrder/CenterServlet?className=LoginAction&methodName=login1
为了写成实现以下这种url:用路径信息指定类名 方法名
http://localhost:8080/OnlineOrder/CenterServlet/
className /methodName?userName=wuyiming&password=123456
首先:
@WebServlet(“/CenterServlet”)
改成:@WebServlet(“/CenterServlet/*”)
这样就能匹配/CenterServlet/*的各种url。
调用request.getPathInfo()能获取 / className / methodName
通过截取pathInfo.substring(1,pathInfo.length()).split(“/”);
就可以获得className或methodName
String pathInfo = request.getPathInfo();
String[] classAndMethodName = pathInfo.substring(1,pathInfo.length()).split(“/”);
String className = “com.wu.action.”+classAndMethodName[0];
String methodName = classAndMethodName[1];
根据传来的参数className反射获取对应的类字节码,methodName反射获取对应的方法。然后调用。
@WebServlet("/CenterServlet") public class CenterServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String className = request.getParameter("className"); className = "com.wu.action."+className; String methodName = request.getParameter("methodName"); Class clazz = null; Method method = null; //className=com.wu.action.LoginAction 这里的className要完整 clazz = Class.forName(className);// method = clazz.getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class); method.invoke(clazz,request, response); //object is not an instance of declaring class说明未实例化,我们可以让反射的方法称为静态,就不要实例化了。 //method.invoke(request, response);wrong number of arguments //对于静态的,只需传入clazz。method.invoke(clazz,request, response); //或者也可以method.invoke(_class.newInstance(), args); } } 创建其他普通的java类 public class LoginAction { public static void login(HttpServletRequest request, HttpServletResponse response){ PrintWriter out = response.getWriter(); out.write("LoginAction login"); System.out.println("LoginAction login"); } public static void login1(HttpServletRequest request, HttpServletResponse response){ PrintWriter out = response.getWriter(); out.write("LoginAction login1"); System.out.println("LoginAction login1"); } }
需要调用的时候:用参数指定类名 方法名
http://localhost:8080/OnlineOrder/CenterServlet?className=LoginAction&methodName=login1
为了写成实现以下这种url:用路径信息指定类名 方法名
http://localhost:8080/OnlineOrder/CenterServlet/
className /methodName?userName=wuyiming&password=123456
首先:
@WebServlet(“/CenterServlet”)
改成:@WebServlet(“/CenterServlet/*”)
这样就能匹配/CenterServlet/*的各种url。
调用request.getPathInfo()能获取 / className / methodName
通过截取pathInfo.substring(1,pathInfo.length()).split(“/”);
就可以获得className或methodName
String pathInfo = request.getPathInfo();
String[] classAndMethodName = pathInfo.substring(1,pathInfo.length()).split(“/”);
String className = “com.wu.action.”+classAndMethodName[0];
String methodName = classAndMethodName[1];
相关文章推荐
- Struts2学习笔记之简单登录
- 第2周项目0-宣告主权
- noip模拟赛(一)密码
- ASP.NET MVC5学习笔记01
- Apache ab 压测
- 针对多种屏幕进行设计
- 抽象工厂模式
- 统计文件中数据出现的次数并排序
- 机房收费系统(四)-上下机总结
- py-faster-rcnn源码解读系列(五)——stage1_rpn_train.pt
- 九度题目1481:Is It A Tree?
- Java - 华为机试训练 - 密码验证合格程序(正则表达式判断)
- 九度题目1481:Is It A Tree?
- css盒模型
- 筛选法找素数算法的一点改进
- rsync 使用示例
- pip安装lxml时报错
- Android并发-Future,FutureTask和Callable及AsyncTask
- mvc从分析到实例,实例到总结
- 于堆栈的(Stack-based ) 和基于寄存器(Register-based) 的虚拟机区别