概要:

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

web.xml

<servlet>
    <servlet-name>CustomerServlet</servlet-name>
    <servlet-class>com.kk.servlet.CustomerServlet</servlet-class>
  </servlet>

<servlet-mapping>
    <servlet-name>CustomerServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  ---提交的请求都要以 ".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  "

最新文章

  1. 转-基于NodeJS的14款Web框架
  2. Jsp字符编码过滤器
  3. tomcat连接器
  4. Qt实现小功能之列表无限加载
  5. 创业型互联网公司应该选择PHP, JavaEE还是.NET技术路线?
  6. 第二章 OO大原则
  7. 2013年7月份第1周51Aspx源码发布详情
  8. ExtJS4.2学习(21)动态菜单与表格数据展示操作总结篇2
  9. MySQL在Django框架下的基本操作(MySQL在Linux下配置)
  10. LINUX增加并管理用户
  11. 移动web开发研究
  12. 阿里云ECS部署ES
  13. django celery的分布式异步之路(一) 起步
  14. HttpClient4 TIME_WAIT和CLOSE_WAIT
  15. SpringBoot系列: Json的序列化和反序列化
  16. nodeJs的Buffer操作
  17. C# yield关键词使用
  18. CSS框架BluePrint
  19. echarts - 特殊需求实现方案汇总
  20. [javaSE] 网络编程(TCP通信)

热门文章

  1. gnu coreutils-8.25 for win32 static - Beta
  2. Html 5 Web Storage
  3. iOS内支付
  4. javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
  5. spring加载过程,源码带你理解从初始化到bean注入
  6. 关于python数据序列化的那些坑
  7. R3.2.2安装
  8. iOS-APP提交上架流程(新手必看!2016年3月1日最新版)
  9. javase-排序
  10. tp框架之AJAX