1、Listener监听器的概念:
2、Listener监听器的作用
3、编写一个Listener监听器:
public class ServletContext implements ServletContextListener {
//创建ServletContext时自动执行
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("ServletContext 创建了");
}
//销毁ServletContext时自动执行
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("ServletContext 销毁了");
}
}
Demo01.ServletContext
4、监听器的分类:
public class ServletContextAttribute implements ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("ServletContext 属性添加了"+servletContextAttributeEvent.getName()+":"+servletContextAttributeEvent.getValue());
}
@Override
public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("ServletContext 属性移除了"+servletContextAttributeEvent.getName()+":"+servletContextAttributeEvent.getValue());
}
@Override
public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("ServletContext 属性替换了"+servletContextAttributeEvent.getName()+":"+servletContextAttributeEvent.getValue());
}
}
1、定时器概念:
2、定时器的创建:
3、常用方法:
public class Timer1 {
public static void main(String[] args){
//获取当前时间
Date date = new Date();
System.out.println("当前时间为"+date.toLocaleString());
System.out.println("延迟4秒后,执行任务");
//获取Timer
Timer timer = new Timer();
//延迟4秒,每隔一秒打印一下当前时间
timer.schedule(new TimerTask() {
@Override
public void run() {
Date date = new Date();
System.out.println(date.toLocaleString());
}
},4000,1000);
}
}
1、获取日历对象:Calendar.getInstance( )
2、常用方法:
public class Calendar1 {
public static void main(String[] args){
//获取日历对象
Calendar calendar = Calendar.getInstance();
//设置为日历对象时间为当前时间
calendar.setTime(new Date());
//设置年份为2020年
calendar.set(Calendar.YEAR,2020);
System.out.println(calendar.getTime().toLocaleString());
}
}
1、案例需求:
2、邮件服务器:
3、电子邮箱:
4、协议:规定数据的格式
5、案例实现:
package Demo04.listener;
import Demo04.bean.User;
import Demo04.service.MailService;
import Demo04.utils.DateUtils;
import Demo04.utils.MailUtils;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class MailListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
//创建一个定时器
Timer timer = new Timer();
//定时发送邮件,(延迟时间:从现在到第二天0时0分0秒所差的时间,间隔时间:一天)
timer.schedule(new TimerTask() {
@Override
public void run() {
//调用方法查询生日在当天的用户
MailService mailService = new MailService();
try {
//获取日历对象
Calendar calendar = Calendar.getInstance();
//设置为日历对象时间为当前时间
calendar.setTime(new Date());
//获取当前的月份和日期
int month=calendar.get(Calendar.MONTH)+1;
int day=calendar.get(Calendar.DAY_OF_MONTH);
String birthday = "%-0"+month+"-0"+day;
//System.out.println(birthday);
List users = mailService.queryUsers(birthday);
//System.out.println(users);
if(users != null){
for(User user:users){
String email = user.getEmail();
String emailMsg = "亲爱的"+user.getUsername()+",生日快乐?!";
MailUtils.sendMail(email,emailMsg);
System.out.println(user.getUsername()+":邮件发送成功");
}
}
} catch (SQLException e) {
e.printStackTrace();
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
},DateUtils.getDelayTime(),DateUtils.getOneDay());
// },4000,1000);
System.out.println();
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
package Demo04.dao;
import Demo04.bean.User;
import Demo04.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class MailDao {
public List queryUsers(String birthday) throws SQLException {
//创建QueryRunner对象
QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
//编写SQL语句
String sql = "select * from user where birthday like ?”;
//执行SQL语句
List users = queryRunner.query(sql, new BeanListHandler(User.class),birthday);
return users;
}
}
package Demo04.service;
import Demo04.bean.User;
import Demo04.dao.MailDao;
import java.sql.SQLException;
import java.util.List;
public class MailService {
public List queryUsers(String birthday) throws SQLException {
MailDao mailDao = new MailDao();
return mailDao.queryUsers(birthday);
}
}
package Demo04.utils;
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
/**
* 获得当前时间距离给定天数零点的毫秒时间
* @param amount
* @return
*/
public static Long getDelayTime(int amount){
//1 设置当前时间
Calendar calendar = Calendar.getInstance();
Date newDate = new Date();
calendar.setTime(newDate);
//2 将时分秒设置成0
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
//3 设置指定天数
calendar.add(Calendar.DATE, amount);
//4 计算当前时间距离设置日期零点的延迟时间
return calendar.getTimeInMillis() - newDate.getTime();
}
/**
* 当前时间具体明天零点的毫秒时间
* @return
*/
public static Long getDelayTime(){
return getDelayTime(1);
}
/**
* 获得一天的毫秒值
* @return
*/
public static Long getOneDay(){
return 24 * 60 * 60 * 1000L;
}
/**
* 获得几月(两位)
* @return
*/
public static String getCurrentMonth(){
//1 设置当前时间
Calendar calendar = Calendar.getInstance();
Date newDate = new Date();
calendar.setTime(newDate);
int m = calendar.get(Calendar.MONTH) + 1;
if(m < 10){
return "0" + m;
}
return "" + m;
}
/**
* 获得几号(两位)
* @return
*/
public static String getCurrentDay(){
//1 设置当前时间
Calendar calendar = Calendar.getInstance();
Date newDate = new Date();
calendar.setTime(newDate);
int d = calendar.get(Calendar.DATE);
if(d < 10){
return "0" + d;
}
return "" + d;
}
}
package Demo04.utils;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class MailUtils {
public static void sendMail(String email, String emailMsg)
throws AddressException, MessagingException {
// 1.创建一个程序与邮件服务器会话对象 Session
Properties props = new Properties();
//设置发送的协议
props.setProperty("mail.transport.protocol", "SMTP");
//设置发送邮件的服务器
props.setProperty("mail.host", "localhost");
props.setProperty("mail.smtp.auth", "true");// 指定验证为true
// 创建验证器
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
//设置发送人的帐号和密码
return new PasswordAuthentication("[email protected]", "123");
}
};
Session session = Session.getInstance(props, auth);
// 2.创建一个Message,它相当于是邮件内容
Message message = new MimeMessage(session);
//设置发送者
message.setFrom(new InternetAddress("[email protected]"));
//设置发送方式与接收者
message.setRecipient(RecipientType.TO, new InternetAddress(email));
//设置邮件主题
message.setSubject("生日祝福");
//设置邮件内容
message.setContent(emailMsg, "text/html;charset=utf-8");
// 3.创建 Transport用于将邮件发送
Transport.send(message);
}
}