一、使用jQuery的Ajax功能,完成轮询邮箱功能。每隔5秒去后台查询数据库,将信息加载到当前页面。
每次查询到的邮件信息包括:发件人、发送时间、邮件内容。
提示:前端可以使用setInterval("getInfo()",5000)完成每5秒,调用getInfo()函数。
1、EmailInfoServlet.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import org.json.*;
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 email.Email;
import util.DBUtil;
@WebServlet(name = "EmailInfoServlet", urlPatterns = { "/emailInfoServlet" })
public class EmailInfoServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setHeader("content-type","application/x-www-form-urlencoded");
resp.setCharacterEncoding("utf-8");
PrintWriter pw=resp.getWriter();
List<Email> list=DBUtil.checkInfo();
JSONArray jsonArray=new JSONArray(list);
String jsonArrayStr=jsonArray.toString();
pw.println(jsonArrayStr);
pw.flush();
pw.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
}
2、email.java
package email;
public class Email {
private String sendName;
private String time;
private String content;
public Email() {
}
public Email(String sendName,String time, String content) {
this.sendName = sendName;
this.time = time;
this.content = content;
}
public String getSendName() {
return sendName;
}
public void setSendName(String sendName) {
this.sendName = sendName;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
3、DBUtil.java
package util;
import java.sql.*;
import java.util.*;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import email.Email;
public class DBUtil {
private static ComboPooledDataSource ds;
static{
ds=new ComboPooledDataSource();
}
public static List<Email> checkInfo(){ //查询到邮件信息
List<Email> list=new ArrayList<Email>();
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
conn=ds.getConnection();
String sql="select sendName,time,content from email ";
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
String sendName=rs.getString("sendName");
String time=rs.getString("time");
String content=rs.getString("content");
Email email=new Email(sendName,time,content);
list.add(email);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
}
4、checkEmail
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>轮询邮箱</title>
<style type="text/css">
#title{
width:100px;
color:red;
}
</style>
<script type="text/javascript" src="../js/jquery-3.1.1.js"></script>
<script type="text/javascript">
function getInfo(){
$.ajax({
method:"post",
url:"/AjaxProject/emailInfoServlet",
success:function(data){
$("#msg").html(data);
}
});
}
setInterval("getInfo()",5000);
</script>
</head>
<body>
<form action="${pageContext.servletContext.contextPath}/emailInfoServlet" method="post" >
<div id="title" > 邮件信息</div><br/><br/>
<span id="msg" style="backgrond-color:yellow" ></span> <br/><br/>
</form>
</body>
</html>