BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #29789同步于 2014/5/6
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

ServletContextListener监听器问题

AAAMWAAA
2014/5/6镜像同步4 回复
我自己编写了一个web项目在里面写了一个GossipListener的监听器,代码如下: package cc.openhome.listener; import java.util.Properties; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; import cc.openhome.model.information.GmailCarrier; @WebListener public class GossipListener implements ServletContextListener { private ServletContext context = null; public GossipListener() { } public void contextInitialized(ServletContextEvent sce) { try{ context=sce.getServletContext(); Properties props=new Properties(); props.load(context.getResourceAsStream("/WEB_INF/mail.properties")); GmailCarrier carrier=new GmailCarrier(props); //设为全局的共享变量 context.setAttribute("GmailCarrier", carrier); System.out.println("wwwwwwwwwwwwwwwwwwwwww"); }catch(Exception e) { } } public void contextDestroyed(ServletContextEvent sce) { // TODO Auto-generated method stub System.out.println("The Simple Web App. Has Been Removed"); this.context = null; } } 不知道为什么,每次启动Tomcat服务器,服务器的控制台并没有输出wwwwwwwwwwwwwwwwwwwwww这句话。 我自己修改了代码,在保存,控制台输出了The Simple Web App. Has Been Removed这句话, 我自己的这个web项目,其他的servlet访问这个共享变量,值是null。不知道为什么啊,求大神指点!
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
yanxuan机器人#1 · 2014/5/6
web.xml里配置这个Listener了嘛
AAAMWAAA机器人#2 · 2014/5/6
【 在 yanxuan 的大作中提到: 】 : web.xml里配置这个Listener了嘛 配置了啊。 不就是加上 <listener> <listener-class>cc.openhome.listener.GossipListener</listener-class> </listener>
gaoxinyu机器人#3 · 2014/5/7
把catch里面的异常栈信息打出来,也许是syso前面的代码出异常了
AAAMWAAA机器人#4 · 2014/5/7
【 在 gaoxinyu 的大作中提到: 】 : 把catch里面的异常栈信息打出来,也许是syso前面的代码出异常了 谢谢了,我已经解决了 是自己把路径写错了。都怪自己的catch有没有写输出信息!