📄 sendletterservlet.java
字号:
package com.lovo.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lovo.po.Mail;
import com.lovo.service.IMailBo;
import com.lovo.service.IMailBoImpl;
import com.lovo.service.IUserBo;
import com.lovo.service.IUserBoImpl;
public class SendLetterServlet extends HttpServlet
{
/** userBo接口,用于业务方法调用 */
private IUserBo userBo = new IUserBoImpl();
/** 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
{
String message = "";//反馈信息
boolean canSend = true;//标记信件是否可以发送
request.getSession().removeAttribute("isUpdate");
/** 设置响应文件类型及字符集编码 */
response.setContentType("text/html; charset=utf-8");
/** 设置响应头,让浏览器不缓存信息 */
response.setHeader("Cache-control", "no-cache");
/** 获取发件人 */
String whoSended = request.getParameter("whoSended");
/** 获取所有收件人 */
String emailNames = request.getParameter("emailNames");
/** 获取邮件主题 */
String mailTitle = request.getParameter("mailTitle");
if(mailTitle.equals("")){mailTitle = "(无主题)";}
/** 获取邮件内容 */
String mailContent = request.getParameter("mailContent");
/** 检查所有收件人是否都存在 */
String[] names = emailNames.split(";");//存收件人帐号
int[] userIds = new int[names.length];//存收件人userId
int judge = 0;//用于存当前用户userId
/**
* 循环条件,遍历每个收件人帐号
*/
for(int i = 0; i < names.length; i++)
{
/** 此方法获得当前帐号的userId */
judge = userBo.isEmailNameExist(names[i]);
/**
* 如果当前userId为-1,说明没有这个用户,不能发送
* 如果当前userId不为-1,说明有这个用户,将他的userId存入userIds数组
*/
if(judge == -1)
{
/** 用户不存在,提示用户哪个帐号录入错误 */
message = "收件人" + names[i] + "不存在请确认收件人!";
/** canSend设置为false表示不做发送信件操作 */
canSend = false;
/** 无需继续检测,跳出检测帐号的循环 */
break;
}
else
{
/** 用户存在,将其userId存入userIds数组 */
userIds[i] = judge;
}
}
/**
* 如果canSend为true,那么发送信件
*/
if(canSend)
{
/** 收件人都存在可以发送信件,开始构建信件并发送 */
Mail mail = null;//临时存要发送的信件
/**
* 循环条件,遍历userId
*/
for(int i = 0; i < userIds.length; i++)
{
/** 创建一个待发信件 */
mail = new Mail();
/** 设置待发信件状态码 */
mail.setMailStatus(Mail.NOT_READ_MAIL);
/** 设置待发信件标题 */
mail.setMailTitle(mailTitle);
/** 设置待发信件内容 */
mail.setMailContent(mailContent);
/** 设置待发信件所属用户 */
mail.setUserId(userIds[i]);
/** 设置待发信件的发件人 */
mail.setWhoSended(Integer.parseInt(whoSended));
/** 设置待发信件的收件人 */
mail.setWhoReceive(userIds[i]);
/** 发送信件 */
mailBo.sendMail(mail);
}
/** 存一份信件到已发送 */
mail = new Mail();
mail.setMailStatus(Mail.SENDED_MAIL);
mail.setMailTitle(mailTitle);
mail.setMailContent(mailContent);
mail.setUserId(Integer.parseInt(whoSended));
mail.setWhoSended(Integer.parseInt(whoSended));
/** 如果收件人为一个人,那么就设为这个人,否则设为自己 */
if(userIds.length == 1)
{
mail.setWhoReceive(userIds[0]);
}
else
{
mail.setWhoReceive(Integer.parseInt(whoSended));
}
mailBo.sendMail(mail);
message = "发送成功!";
}
/** 获得输出流 */
PrintWriter writer = response.getWriter();
/** 反馈操作信息 */
writer.println(message);
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 + -