用简单的反射优化代码(动态web项目)
在动态web项目中,没有使用框架时,只是简单的jsp访问servlet实现增删改查,
无论是哪个方法都需要经过Servlet中的doGet()方法或doPost()方法,我们可以在链接中附带参数进行区分,
但是这样doGet()方法或doPost()方法中的代码就会非常长,不方便查看和管理。
有两种解决办法:
1、定义多个Servlet,显然这样也比较繁琐。
2、利用简单的反射对代码进行优化
我们还是用在链接中附带参数的方法,向后台传递一个请求参数oper(页面中使用了EL表达式,JSTL标签,JQuery和AJAX)
<table>
<c:choose>
<c:when test="${not empty userList }">
<tr><th>id</th><th>姓名</th><th>密码</th><th>邮箱</th><th>操作</th></tr>
<c:forEach var="user" items="${userList }">
<tr><td>${user.userid }</td><td>${user.username }</td>
<td>${user.password }</td><td>${user.useremail }</td>
<td><a href='<c:url value="/LoginServlet?oper=detail&userId=${user.userid }"></c:url>'>详情</a>
<a href='<c:url value="/LoginServlet?oper=edit&userId=${user.userid }"></c:url>'>修改</a>
<a onclick="delConfirm(${user.userid })">删除</a></td></tr>
</c:forEach>
</c:when>
<c:otherwise></c:otherwise>
</c:choose>
</table>
<script type="text/javascript">
function delConfirm(userid){
var b=confirm("确定删除吗?");
if(b){
$(function(){
$.post("${pageContext.request.contextPath}/LoginServlet?oper=delete",
{
userid:userid
},
function(data,status){
if(data==1){
alert("删除成功!");
}else{
alert("删除失败!");
}
window.location.href="${pageContext.request.contextPath}/LoginServlet?oper=login";
});
});
}
}
</script>
在后台获取请求参数oper,将其映射成函数,然后就可以在相应的函数中做处理了。
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
// 获取请求参数oper
String methodName = request.getParameter("oper");
// 获取当前类的Class对象
Class cla = this.getClass();
try {
// 通过方法名获取到方法的对象
// getDeclaredMethod需要两个参数,方法名和参数名,因为在java需要通过方法名和参数列表来确定一个方法
Method method = cla.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
// 设置方法的访问权限
method.setAccessible(true);
// 调用方法
// invoke用于调用一个方法,第一个参数时要调用方法的对象,剩下是调用方法需要的参数
method.invoke(this, request, response);
} catch (Exception e) {
e.printStackTrace();
}
} protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} public void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//对请求和响应做处理
}
public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//对请求和响应做处理
}
public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//对请求和响应做处理
}
public void delete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
//对请求和响应做处理
}
//登录
public void login(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
//对请求和响应做处理
}
用简单的反射优化代码(动态web项目)的更多相关文章
- 简单的文件上传的下载(动态web项目)
1.在页面中定义一个form表单,如下: <!-- 文件上传 --> <form action="${pageContext.request.contextPath}/Fi ...
- springmvc 项目完整示例02 项目创建-eclipse创建动态web项目 配置文件 junit单元测试
包结构 所需要的jar包直接拷贝到lib目录下 然后选定 build path 之后开始写项目代码 配置文件 ApplicationContext.xml <?xml version=" ...
- 创建一个动态Web项目:
开始你的Eclipse,然后进入“文件”>“新建”>“动态Web项目,然后输入项目名称为HelloWorldStruts2和设置其他的选项,在下面的屏幕: 选择在屏幕上的所有默认选项,最后 ...
- 搭建Dynamic Web Project(动态web项目)的springmvc工程2
本文转载自:http://blog.csdn.net/typa01_kk/article/details/45905129 此篇为“创建Dynamic Web Projec工程,”搭建Dynamic ...
- 搭建Dynamic Web Project(动态web项目)的springmvc工程1
本文转载自:http://blog.csdn.net/typa01_kk/article/details/45902955 此篇创建Dynamic Web Projec工程(动态web项目),下一篇, ...
- IDEA创建动态Web项目
1.IDEA创建动态Web项目 1.1.使用IDEA创建动态Web项目,选择Java Enterprise,记得选择服务器,我这里使用的时tomcat 1.2记得勾选Web Application,其 ...
- Velocity笔记--使用Velocity获取动态Web项目名的问题
以前使用jsp开发的时候,可以通过request很轻松的获取到根项目名,现在换到使用velocity渲染视图,因为已经不依赖servlet,request等一些类的环境,而Web项目的根项目名又不是写 ...
- Eclipse 中构建 Maven 项目的完整过程 - 动态 Web 项目
进行以下步骤的前提是你已经安装好本地maven库和eclipse中的maven插件了(有的eclipse中已经集成了maven插件) 一.Maven项目的新建 1.鼠标右键---->New--- ...
- 用eclipse创建动态web项目手动生成web.xml方法
建一个web项目,后来在用到web.xml文件时,才发现项目创建时没有自动创建web.xml文件. 在创建的项目上单击右键,然后单击java EE Tools下的用红线圈住的地方,然后查看你的WEB- ...
随机推荐
- PAT trie
最近在上计算机应用编程,老师给了一个大小为900MB的含20000000行邮箱地址的文件. 然后再给出了1000条查询数据,让你用字典树建树然后查询是否出现过. 试了下普通的tire树,特意用二进制写 ...
- [Spring MVC]学习笔记--@RequestMapping支持的返回类型
下面针对官方文档列出的支持类型进行举例. (本篇例子存于github上, https://github.com/lemonbar/spring-mvc-requestmapping) 可以直接下载, ...
- The Best Hacking Tools
The Best Hacking Tools Hacking Tools : List of security tools specifically aimed toward security pro ...
- Introduction to Mathematical Thinking - Week 7
Q: Why did nineteenth century mathematicians devote time to the proof of self-evident results? Selec ...
- MVC4学习笔记之--身份认证过滤器
过滤器作为MVC模式中面向切面编程应用很广泛,例如身份验证,日志,异常,行为截取等.博客园里面的大神对应过滤器的介绍以及很多,MVC4中不同的过滤器也介绍得很清楚.FlyDragon 辉太 禁止吸烟 ...
- mesos cluster
http://spark.apache.org/docs/latest/running-on-mesos.html http://stackoverflow.com/questions/1993985 ...
- ASP-Command-SQL格式
conn.open constrSet c=Server.CreateObject("ADODB.Command")With cSet .ActiveConnection = co ...
- 【转】IBM PowerVM虚拟化技术笔记
1. 从CPU虚拟化的角度, 分区(partition)可以分成两大类:Micro-partition和Dedicated-procesor.前者可以将物理处理器以0.01的 粒度分配给微分区,分区创 ...
- setlocale()函数测试当前语言的两个程序
http://www.cnblogs.com/cnyao/archive/2010/05/06/1729220.html setlocale()函数是用来配置地域信息的,原本以为这个也是windows ...
- python——列表&字符串互相转换方法小结
字符串(str)转列表(list) 转换方法:str.split() str = 'zhu gao chao' print(str.split(' ')) # 用split进行转换 str——> ...