java web(六)多个请求对应一个Servlet
概要:
提交请求的常用方式有两种,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 "
最新文章
- 转-基于NodeJS的14款Web框架
- Jsp字符编码过滤器
- tomcat连接器
- Qt实现小功能之列表无限加载
- 创业型互联网公司应该选择PHP, JavaEE还是.NET技术路线?
- 第二章 OO大原则
- 2013年7月份第1周51Aspx源码发布详情
- ExtJS4.2学习(21)动态菜单与表格数据展示操作总结篇2
- MySQL在Django框架下的基本操作(MySQL在Linux下配置)
- LINUX增加并管理用户
- 移动web开发研究
- 阿里云ECS部署ES
- django celery的分布式异步之路(一) 起步
- HttpClient4 TIME_WAIT和CLOSE_WAIT
- SpringBoot系列: Json的序列化和反序列化
- nodeJs的Buffer操作
- C# yield关键词使用
- CSS框架BluePrint
- echarts - 特殊需求实现方案汇总
- [javaSE] 网络编程(TCP通信)
热门文章
- gnu coreutils-8.25 for win32 static - Beta
- Html 5 Web Storage
- iOS内支付
- javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
- spring加载过程,源码带你理解从初始化到bean注入
- 关于python数据序列化的那些坑
- R3.2.2安装
- iOS-APP提交上架流程(新手必看!2016年3月1日最新版)
- javase-排序
- tp框架之AJAX