📄 receivemailservlet.java
字号:
package com.lovo.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.lovo.po.Mail;
import com.lovo.po.MailVo;
import com.lovo.service.IMailBo;
import com.lovo.service.IMailBoImpl;
public class ReceiveMailServlet extends HttpServlet
{
/** mailBo接口,用于业务方法调用 */
private IMailBo mailBo = new IMailBoImpl();
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
/** 设置响应文件类型及字符集编码 */
response.setContentType("text/xml; charset=utf-8");
/** 设置响应头,让浏览器不缓存信息 */
response.setHeader("Cache-control", "no-cache");
/** 获取请求收件的帐号userId */
int userId = Integer.parseInt(request.getParameter("userId"));
/** 查询未读信件 */
List<MailVo> notReadList = mailBo.queryMail(userId, Mail.NOT_READ_MAIL);
/** 查询已读信件 */
List<MailVo> readYetList = mailBo.queryMail(userId, Mail.READ_YET_MAIL);
/** 将收到的信件添加到session */
HttpSession session = request.getSession();
session.setAttribute("notReadList", notReadList);
session.setAttribute("readYetList", readYetList);
boolean create = true;//标志是否需要构建xml
int notReadMail = notReadList == null ? 0 : notReadList.size();//未读信件数量
int readYetMail = readYetList == null ? 0 : readYetList.size();//已读信件数量
// System.out.println("notReadList.size=" + notReadMail);
// System.out.println("readYetList.size=" + readYetMail);
/** 判断是否需要构建xml */
if(notReadMail == 0 && readYetMail == 0)
{
/** 既没有未读信件,也没有已读信件则不需构建xml,将create设为false */
create = false;
}
/** 无信息返回 */
if(create == false)
{
return;
}
/** 获得输出流 */
PrintWriter writer = response.getWriter();
/** 构建xml */
writer.println("<?xml version='1.0' encoding='UTF-8'?>");
writer.println("<mail>");
String whoSend = "";//存发件人帐号
String mailTitle = "";//存邮件标题
int mailId = 0;//存邮件Id
MailVo mailVo = null;//存当前邮件
/** 如果有未读信件那么做以下操作 */
if(notReadMail != 0)
{
/**
* 遍历notReadList获得要返回页面的信息并构建xml
*/
for(int i = 0; i < notReadMail; i++)
{
/** 获得当前未读邮件 */
mailVo = notReadList.get(i);
/** 获得发件人帐号 */
whoSend = mailVo.getWhoSendedName();
/** 获得邮件主题 */
mailTitle = mailVo.getMailTitle();
/** 获得邮件ID */
mailId = mailVo.getMailId();
writer.println("<notreadmail>");
writer.println("<whosend>" + whoSend + "</whosend>");
writer.println("<mailtitle>" + mailTitle + "</mailtitle>");
writer.println("<mailid>" + mailId + "</mailid>");
writer.println("</notreadmail>");
}
}
/** 如果有已读信件那么做以下操作 */
if(readYetMail != 0)
{
/**
* 遍历readYetList获得要返回页面的信息并构建xml
*/
for(int i = 0; i < readYetMail; i++)
{
/** 获得当前已读邮件 */
mailVo = readYetList.get(i);
/** 获得发件人帐号 */
whoSend = mailVo.getWhoSendedName();
/** 获得邮件主题 */
mailTitle = mailVo.getMailTitle();
/** 获得邮件ID */
mailId = mailVo.getMailId();
writer.println("<readyetmail>");
writer.println("<whosend>" + whoSend + "</whosend>");
writer.println("<mailtitle>" + mailTitle + "</mailtitle>");
writer.println("<mailid>" + mailId + "</mailid>");
writer.println("</readyetmail>");
}
}
writer.println("</mail>");
writer.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -