package com.itheima.birthday;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import javax.mail.MessagingException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.itheima.mail.MailUtils;
public class BirthdayListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent sce) {
//当web应用启动 开启任务调动---功能在用户的生日当前发送邮件
//开启一个定时器
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// 为当前的生日的用户发邮件
//1、获得今天过生日的人
//获得今天的日期
SimpleDateFormat format = new SimpleDateFormat("MM-dd");
String currentDate = format.format(new Date());
//根据当前时间从数据查询今天过生日的人
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from customer where birthday like ?";
List<Customer> customerList = null;
try {
customerList = runner.query(sql, new BeanListHandler<Customer>(Customer.class) ,"%"+currentDate+"%");
} catch (SQLException e) {
e.printStackTrace();
} //08-18
//2、发邮件
if(customerList!=null&&customerList.size()>0){
for(Customer c : customerList){
String emailMsg = "亲爱的:"+c.getRealname()+",生日快乐!";
try {
MailUtils.sendMail(c.getEmail(), "生日祝福", emailMsg);
System.out.println(c.getRealname()+"邮件发送完毕");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
}
}, new Date(), 1000*10);
//实际开发中起始时间是一个固定的时间
//实际开发中间隔时间是1天
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
23-Listener-源代码.rar
共48个文件
class:16个
java:14个
xml:5个
需积分: 5 0 下载量 165 浏览量
2023-08-20
08:09:54
上传
评论
收藏 1.49MB RAR 举报
温馨提示
案例-使用监听器完成定时生日祝福、,一、监听器Listener 二、邮箱服务器。 1.什么是监听器? 2.监听器有哪些? 3.监听三大域对象的创建与销毁的监听器 4.监听三大域对象的属性变化的 5.与session中的绑定的对象相关的监听器(对象感知监听器) 1.邮箱服务器的基本概念 2.邮箱的发送过程 3.邮箱服务器的安装 4.邮箱客户端的安装 5.邮件发送代码。 5)ServletContextListener监听器的主要作用 a、初始化的工作:初始化对象 初始化数据 ---- 加载数据库驱动 连接池的初始 化 b、加载一些初始化的配置文件 --- spring的配置文件 c、任务调度----定时器----Timer/TimerTask
资源推荐
资源详情
资源评论
收起资源包目录
23_Listener-源代码.rar (48个子文件)
23_Listener-源代码
.classpath 841B
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.jdt.core.prefs 364B
.jsdtscope 567B
org.eclipse.wst.common.component 467B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.wst.jsdt.ui.superType.container 49B
src
c3p0-config.xml 322B
com
itheima
attribute
MyServletContextAttributeListener.java 902B
TestMyServletContextAttributeListener.java 914B
birthday
BirthdayListener.java 2KB
DataSourceUtils.java 2KB
Customer.java 966B
create
MyHttpSessionListener.java 445B
MyServletContextListener.java 2KB
domian
TestCustomerActiveServlet2.java 837B
TestCustomerActiveServlet.java 927B
Customer.java 780B
Person.java 749B
TestPersonBindingServlet.java 940B
mail
SendMailTest.java 344B
MailUtils.java 2KB
build
classes
c3p0-config.xml 322B
com
itheima
attribute
TestMyServletContextAttributeListener.class 1KB
MyServletContextAttributeListener.class 1KB
birthday
DataSourceUtils.class 2KB
BirthdayListener.class 969B
Customer.class 1KB
BirthdayListener$1.class 3KB
create
MyServletContextListener.class 807B
MyHttpSessionListener.class 1KB
domian
Person.class 1KB
TestCustomerActiveServlet.class 1KB
Customer.class 1KB
TestPersonBindingServlet.class 1KB
TestCustomerActiveServlet2.class 1KB
mail
MailUtils$1.class 716B
MailUtils.class 2KB
SendMailTest.class 713B
WebContent
WEB-INF
lib
c3p0-0.9.1.2.jar 596KB
commons-dbutils-1.4.jar 48KB
mysql-connector-java-5.0.4-bin.jar 484KB
mail.jar 483KB
web.xml 3KB
session.jsp 374B
META-INF
context.xml 465B
MANIFEST.MF 39B
.project 1KB
共 48 条
- 1
资源评论
常驻客栈
- 粉丝: 1w+
- 资源: 1359
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功