1、使用jQuery的Ajax功能,完成查询邮箱功能。
每隔五秒刷新一次即查询数据库将信息加载到当前页面,
每次查询到邮件的信息包括:发件人,发送时间,邮件内容。
创建数据库:
DAO层代码:
package pojo;
import java.sql.Timestamp;
import java.util.Date;
//邮件信息实体类
public class Mail {
private int id;
private String name;
private Timestamp date;
private String content;
public Mail() {
}
public Mail(int id, String name, Timestamp date, String content) {
this.id = id;
this.name = name;
this.date = date;
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDate() {
return date;
}
public void setDate(Timestamp date) {
this.date = date;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
package util;
import java.sql.*;
import java.util.*;
import pojo.Mail;
import com.mchange.v2.c3p0.ComboPooledDataSource;
//通过数据库连接池获取数据库连接
public class DBUtil {
private static int pageSize=3; //定义每页查询最多信息条数
private static ComboPooledDataSource ds; //定义全局变量数据库连接池对象
static{
ds = new ComboPooledDataSource(); //静态代码块中进行数据库连接池对象实例化
}
public static List<Mail> findMail(int page){
List<Mail> list = new ArrayList<Mail>(); //实例化一个ArrayList集合来存储邮件信息
Connection conn = null;
PreparedStatement ps = null; //定义数据库预处理语句对象
ResultSet rs = null;
try {
conn = ds.getConnection(); //通过数据库连接池获取数据库连接对象
String sql = "select id,name,time,context from email limit ?,?"; //定义数据库查询语句
ps = conn.prepareStatement(sql);
ps.setInt(1, (page - 1) * pageSize);
ps.setInt(2, pageSize);
rs = ps.executeQuery(); //将执行后的结果放到rs结果集对象实例中
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
Timestamp date = rs.getTimestamp("time");
String content = rs.getString("context");
Mail mail = new Mail(id, name, date, content);
list.add(mail);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
// 根据pageSize与总条数计算总页数
public static int totalPage(){
int count=0;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn=ds.getConnection();
String sql="select count(*) from student";
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next()){
count=rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return count%pageSize==0?count/pageSize:count/pageSize+1;
}
//获取每个邮件详细信息
public static Mail getMailInfo(int id){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Mail mail = null; //定义邮件对象为全局变量,以便于作为返回值
try {
conn = ds.getConnection();
String sql = "select id,name,time,context from email where id=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs =ps.executeQuery();
while(rs.next()){
int mailid = rs.getInt("id");
String name = rs.getString("name");
Timestamp date = rs.getTimestamp("time");
String context = rs.getString("context");
mail = new Mail(mailid,name,date,context);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{ //关闭资源
try {
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return mail;
}
}
前端页面代码:
<%@page import="java.net.URLDecoder"%>
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>邮件信息概览</title>
</head>
<body>
<a href="${pageContext.servletContext.contextPath}/allMailServlet?page=1">查看邮件信息</a>
</body>
</html>
<%@page import="java.net.URLDecoder"%>
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>登录查询界面</title>
<style type="text/css">
#msg{
width:200px;
margin:10px auto;
}
</style>
</head>
<body>
<h3 style="color:red">${msg}</h3>
<div id="check">
<div id="3">
<c:forEach items="${requestScope.maillist}" var="mail">
<a href="${pageContext.servletContext.contextPath}/DetailServlet?id=${mail.id}">${mail.name}</a> <br/><br/>
</c:forEach>
<a href="${pageContext.servletContext.contextPath}/allMailServlet?page=1">首页</a>
<a href="${pageContext.servletContext.contextPath}/allMailServlet?page=${requestScope.currentPage-1}">上一页</a>
<a href="${pageContext.servletContext.contextPath}/allMailServlet?page=${requestScope.currentPage+1}">下一页</a>
<a href="${pageContext.servletContext.contextPath}/allMailServlet?page=${requestScope.totalPage}">末页</a>
</div>
</div>
</body>
</html>
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>邮件详细信息</title>
<style type="text/css">
div{
width:200px;
margin:10px auto;
}
</style>
<script type="text/javascript" src="../js/jquery-3.1.1.js"></script>
<script type="text/javascript">
$(function(){
$("#sort").blur(function(){
$.ajax({
method:"get",
url:"/AjaxHomeWork/DetailServlet",
data:{},
beforeSend:function(){
alert("您查询的邮件信息即将显示...")
},
success:function getinfo(email){
$("#1").html(email.id);
$("#2").html(email.name);
$("#3").html(email.date);
$("#4").html(email.content);
}
});
});
});
setInterval(getInfo(),5000,"每隔五秒定时刷新!");
</script>
</head>
<body>
<div id="sort">
<h2 style="color:blue">邮件详细信息</h2>
邮件编号:<span id="1"></span><br/>
邮件发件人:<span id="2"></span><br/>
邮件发送日期:<span id="3"></span><br/>
邮件发送内容:<span id="4"></span><br/>
</div>
</body>
</html>
servlet类:
package servlet;
import java.io.IOException;
import java.util.List;
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 pojo.Mail;
import util.DBUtil;
//从数据库查询邮件具体信息
@WebServlet(name = "CheckMailServlet", urlPatterns = { "/allMailServlet" })
public class CheckMailServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doPost(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String pageStr = req.getParameter("page");
int page = Integer.parseInt(pageStr);
int totalPage = DBUtil.totalPage();
if (page < 1) {
req.setAttribute("msg", "已经是首页了!");
page = 1;
}
if (page > totalPage) {
req.setAttribute("msg", "已经是最后一页了!");
page = totalPage;
}
List<Mail> list = DBUtil.findMail(page);
req.setAttribute("maillist", list); // 保存查询页的邮件信息列表
req.setAttribute("currentPage", page); // 保存要查询的页码
req.setAttribute("totalPage", totalPage); // 保存总页数
req.getRequestDispatcher("/login/login.jsp").forward(req, resp);
}
}
package servlet;
import java.io.IOException;
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 pojo.Mail;
import util.DBUtil;
@WebServlet(name="DetailServlet",urlPatterns={"/DetailServlet"})
public class DetailServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String idStr = req.getParameter("id"); //获取邮件id字符串
int id = Integer.parseInt(idStr); //将id字符串转化为int类型
Mail mail = DBUtil.getMailInfo(id);//通过调用M层的方法获取邮件对象
req.setAttribute("email", mail); //将邮件对象实例放到请求中
req.getRequestDispatcher("login/emailInfo.jsp").forward(req, resp); //通过服务器转发
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}