java customerservlet_java web(六)多个请求对应一个Servlet

概要:

提交请求的常用方式有两种,get/post , 运行程序后被请求,在加载执行web.xml文件时通过该文件中的映射关系找到即将要执行的Servlet; 而在要执行的Servlet文件中可通过反射的方式找到要执行的方法,部分代码如下:

web.xml

CustomerServlet

com.kk.servlet.CustomerServlet

CustomerServlet

*.do

---提交的请求都要以 ".do" 结尾

CustomerServlet.java

doPost方法体中:

//1、获取ServletPath: /edit.do或 addCustomer.do

String servletPath=request.getServletPath();

//2、去除 / 和 .do ,得到类似于edit或addCustomer这样的字符串

String methodName=servletPath.substring(1);

methodName=methodName.substring(0, methodName.length()-3);

System.out.println("所获取的值:"+servletPath+" 得到方法名 "+methodName);

try {

//3、利用反射获取menthodName获取对应的的方法

Method method=getClass().getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);

//利用反射调用methodName对应的方法

method.invoke(this, request,response);

} catch (Exception e) {

response.sendRedirect("error.jsp");

}

eg.

private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {

..........................

}

以该方法为例,请求==>" delete.do  "

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值