zoukankan      html  css  js  c++  java
  • InvocationTargetException异常的深入研究-servlet的setAttribute与getAttribute

    在某项目中,前端jsp传入的用户id数据通过session域传入后台servlet进行处理的过程中,无意间出现了InvocationTargetException异常
    

    前端部分代码如下:测试代码,非原项目代码

     1 // 登录处理源码
     2 if ("student".equals(role)) {
     3     // 学生登录
     4     // 创建学生服务对象
     5     StudentService ss = new StudentServiceImpl();
     6     // 调用服务中的登录进程
     7     Student student = ss.login(uid, upwd);
     8     // 判断此学生是否存在
     9     if(student != null) {
    10         // 如果存在,则登录成功, 存储学生信息
    11         /* javax.servlet.http.HttpSession源码
    12          * public void setAttribute(String name, Object value);
    13         */
    14         // 第一种设置域属性方式:sid 在实体类中定义的是int类型,setAttribute中传入值则自动装箱成Integer
    15         // req.getSession().setAttribute("uid", student.getSid());
    16         // 第二种设置属性方式:为了验证,此处给指定字符串数据
    17         req.getSession().setAttribute("uid", "1");
    18         req.getSession().setAttribute("uname", student.getSname());
    19         // 页面跳转
    20         resp.sendRedirect(req.getContextPath() + "/index.jsp");
    21         return;
    22     } else {
    23         // 失败信息
    24         req.setAttribute("error", "用户名或密码错误,请重新登录!");
    25         // 请求转发
    26         req.getRequestDispatcher("login.jsp").forward(req, resp);
    27         return;
    28     }
    29 }
    1 <-- 登录成功后的跳转链接 -->
    2 <div>
    3     <a href="student.action?operation=queryInfo&uid=${uid }" class="left-font03">查看个人信息</a>
    4 </div>
     1 // 获取详细信息的servlet代码
     2 public void queryInfo(HttpServletRequest req, HttpServletResponse resp){
     3     StudentService ss = new StudentServiceImpl();
     4     // 直接从url中的参数中获取uid,为String类型数据
     5     //String uid = req.getParameter("uid");
     6 
     7     // 第一种获取uid方式:从登录时保存在session中属性获取,此属性为int类型,通过setAttribute方法自动装箱成Integer类型属性传递
     8     // int sid = (Integer) req.getSession().getAttribute("uid"); 
     9 
    10     // 第二种获取uid方式:从登录时保存在session中属性获取,此属性为String类型
    11     String uid = req.getSession().getAttribute("uid");
    12 
    13     // 出现InvocationTargetException异常的源头就在此,由于使用getAttribute方法获取保存在对应域内的属性值默认属性为Object,
    14     // 此时强转类型需要使用与原类型匹配的类型,否则在下面语句转换数据为int类型时会出错
    15     int sid = Integer.parseInt(uid);
    16     Student student = ss.queryById(sid);
    17     if(cs.queryById(sid) != null ){
    18         req.setAttribute("student", student);
    19         try {
    20 
    21 // 请求转发         req.getRequestDispatcher("student/information.jsp").forward(req, resp);
    22             return;
    23         } catch (ServletException e) {
    24             e.printStackTrace();
    25         } catch (IOException e) {
    26             e.printStackTrace();
    27         }
    28     }
    29 }
    30 
    31 // 重写service方法,通过反射提取方法
    32 @Override
    33 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    34     // 获取当前对象所属类的Class对象
    35     Class<?> c = this.getClass();
    36     // 接收想要被调用的方法的名字
    37     String name = req.getParameter("operation");
    38     try {
    39         // 通过class对象获取要调用的方法
    40         // 如果被调用方法出错,此处就会抓取异常信息,也就是InvocationTargetException异常的来源
    41         Method method = c.getMethod(name, HttpServletRequest.class, HttpServletResponse.class);
    42         // 通过反射调用该方法
    43         method.invoke(this, req, resp);
    44     } catch (Exception e) {
    45         e.printStackTrace();
    46     }
    47 }

    下面这段代码需要特别注意:

      如果在保存属性时,属性内容,setAttribute中传递的是数字,非字符串,则在接收属性时,不再需要使用 Integer.parseInt(uid) 对其进行转换,直接使用 (Integer) req.getSession().getAttribute("uid");进行强转即可得到对应的数字。此时如果使用String类型变量来接收该属性,则会存在数据类型不匹配的隐患,然后再使用Integer.parseInt(uid)进行强制类型转换,就会出现异常,从而导致通过反射调用该方法是出现 InvocationTargetException 异常。

      如果在保存属性时,属性内容,setAttribute中传递的是由数字用双引号转化的字符串,则需要用一个String类型的变量接收获取的属性,String uid = req.getSession().getAttribute("uid");然后再使用Integer.parseInt(uid)对其进行强转后才能向实体对象中保存或做其他用途。

    1 // 第一种获取uid方式:从登录时保存在session中属性获取,此属性为int类型,通过setAttribute方法自动装箱成Integer类型属性传递
    2 // int sid = (Integer) req.getSession().getAttribute("uid"); 
    3 // 第二种获取uid方式:从登录时保存在session中属性获取,此属性为String类型
    4 String uid = req.getSession().getAttribute("uid");
    5 // 出现InvocationTargetException异常的源头就在此,由于使用getAttribute方法获取保存在对应域内的属性值默认属性为Object,
    6 // 此时强转类型需要使用与原类型匹配的类型,否则在下面语句转换数据为int类型时会出错


    个人总结分享,共同学习。

    纸上得来终觉浅 绝知此事要躬行
  • 相关阅读:
    TOMCAT源码分析(转)
    CSS从零开始(1)--CSS基础语法
    java生成验证码
    [转]java中作用域public private protected 以及不写的区别
    Java Socket NIO详解(转)
    mysql数据库单表只有一个主键自增id字段,ibatis实现id自增
    手机验证码执行流程
    javaweb之Filter过滤器详解
    HttpSessionListener的用法
    SpringContextHolder 静态持有SpringContext的引用
  • 原文地址:https://www.cnblogs.com/lslk89/p/6898435.html
Copyright © 2011-2022 走看看