第1章 Filter拦截方式
1.1 include
当设置过滤器的拦截方式为include的时候,只有当使用include方式转发的请求才能被拦截。文章源自JAVA秀-https://www.javaxiu.com/825.html
1.1.1 include拦截方式代码演示
1.创建IncludeServlet,使用include转发的方式转发到index.jsp去。文章源自JAVA秀-https://www.javaxiu.com/825.html
IncludeServlet代码如下:文章源自JAVA秀-https://www.javaxiu.com/825.html
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
name = "IncludeServlet",urlPatterns = "/IncludeServlet")
(public class IncludeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("include方式转发到index.jsp页面去");
request.getRequestDispatcher("index.jsp").include(request,response);
}
}
2.创建MethodFilter,配置MethodFilter的拦截方式为include,拦截路径为/*文章源自JAVA秀-https://www.javaxiu.com/825.html
MethodFilter代码如下:文章源自JAVA秀-https://www.javaxiu.com/825.html
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
filterName = "MethodFilter",dispatcherTypes = DispatcherType.INCLUDE,urlPatterns = "/*")
(public class MethodFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("+++++++++MethodFilter过滤器执行了++++++++++");
chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException {
}
}
3.浏览器地址栏输入http://localhost:9090/IncludeServlet,文章源自JAVA秀-https://www.javaxiu.com/825.html
查看控制台,结果如下:文章源自JAVA秀-https://www.javaxiu.com/825.html
文章源自JAVA秀-https://www.javaxiu.com/825.html
1.2 error
当设置过滤器的拦截方式为error的时候,只有当发生异常的时候请求才能被拦截文章源自JAVA秀-https://www.javaxiu.com/825.html
过滤器:文章源自JAVA秀-https://www.javaxiu.com/825.html
package com.itheima.myfilter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
filterName = "ErrorFilter",urlPatterns = "/*",dispatcherTypes = DispatcherType.ERROR)
(public class ErrorFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("拦截异常!!!");
chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException {
}
}
发生异常的jsp文章源自JAVA秀-https://www.javaxiu.com/825.html
<%page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<% int i = 1/0; %>出现异常!!!
</body>
</html>
xml配置:文章源自JAVA秀-https://www.javaxiu.com/825.html
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<error-page>
<!--错误响应码-->
<error-code>500</error-code>
<!--发生错误之后跳转的页面-->
<location>/500.jsp</location>
</error-page>
</web-app>
第2章 JavaMail
在我们开发中,我们很多时候都需要涉及发送邮件的操作,比如:用户注册成功邮件激活账号,会员生日邮件提醒等等。这样的邮件操作就需要学习JavaMail来完成我们的java发送邮件的操作。文章源自JAVA秀-https://www.javaxiu.com/825.html
JavaMail是Sun公司提供的处理电子邮件的一套编程接口。使用JavaMail我们可以使用java代码完成邮件的接收和发送。文章源自JAVA秀-https://www.javaxiu.com/825.html
2.2 JavaMail应用场景
2.2.1 会员生日时发送邮件
例如qq每当到qq用户的生日的时候,qq会发送一封生日祝福邮件给qq用户。文章源自JAVA秀-https://www.javaxiu.com/825.html
2.2.2 会员注册成功,发送邮件,会员激活后,会员才能登陆。
我们现在上网很多时候需要注册一个用户,往往在我们注册后,网站会给我们发送一封激活邮件,需要我们去激活才能进行登录。文章源自JAVA秀-https://www.javaxiu.com/825.html
2.3 邮件使用的协议
网易有网易的邮件服务,qq有qq的邮件服务,传智播客有传智播客的邮件服务...那么我们我们播客的邮箱账号可以给网易的邮箱发送邮件,也可以给qq邮箱发送邮件。也就是说网页和qq邮箱服务能够解析我们传智发送的邮件,所以网络中发送邮件应该有与http协议一样概念的邮件协议。文章源自JAVA秀-https://www.javaxiu.com/825.html
2.3.1 发邮件协议
发邮件协议使用的是SMTP,协议的端口号是25。文章源自JAVA秀-https://www.javaxiu.com/825.html
SMTP的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议。它是一组用于从源地址到目的地址传输邮件的规范,通过它来控制邮件的中转方式。SMTP 协议属于 TCP/IP 协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地。文章源自JAVA秀-https://www.javaxiu.com/825.html
2.3.2 收邮件协议
收邮件协议有2中,一种是POP3,还有一种是IMAP。协议的端口号为110。文章源自JAVA秀-https://www.javaxiu.com/825.html
POP3是Post Office Protocol 3的简称,即邮局协议的第3个版本,它规定怎样将个人计算机连接到Internet的邮件服务器和下载电子邮件的电子协议。它是因特网电子邮件的第一个离线协议标准,POP3允许用户从服务器上把邮件存储到本地主机(即自己的计算机)上,同时删除保存在邮件服务器上的邮件。文章源自JAVA秀-https://www.javaxiu.com/825.html
IMAP全称是Internet Mail Access Protocol,即交互式邮件存取协议,它是跟POP3类似邮件访问标准协议之一。不同的是,开启了IMAP后,您在电子邮件客户端收取的邮件仍然保留在服务器上,同时在客户端上的操作都会反馈到服务器上,如:删除邮件,标记已读等,服务器上的邮件也会做相应的动作。所以无论从浏览器登录邮箱或者客户端软件登录邮箱,看到的邮件以及状态都是一致的。文章源自JAVA秀-https://www.javaxiu.com/825.html
2.4 邮件服务器安装
1.找到易邮邮件服务器安装包,双击安装文章源自JAVA秀-https://www.javaxiu.com/825.html
2.选择邮件服务器的安装位置文章源自JAVA秀-https://www.javaxiu.com/825.html
3.启动邮件服务器文章源自JAVA秀-https://www.javaxiu.com/825.html
4.出现如下页面,继续点击确定文章源自JAVA秀-https://www.javaxiu.com/825.html
5.设置邮箱服务器文章源自JAVA秀-https://www.javaxiu.com/825.html
6.设置服务器的域名文章源自JAVA秀-https://www.javaxiu.com/825.html
7.创建2个账号文章源自JAVA秀-https://www.javaxiu.com/825.html
8.创建账号zhangsan ,密码为123 和账号lisi,密码123文章源自JAVA秀-https://www.javaxiu.com/825.html
2.5 Foxmail安装
1.双击foxmail邮件客户端安装软件文章源自JAVA秀-https://www.javaxiu.com/825.html
2.然后一直点击下一步,直到安装成功文章源自JAVA秀-https://www.javaxiu.com/825.html
3.运行foxmail邮箱客户端文章源自JAVA秀-https://www.javaxiu.com/825.html
4.设置登录foxmail的账号名和密码文章源自JAVA秀-https://www.javaxiu.com/825.html
5设置登录账号的邮箱服务器的位置文章源自JAVA秀-https://www.javaxiu.com/825.html
6.同样的设置使用lisi的账号登录foxmail文章源自JAVA秀-https://www.javaxiu.com/825.html
7.然后使用zhangsan的账号给lisi的账号发送邮件,如果lisi接受成功,安装完成文章源自JAVA秀-https://www.javaxiu.com/825.html
2.6 JavaMail使用
2.6.1 常用的API
Session.getInstance(Properties props, Authenticator authenticator); 创建java到邮件服务器间的会话对象Session Properties以key 和value的形式保存邮件服务器的设置 例如:设置服务器的地址。 props.put("mail.smtp.host", "127.0.0.1"); Properties props = new Properties();
Message message = new MimeMessage(session); 创建邮件对象。session是java和服务器的会话对象session
message.setFrom(new InternetAddress("admin@abc.com")); 这只这封邮件的发件人。InternetAddress发件人地址对象。
message.setRecipient(RecipientType.TO, new InternetAddress(toEmail)); 参数一:设置邮件的接受方式。 RecipientType.TO:为正常接受邮件 RecipientType.CC:表示抄送 RecipientType.BCC:表示秘密抄送 参数二:设置邮件的收件人。
message.setSubject("通知"); 设置邮件的主题。
message.setContent(emailMsg, "text/html;charset=UTF-8"); 参数一:设置邮件的内容 参数二:设置解析邮件的方式为html,并且以utf-8的编码解析。
Transport.send(message); 发送邮件。参数就是需要发送的邮件对象Message
2.6.2 JavaMail发送邮件步骤
- 1.登录SMTP服务器
- 1.1 创建Properties对象,以指定主机(mail.smtp.host=localhost)和是否验证 (mail.smtp.auth=true)。
- 1.2 创建Authenticator抽象类,重写方法getPasswordAuthentication(),返回它的子类:PasswordAuthentication(),指定用户名和密码进行加密。
- 1.3 创建Session对象,传入Properties和Authenticator的参数
- 2.创建邮件,提供(发件人,收件人,主题,内容)
- 2.1 创建MimeMessage对象
- 2.2 设置发件人,与登录的用户名一致,需要使用到InternetAddress类型
- 2.3 设置收件人,指定收件人的类型:RecipientType.TO
- 2.4 设置正文和主题,需要指定内容类型和编码
- 3.发送邮件,Transport.send()发送邮件
- 4.封装邮件发送工具类MailUtil
2.6.3 JavaMail发送邮件工具类代码
public class MailUtils {
//发送邮件
//mail:收件人 mailMsg:邮件内容 subject:邮件标题
public static void sendMail(String mail,String mailMsg) throws AddressException, MessagingException{
Properties pro = new Properties();
pro.setProperty("mail.transport.protocol", "SMTP");//设置邮箱服务器协议
pro.setProperty("mail.host", "localhost");//设置发邮件的邮箱服务器地址
pro.setProperty("mail.smtp.auth", "true");// 验证发件箱的账户名和密码
//创建验证器
Authenticator auth = new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
//第一个参数:账户名 第二个参数:密码
return new PasswordAuthentication("zhangsan","123");
}
};
//创建一个程序和邮箱服务器的会话对象Session
Session session = Session.getInstance(pro, auth);
Message message = new MimeMessage(session);//创建一个message对象,用于设置邮件内容
message.setFrom(new InternetAddress("zhangsan@gyhqq.com"));//设置发件人
message.setRecipient(RecipientType.TO, new InternetAddress(mail));//设置发件方式和接受者
message.setSubject("邮件主题");//设置标题
message.setContent(mailMsg, "text/html;charset=utf-8");//设置邮件内容
//执行发送
Transport.send(message);
}
public static void main(String[] args) throws AddressException, MessagingException {
sendMail("lisi@gyhqq.cn", "你好");
}
}
第3章 定时任务
3.1 什么是定时任务
定时任务用于安排以后在后台线程中执行的任务。可安排任务执行一次,或者定期重复执行。文章源自JAVA秀-https://www.javaxiu.com/825.html
3.2 定时任务的应用场景
3.2.1 每隔1小时检查用户在线人数
每隔1小时系统自动统计在线的用户人数。首先需要编写统计用户在线人数功能,然后我们需要1小时执行一次。需要java中的定时任务。文章源自JAVA秀-https://www.javaxiu.com/825.html
3.2.2 每隔5分钟更新新闻
新闻都是实时的,而且新闻都是每隔一段时间更新最新的新闻。所以我们需要定时任务完成。文章源自JAVA秀-https://www.javaxiu.com/825.html
3.3 Timer常用的API
3.3.1 API介绍
Timer() 创建一个新计时器。
schedule(TimerTask task, Date firstTime, long period) 执行定时任务的方法 TimerTask:要执行的定时任务 firstTime:开始执行任务的时间 period:间隔多长时间重复执行
TimerTask() 创建定时器的任务
abstract void run() 要执行的定时任务在run方法中定义
3.3.2 使用步骤
1.创建定时器Timer对象文章源自JAVA秀-https://www.javaxiu.com/825.html
2.调用Timer对象的schedule方法执行定时任务文章源自JAVA秀-https://www.javaxiu.com/825.html
3.创建定时器任务对象TimerTask文章源自JAVA秀-https://www.javaxiu.com/825.html
4.实现TimerTask的run方法文章源自JAVA秀-https://www.javaxiu.com/825.html
3.3.3 案例代码
public static void main(String[] args) throws AddressException, MessagingException {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
System.out.println("该睡觉了");
}
},new Date(),1000*3);
}
3.4 案例:每隔30秒向易邮邮件服务器发送邮件
3.4.1 案例需求
每隔30秒向易邮邮件服务器发送邮件文章源自JAVA秀-https://www.javaxiu.com/825.html
3.4.2 案例效果
每隔30秒钟,lisi账户接受到zhangsan发送的邮件文章源自JAVA秀-https://www.javaxiu.com/825.html
3.4.3 案例分析
编写ServletContextListenner监听器监听servletContext对象的创建文章源自JAVA秀-https://www.javaxiu.com/825.html
在监听servletContext对象创建的方法contextInitialized中创建Timer定时器对象文章源自JAVA秀-https://www.javaxiu.com/825.html
设置定时器执行任务文章源自JAVA秀-https://www.javaxiu.com/825.html
创建TimerTask定时器任务对象文章源自JAVA秀-https://www.javaxiu.com/825.html
实现TimerTask的run方法,其中使用邮件工具类,给lisi发送邮件文章源自JAVA秀-https://www.javaxiu.com/825.html
3.4.4 案例代码
public class SendMailListenner implements ServletContextListener { public SendMailListenner() { } public void contextInitialized(ServletContextEvent sce) { //创建定时器对象 Timer timer = new Timer(); //执行定时任务,每隔30秒执行一次 timer.schedule(new TimerTask() { @Override public void run() { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String time = format.format(new Date()); System.out.println("开始发送邮件,现在时间"+time); try { //发送邮件 MailUtils.sendMail("lisi@gyhqq.com", "你好"); } catch (AddressException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }, new Date(), 1000*30); } public void contextDestroyed(ServletContextEvent sce) { } }文章源自JAVA秀-https://www.javaxiu.com/825.html

评论