BaseServlet方法分发
方法 分发
2023-09-11 14:22:18 时间
BaseServlet.java
1 package org.guangsoft.controller;
2
3 import java.io.IOException;
4 import java.lang.reflect.InvocationTargetException;
5 import java.lang.reflect.Method;
6
7 import javax.servlet.ServletException;
8 import javax.servlet.http.HttpServlet;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 public class BaseServlet extends HttpServlet
13 {
14 /**
15 * 将请求方法到不同的servlet中的不同方法
16 */
17 @Override
18 protected void service(HttpServletRequest request, HttpServletResponse response)
19 throws ServletException, IOException
20 {
21 request.setCharacterEncoding("UTF-8");
22 response.setCharacterEncoding("UTF-8");
23 response.setContentType("text/html; charset=utf-8");
24 try
25 {
26 //获取调用的方法名
27 String option = request.getParameter("option");
28 //获取真实调用的servlet字节码文件
29 Class clazz = this.getClass();
30 //获取调用的方法
31 Method method = clazz.getDeclaredMethod(option, HttpServletRequest.class,HttpServletResponse.class);
32 //执行调用的方法
33 method.invoke(this, request, response);
34 }
35 catch (Exception e)
36 {
37 e.printStackTrace();
38 }
39 }
40 }
相关文章
- a标签点击不跳转的几种方法
- Java ceil() 方法
- 【COCOS2D-X(1.X 2.X) 修复篇】IOS6 中LIBCURL.A 无法通过ARMV7S编译以及IOS6中无法正常游戏横屏的解决方法
- 【转载】Python脚本判断一个数是否为素数的几种方法
- 提升R代码运算效率的11个实用方法
- viusal studio 调试错误及解决方法(长期更新记录)
- Algorithm:机械优化设计的数学模型简介、常用优化方法、优化计算工具简介之详细攻略
- Android 圆形/圆角图片的方法
- 097:vue+openlayers: 非4326,3857的投影方法 (示例代码)
- 炫龙毁灭者KP2笔记本U盘重装系统方法图文详细教学
- Java中的getResource()方法,及路径相关问题
- JS截取字符串(3种方法)