InvocationTargetException异常的深入研究-servlet的setAttribute与getAttribute

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

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

// 登录处理源码
if ("student".equals(role)) {
    // 学生登录
    // 创建学生服务对象
    StudentService ss = new StudentServiceImpl();
    // 调用服务中的登录进程
    Student student = ss.login(uid, upwd);
    // 判断此学生是否存在
    if(student != null) {
        // 如果存在,则登录成功, 存储学生信息
        /* javax.servlet.http.HttpSession源码
         * public void setAttribute(String name, Object value);
        */
        // 第一种设置域属性方式:sid 在实体类中定义的是int类型,setAttribute中传入值则自动装箱成Integer
        // req.getSession().setAttribute("uid", student.getSid());
        // 第二种设置属性方式:为了验证,此处给指定字符串数据
        req.getSession().setAttribute("uid", "1");
        req.getSession().setAttribute("uname", student.getSname());
        // 页面跳转
        resp.sendRedirect(req.getContextPath() + "/index.jsp");
        return;
    } else {
        // 失败信息
        req.setAttribute("error", "用户名或密码错误,请重新登录!");
        // 请求转发
        req.getRequestDispatcher("login.jsp").forward(req, resp);
        return;
    }
}
<-- 登录成功后的跳转链接 -->
<div>
    <a href="student.action?operation=queryInfo&uid=${uid }" class="left-font03">查看个人信息</a>
</div>
// 获取详细信息的servlet代码
public void queryInfo(HttpServletRequest req, HttpServletResponse resp){
    StudentService ss = new StudentServiceImpl();
    // 第一种获取uid方式:从url中的参数中获取uid,为String类型数据
    //String uid = req.getParameter("uid");

    // 第二种获取uid方式:从登录时保存在session中属性获取,此属性为int类型,通过setAttribute方法自动装箱成Integer类型属性传递
    // int sid = (Integer) req.getSession().getAttribute("uid"); 

    // 第二种获取uid方式:从登录时保存在session中属性获取,此属性为String类型
    String uid = (String) req.getSession().getAttribute("uid");

    // 出现InvocationTargetException异常的源头就在此,由于使用getAttribute方法获取保存在对应域内的属性值默认属性为Object,
    // 此时强转类型需要使用与原类型匹配的类型,否则在下面语句转换数据为int类型时会出错
    int sid = Integer.parseInt(uid);
    Student student = ss.queryById(sid);
    if(cs.queryById(sid) != null ){
        req.setAttribute("student", student);
        try {

// 请求转发         req.getRequestDispatcher("student/information.jsp").forward(req, resp);
            return;
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

// 重写service方法,通过反射提取方法
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // 获取当前对象所属类的Class对象
    Class<?> c = this.getClass();
    // 接收想要被调用的方法的名字
    String name = req.getParameter("operation");
    try {
        // 通过class对象获取要调用的方法
        // 如果被调用方法出错,此处就会抓取异常信息,也就是InvocationTargetException异常的来源
        Method method = c.getMethod(name, HttpServletRequest.class, HttpServletResponse.class);
        // 通过反射调用该方法
        method.invoke(this, req, resp);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: setattribute和getattribute是JavaScript中的两个方法,用于设置和获取HTML元素的属性。 setattribute方法可以用来设置HTML元素的属性,例如: ``` document.getElementById("myElement").setAttribute("class", "myClass"); ``` 这会将ID为“myElement”的元素的class属性设置为“myClass”。 getattribute方法可以用来获取HTML元素的属性,例如: ``` var myClass = document.getElementById("myElement").getAttribute("class"); ``` 这会将ID为“myElement”的元素的class属性的值存储在变量myClass中。 ### 回答2: setAttribute和getAttribute都是Javascript中常用的方法,主要用于操作和获取HTML元素的属性。 setAttribute方法可以用来动态地改变HTML元素的属性值,例如: document.getElementById("myImg").setAttribute("src", "newImg.jpg"); 这段代码会将id为“myImg”的元素的src属性修改为“newImg.jpg”。setAttribute方法有两个参数,第一个参数是要设置的属性名,第二个参数是要设置的属性值。 getAttribute方法则用于获取HTML元素的属性值,例如: var title = document.getElementById("myTitle").getAttribute("title"); 这段代码会获取id为“myTitle”的元素的title属性值,并将其赋值给变量“title”。getAttribute方法有一个参数,即要获取的属性名。 需要注意的是,setAttribute和getAttribute只能操作和获取HTML元素的标准属性,不能操作和获取行内样式的属性。如果要操作和获取行内样式的属性,需要使用style属性。 另外,需要注意的是,在HTML5规范中,可以直接通过对象.属性的方式来设置和获取HTML元素的标准属性,例如: document.getElementById("myImg").src = "newImg.jpg"; var title = document.getElementById("myTitle").title; 但是,这种方式只适用于HTML元素的标准属性,不能操作和获取非标准属性或自定义属性。因此,在一些较老的浏览器中,仍然需要使用setAttribute和getAttribute方法来操作和获取HTML元素的属性。 ### 回答3: setAttribute和getAttribute是JavaScript中DOM(Document Object Model)的元素属性操作方法。setAttribute方法用于设置指定元素的属性值,而getAttribute方法用于获取指定元素的属性值。 setAttribute方法需要两个参数:属性名和属性值。例如,如果要将元素的class属性设置为“myclass”,则可以使用以下代码: element.setAttribute("class", "myclass"); 使用getAttribute方法来检索属性值。例如,如果想要获取元素的class属性的值,则可以使用以下代码: var classname = element.getAttribute("class"); 当使用setAttribute方法设置属性值时,它将覆盖任何现有的相同属性。因此,在设置属性值之前,最好先检查其是否已存在。 getAttribute方法返回属性值的字符串表示形式。如果属性不存在,则返回null。请注意,它不会返回数字对象或布尔值。 在JavaScript中,setAttribute和getAttribute方法不仅适用于HTML元素,也适用于XML文档中的元素。因此,在处理XML文档时,这些方法比较有用。 总之,setAttribute和getAttribute方法对于操作DOM元素的属性非常有用,可以帮助我们控制HTML和XML文档的表现和行为。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值