用简单的反射优化代码(动态web项目)

在动态web项目中,没有使用框架时,只是简单的jsp访问servlet实现增删改查,

无论是哪个方法都需要经过Servlet中的doGet()方法或doPost()方法,我们可以在链接中附带参数进行区分,

但是这样doGet()方法或doPost()方法中的代码就会非常长,不方便查看和管理。

有两种解决办法:

1、定义多个Servlet,显然这样也比较繁琐。

2、利用简单的反射对代码进行优化

我们还是用在链接中附带参数的方法,向后台传递一个请求参数oper(页面中使用了EL表达式,JSTL标签,JQuery和AJAX)

 1 <table>
 2     <c:choose>
 3         <c:when test="${not empty userList }">
 4             <tr><th>id</th><th>姓名</th><th>密码</th><th>邮箱</th><th>操作</th></tr>
 5             <c:forEach var="user" items="${userList }">
 6             <tr><td>${user.userid }</td><td>${user.username }</td>
 7                 <td>${user.password }</td><td>${user.useremail }</td>
 8                 <td><a href='<c:url value="/LoginServlet?oper=detail&userId=${user.userid }"></c:url>'>详情</a>
 9                     <a href='<c:url value="/LoginServlet?oper=edit&userId=${user.userid }"></c:url>'>修改</a>
10                     <a onclick="delConfirm(${user.userid })">删除</a></td></tr>
11             </c:forEach>
12         </c:when>
13         <c:otherwise></c:otherwise>
14     </c:choose>
15 </table>
 1 <script type="text/javascript">
 2 function delConfirm(userid){
 3     var b=confirm("确定删除吗?");
 4     if(b){
 5         $(function(){
 6             $.post("${pageContext.request.contextPath}/LoginServlet?oper=delete",
 7             {
 8                 userid:userid
 9             },
10             function(data,status){
11                 if(data==1){
12                     alert("删除成功!");
13                 }else{
14                     alert("删除失败!");
15                 }
16                 window.location.href="${pageContext.request.contextPath}/LoginServlet?oper=login";
17             });
18         });
19     }
20 }
21 </script>

在后台获取请求参数oper,将其映射成函数,然后就可以在相应的函数中做处理了。

 1 protected void doGet(HttpServletRequest request, HttpServletResponse response)
 2         throws ServletException, IOException {
 3     request.setCharacterEncoding("utf-8");
 4     // 获取请求参数oper
 5     String methodName = request.getParameter("oper");
 6     // 获取当前类的Class对象
 7     Class cla = this.getClass();
 8     try {
 9         // 通过方法名获取到方法的对象
10         // getDeclaredMethod需要两个参数,方法名和参数名,因为在java需要通过方法名和参数列表来确定一个方法
11         Method method = cla.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
12         // 设置方法的访问权限
13         method.setAccessible(true);
14         // 调用方法
15         // invoke用于调用一个方法,第一个参数时要调用方法的对象,剩下是调用方法需要的参数
16         method.invoke(this, request, response);
17     } catch (Exception e) {
18         e.printStackTrace();
19     }
20 }
21 
22 protected void doPost(HttpServletRequest request, HttpServletResponse response)
23         throws ServletException, IOException {
24     doGet(request, response);
25 }
26 
27 public void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
28     //对请求和响应做处理
29 }
30 public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
31     //对请求和响应做处理
32 }
33 public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
34     //对请求和响应做处理
35 }
36 public void delete(HttpServletRequest request, HttpServletResponse response)
37         throws ServletException, IOException, SQLException {
38     //对请求和响应做处理
39 }
40 //登录
41 public void login(HttpServletRequest request, HttpServletResponse response)
42         throws ServletException, IOException, SQLException {
43     //对请求和响应做处理
44 }
原文地址:https://www.cnblogs.com/lixiang1993/p/7388731.html