在动态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项目)的更多相关文章

  1. 简单的文件上传的下载(动态web项目)

    1.在页面中定义一个form表单,如下: <!-- 文件上传 --> <form action="${pageContext.request.contextPath}/Fi ...

  2. springmvc 项目完整示例02 项目创建-eclipse创建动态web项目 配置文件 junit单元测试

    包结构 所需要的jar包直接拷贝到lib目录下 然后选定 build path 之后开始写项目代码 配置文件 ApplicationContext.xml <?xml version=" ...

  3. 创建一个动态Web项目:

    开始你的Eclipse,然后进入“文件”>“新建”>“动态Web项目,然后输入项目名称为HelloWorldStruts2和设置其他的选项,在下面的屏幕: 选择在屏幕上的所有默认选项,最后 ...

  4. 搭建Dynamic Web Project(动态web项目)的springmvc工程2

    本文转载自:http://blog.csdn.net/typa01_kk/article/details/45905129 此篇为“创建Dynamic Web Projec工程,”搭建Dynamic ...

  5. 搭建Dynamic Web Project(动态web项目)的springmvc工程1

    本文转载自:http://blog.csdn.net/typa01_kk/article/details/45902955 此篇创建Dynamic Web Projec工程(动态web项目),下一篇, ...

  6. IDEA创建动态Web项目

    1.IDEA创建动态Web项目 1.1.使用IDEA创建动态Web项目,选择Java Enterprise,记得选择服务器,我这里使用的时tomcat 1.2记得勾选Web Application,其 ...

  7. Velocity笔记--使用Velocity获取动态Web项目名的问题

    以前使用jsp开发的时候,可以通过request很轻松的获取到根项目名,现在换到使用velocity渲染视图,因为已经不依赖servlet,request等一些类的环境,而Web项目的根项目名又不是写 ...

  8. Eclipse 中构建 Maven 项目的完整过程 - 动态 Web 项目

    进行以下步骤的前提是你已经安装好本地maven库和eclipse中的maven插件了(有的eclipse中已经集成了maven插件) 一.Maven项目的新建 1.鼠标右键---->New--- ...

  9. 用eclipse创建动态web项目手动生成web.xml方法

    建一个web项目,后来在用到web.xml文件时,才发现项目创建时没有自动创建web.xml文件. 在创建的项目上单击右键,然后单击java EE Tools下的用红线圈住的地方,然后查看你的WEB- ...

随机推荐

  1. SharePoint server 2016中文版导出list template,在另外一个环境不能显示

    SharePoint server 2016中文版导出list template,在另外一个环境不能显示,解决方案: $web = Get-SPWeb <url of web> $web. ...

  2. 获取字符串已utf-8表示的字节数

    private static int utf8Length(String string) { /** Returns the number of bytes required to write thi ...

  3. 关于git 提交到分支

    想必大家对于github并不陌生,但是有时候我们提交到github上的页面,想将静态的页面展示给别人看,所以这个时候,需要创建一个gh-pages的分支,然后利用 https://you github ...

  4. C语言结构体数组内带字符数组初始化和赋值

    1.首先定义结构体数组: typedef struct BleAndTspRmtCmd{ char terminal[3]; char note[3]; char rmtCmd[10]; char c ...

  5. SQLServer中exists和except用法

    一.exists 1.1 说明 EXISTS(包括 NOT EXISTS)子句的返回值是一个BOOL值.EXISTS内部有一个子查询语句(SELECT ... FROM...),我将其称为EXIST的 ...

  6. python函数的学习笔记

    这篇文章是我关于学习python函数的一些总结 一.随着函数的引入,这里首先要说的就是全局变量和局部变量了. 什么是全局变量.什么是局部变量: 全局变量就是全局都能调用的变量,一般都在文件的开头,顶头 ...

  7. python模块学习(三)

    logging模块 简单应用 import logging  logging.debug('debug message')  logging.info('info message')  logging ...

  8. Android系统移植与调试之------->build.prop文件详细赏析

    小知识:什么是build.prop?   /system/build.prop 是一个属性文件,在Android系统中.prop文件很重要,记录了系统的设置和改变,类似於/etc中的文件.这个文件是如 ...

  9. 运行scrapy保存图片,报错ValueError: Missing scheme in request url: h

    查阅相关资料,了解到使用ImagesPipeline传入的url地址必须是一个list,而我写的是一个字符串,所以报错,所以需要修改一下传入的url格式就行了 def parse_detail(sel ...

  10. Log level with log4j and Spark

    Log Level Usages OFF This is the most specific, which allows no logging at all FATAL This is the mos ...