• 用简单的反射优化代码(动态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 }
  • 相关阅读:
    《大数据之路:阿里巴巴大数据实践》——7-章 数据挖掘
    《如何做到毫秒级从百亿大表任意维度筛选数据?》
    《大数据之路:阿里巴巴大数据实践》——6-章 数据服务
    《【原创】推荐系统
    给机器学习面试者的十项建议 | 面试官角度
    干货 | NLP算法岗大厂面试经验与路线图分享
    目标检测任务中的训练宝典 |实用技巧
    食物图片变菜谱:这篇CVPR论文让人人都可以学习新料理
    一文彻底搞懂BP算法:原理推导+数据演示+项目实战(下篇)
    CVPR 2019细粒度图像分类竞赛中国团队DeepBlueAI获冠军 | 技术干货分享
  • 原文地址:https://www.cnblogs.com/lixiang1993/p/7388731.html
Copyright © 2020-2023  润新知