JavaWeb(33) : BaseServlet

一、BaseServlet是用来干嘛的

 

public class AServlet extends HttpServlet {
	
	@Override
	public void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 1.获取参数,用来识别哪种方法
		String methodName = req.getParameter("method");
		
		
		// 2.判断是哪种方法并调用
	        if (methodName.equals("addUser")) {
		    addUser(req, resp);
	        }else if (methodName.equals("editUser")) {
		    editUser(req, resp);
	        }else if (methodName.equals("deleteUser")) {
		    deleteUser(req, resp);
	        }
		
		


	public void addUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("addUser()...");


	}

	public void editUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("editUser()...");


	}
	
	public void deleteUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("deleteUser()...");

	}

	public void loadUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("loadUser()...");

	}
}

上面试之前我们获取请求参数并调用方法的方式。这样写每次多一个方法都要加一个else if 判断,很麻烦。

而且方法越多效率越低。

我们希望创建一个类,通过反射可以调用它自己的任何方法。

具体要调用哪个方法,由请求所带的参数决定,这样我们就不必写那么多if...else if语句,而只把重点放在如何实现业务需求了。

二、BaseServlet代码

public abstract class BaseServlet extends HttpServlet {
	public void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		/*
		 * 1. 获取参数,用来识别用户想请求的方法
		 * 2. 然后判断是否哪一个方法,是哪一个我们就调用哪一个
		 */
		String methodName = req.getParameter("method");
		
		if(methodName == null || methodName.trim().isEmpty()) {
			throw new RuntimeException("您没有传递method参数!无法确定您想要调用的方法!");
		}
		
		/*
		 * 得到方法名称,是否可通过反射来调用方法?
		 * 1. 得到方法名,通过方法名再得到Method类的对象!
		 *   * 需要得到Class,然后调用它的方法进行查询!得到Method
		 *   * 我们要查询的是当前类的方法,所以我们需要得到当前类的Class
		 */
		Class c = this.getClass();//得到当前类的class对象
		Method method = null;
		try {
			method = c.getMethod(methodName, 
					HttpServletRequest.class, HttpServletResponse.class);
		} catch (Exception e) {
			throw new RuntimeException("您要调用的方法:" + methodName + "(HttpServletRequest,HttpServletResponse),它不存在!");
		}
		
		/*
		 * 调用method表示的方法
		 */
		try {
			String result = (String)method.invoke(this, req, resp);
			/*
			 * 获取请求处理方法执行后返回的字符串,它表示转发或重定向的路径!
			 * 帮它完成转发或重定向!
			 */
			/*
			 * 如果用户返回的是字符串为null,或为"",那么我们什么也不做!
			 */
			if(result == null || result.trim().isEmpty()) {
				return;
			}
			/*
			 * 查看返回的字符串中是否包含冒号,如果没有,表示转发
			 * 如果有,使用冒号分割字符串,得到前缀和后缀!
			 * 其中前缀如果是f,表示转发,如果是r表示重定向,后缀就是要转发或重定向的路径了!
			 */
			if(result.contains(":")) {
				// 使用冒号分割字符串,得到前缀和后缀
				int index = result.indexOf(":");//获取冒号的位置
				String s = result.substring(0, index);//截取出前缀,表示操作
				String path = result.substring(index+1);//截取出后缀,表示路径
				if(s.equalsIgnoreCase("r")) {//如果前缀是r,那么重定向!
					resp.sendRedirect(req.getContextPath() + path);
				} else if(s.equalsIgnoreCase("f")) {
					req.getRequestDispatcher(path).forward(req, resp);
				} else {
					throw new RuntimeException("你指定的操作:" + s + ",当前版本还不支持!");
				}
			} else {//没有冒号,默认为转发!
				req.getRequestDispatcher(result).forward(req, resp);
			}
		} catch (Exception e) {
			System.out.println("您调用的方法:" + methodName + ", 它内部抛出了异常!");
			throw new RuntimeException(e);
		}
	}
}

有了BaseServlet之后,只需新建Servlet并继承BaseServlet即可。注意:新建Servlet中所有方法修饰符都要为public,否则反射无法获取,会出现异常。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值