📄 querymailservlet.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 QueryMailServlet 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"));
/** 获取请求查询的邮件的类型 */
int mailStatus = Integer.parseInt(request.getParameter("mailStatus"));
/** 查询信件 */
List<MailVo> mailList = mailBo.queryMail(userId, mailStatus);
/** 将查到的信件添加到session以便后面操作 */
HttpSession session = request.getSession();
switch(mailStatus)
{
case Mail.DRAFT_MAIL: session.setAttribute("draftMailList", mailList);
break;
case Mail.SENDED_MAIL: session.setAttribute("sendedMailList", mailList);
break;
case Mail.DELETED_MAIL: session.setAttribute("deletedMailList", mailList);
break;
}
boolean needCreateXML = true;//标志是否需要构建xml
int mailLength = mailList == null ? 0 : mailList.size();//信件数量
// System.out.println("请求查询的帐号id=" + userId);
// System.out.println("请求查询的邮件status=" + mailStatus);
// System.out.println("查询得到的邮件数量=" + mailLength);
/**
* 判断是否需要构建xml,如果没有信件则无需构建xml
*/
if(mailLength == 0)
{
needCreateXML = false;
}
/** 无信息返回 */
if(needCreateXML == false)
{
return;
}
// System.out.println("needCreateXML=" + needCreateXML);
/** 获得输出流 */
PrintWriter writer = response.getWriter();
/** 构建xml */
writer.println("<?xml version='1.0' encoding='UTF-8'?>");
writer.println("<mail>");
String who = "";//存收件人,或发件人帐号
String mailTitle = "";//存邮件主题
int mailId = 0;//存邮件Id
MailVo mailVo = null;//存当前邮件
/**
* 遍历mailList获得要返回页面的信息并构建xml
*/
for(int i = 0; i < mailLength; i++)
{
/** 获得当前邮件 */
mailVo = mailList.get(i);
/**
* 如果要查询的是已删除信件,那么获得它的发件人帐号,否则获得收件人帐号
*/
if(mailStatus == Mail.DELETED_MAIL)
{
/** 要查的是已删除信件,获得发件人帐号 */
who = mailVo.getWhoSendedName();
}
else
{
/** 要查的是草稿信件或已发送信件,获得收件人帐号 */
who = mailVo.getWhoReceiveName();
}
/** 获得信件主题 */
mailTitle = mailVo.getMailTitle();
/** 获取信件主键mailId */
mailId = mailVo.getMailId();
/** 把获得的信息构建成xml */
writer.println("<letter>");
writer.println("<who>" + who + "</who>");
writer.println("<mailtitle>" + mailTitle + "</mailtitle>");
writer.println("<mailid>" + mailId + "</mailid>");
writer.println("</letter>");
}
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 + -