编写感知Session绑定状态的监听器是出现java.lang.NoSuchMethodException异常
来源:互联网 发布:欧美番号推荐知乎 编辑:程序博客网 时间:2024/05/19 20:43
在学习感知Session绑定的事件监听器时,编写的事件监听器实现了HttpSessionBindingListener接口,监听对象在Session域中的绑定和解绑事件,下面是我的代码:
实现HttpSessionBindingListener接口的JavaBean类:
package Listener;import javax.servlet.annotation.WebListener;import javax.servlet.http.HttpSessionBindingEvent;import javax.servlet.http.HttpSessionBindingListener;/* * 实现了HttpSessionBindingListener接口的JavaBean对象可以感知自己被绑定到Session中和从Session删除的事件 * 当对象被绑定到HttpSession对象中时,web服务器调用该对象的void valueBound(HttpSessionBindingEvent event)方法 * 当对象从HttpSession对象中解除绑定时,web服务器调用该对象的void valueUnbound(HttpSessionBindingEvent event)方法 *//** * Application Lifecycle Listener implementation class JavaBeanDemo1 * */@WebListenerpublic class JavaBeanDemo1 implements HttpSessionBindingListener {private String name;public void valueBound(HttpSessionBindingEvent event) { System.out.println(name+"被加到session中"); }public void valueUnbound(HttpSessionBindingEvent event) { System.out.println(name+"被session踢出来"); }public String getName(){return name;}public void setName(String name){this.name = name;}public JavaBeanDemo1(String name) { this.name = name; }}
运行的Jsp:
<%@ page language="java" pageEncoding="UTF-8"%><%@page import="Listener.JavaBeanDemo1" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title></title> </head> <body> <% //将Javabean对象绑定到Session中 session.setAttribute("bean",new JavaBeanDemo1("yanglin")); //从Session中删除javaBean对象 session.removeAttribute("bean"); %> </body></html>但是运行时发生报错
可以发现是由于在jsp中有new JavaBeanDemo1("yanglin"),说明JavaBeanDemo1中必须要有无参的构造函数,而由于JavaBeanDemo1中构造函数有参数,所以
需要把默认的无参构造函数显示的写出了,添加public JavaBeanDemo1(){}即可,这是一个小问题,但是不能忽视,所以在以后写代码时,尽量先把无参的构造函数
写出了,以免编译错误。
0 0
- 编写感知Session绑定状态的监听器是出现java.lang.NoSuchMethodException异常
- java.lang.NoSuchMethodException异常
- struts2的java.lang.NoSuchMethodException异常处理
- struts2的java.lang.NoSuchMethodException异常处理
- struts2的java.lang.NoSuchMethodException异常处理
- struts2的java.lang.NoSuchMethodException异常处理 \
- struts2的java.lang.NoSuchMethodException异常处理
- struts2的java.lang.NoSuchMethodException异常处理
- struts2的java.lang.NoSuchMethodException异常处理
- struts2的java.lang.NoSuchMethodException异常处理
- struts的java.lang.NoSuchMethodException异常处理
- struts2的java.lang.NoSuchMethodException异常处理
- struts2的java.lang.NoSuchMethodException异常处理
- struts2的java.lang.NoSuchMethodException异常处理
- struts2的java.lang.NoSuchMethodException异常处理
- Struts2 spring整合 Action 出现java.lang.NoSuchMethodException异常
- struts2:java.lang.NoSuchMethodException异常
- java.lang.NoSuchMethodException 异常总结
- PAT甲 1032. Sharing (25)
- 搭建L2TP over IPSec VPN
- 模板方法模式
- IOC理解
- OBJ文件解析之OBJ的整体结构
- 编写感知Session绑定状态的监听器是出现java.lang.NoSuchMethodException异常
- *浙大PAT甲级 1111
- How web servers work?
- PAT甲 1041. Be Unique (20)
- Python--tile函数简单介绍
- 数据结构知识点复习——线性表
- InetAddress类的一些常用方法-tcp/ip协议知识体系
- java今后方向笔记
- PAT甲 1048. Find Coins (25)