⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 viewletterservlet.java

📁 运用JSP/servlet/JavaBean 技术
💻 JAVA
字号:
package com.lovo.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
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 ViewLetterServlet extends HttpServlet
{
	/** IMailBo接口,供Mail的业务方法调用 */
	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");
		/** 获得做什么操作的指示字符串 */
		String operation = request.getParameter("operation");
		HttpSession session = request.getSession();//存当前会话
		/** 如果要做的是设置要查看的邮件Id那么做以下操作 */
		if(operation.equals("setMailId"))
		{
			List<MailVo> mailVo = null;//存从session中取出的所有MailVo
			/** 获取要查看的mailId */
			int mailId = Integer.parseInt(request.getParameter("mailId"));
			/** 获取要查看的信件类型 */
			int mailStatus = 
				Integer.parseInt(request.getParameter("mailStatus"));
			/** 获取当前信件并添加到session中 */
			switch(mailStatus)
			{
			case Mail.NOT_READ_MAIL: mailVo = 
				(ArrayList<MailVo>)session.getAttribute("notReadList");
				if(mailVo != null)
				{
					addCurrentMailToSession(mailVo, session, mailId);
				}
				mailVo = (ArrayList<MailVo>)session.getAttribute("readYetList");
				if(mailVo != null)
				{
					addCurrentMailToSession(mailVo, session, mailId);
				}
				break;
			case Mail.DRAFT_MAIL: mailVo =
				(ArrayList<MailVo>)session.getAttribute("draftMailList");
				addCurrentMailToSession(mailVo, session, mailId);
				/** 设置属性isUpdate,指示如果存草稿,那么是存新的一个信件还是只是草稿的更新 */
				session.setAttribute("isUpdate", "true");
				break;
			case Mail.SENDED_MAIL: mailVo =
				(ArrayList<MailVo>)session.getAttribute("sendedMailList");
				addCurrentMailToSession(mailVo, session, mailId);
				break;
			case Mail.DELETED_MAIL: mailVo =
				(ArrayList<MailVo>)session.getAttribute("deletedMailList");
				addCurrentMailToSession(mailVo, session, mailId);
				break;
			}
		}
		/**
		 * 如果要做的操作是查看信件信息,那么做以下操作
		 */
		if(operation.equals("viewLetter"))
		{
			/** 获得当前要查看的信件对象 */
			MailVo mail = (MailVo)session.getAttribute("currentMail");
			/** 获得输出流 */
			PrintWriter writer = response.getWriter();
			/** 构建xml */
			writer.println("<?xml version='1.0' encoding='UTF-8'?>");
			writer.println("<mail>");
			writer.println("<title>" + mail.getMailTitle() + "</title>");
			writer.println("<whosended>" + 
					mail.getWhoSendedName() + "</whosended>");
			writer.println("<whoreceive>" + 
					mail.getWhoReceiveName() + "</whoreceive>");
			writer.println("<content>" + mail.getMailContent() + "</content>");
			writer.println("</mail>");
			writer.flush();
		}
	}

	/**
	 * 此方法用来在session中找到当前信件,如果找到,那么将需要的信息设置到session里,以便之后查看
	 * @param mailVo 存了和当前信件同类的所有信件,将从中找出当前信件
	 * @param session 当前会话
	 * @param mailId 当前要查找的信件
	 */
	private void addCurrentMailToSession(List<MailVo> mailVo,
			HttpSession session, int mailId)
	{
		int loop = mailVo.size();//list的长度
		MailVo mail = null;//存list中的当前邮件对象
		/**
		 * 遍历list,找到当前要查看的信件
		 */
		for(int i = 0; i < loop; i++)
		{
			mail = mailVo.get(i);
			/**
			 * 如果找到了要查看的信件那么将其放入session中
			 */
			if(mail.getMailId() == mailId)
			{
				/** 如果是未读信件,那么把它设置为已读 */
				if(mail.getMailStatus() == Mail.NOT_READ_MAIL)
				{
					Mail m = new Mail();
					m.setMailId(mail.getMailId());
					m.setMailStatus(Mail.READ_YET_MAIL);
					m.setMailTitle(mail.getMailTitle());
					m.setMailContent(mail.getMailContent());
					m.setUserId(mail.getUserId());
					m.setWhoSended(mail.getWhoSended());
					m.setWhoReceive(mail.getWhoReceive());
					mailBo.updateMail(m);
				}
				session.setAttribute("currentMail", mail);
				break;
			}
		}
	}
	/**
	 * 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 + -