第十二周上机作业
1.实现 删除 回复邮件
2.实现阅读邮件功能:在main.jsp中点击任意邮件的标题,进入到detail.jsp显示邮件详情,包括发件人,主题,内容,时间。同时需要把邮件状态修改为已读。
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.List; import entity.Msg; public class MsgDao extends BaseDao { // 根据姓名查看所有邮件 public List<Msg> getMailByReceiver(String name) { List<Msg> list = new ArrayList<Msg>(); Connection con = getConnection(); String sql = "select * from msg where sendto=?"; PreparedStatement ps = null; ResultSet rs = null; try { ps = con.prepareStatement(sql); ps.setString(1, name); rs = ps.executeQuery(); while (rs.next()) { Msg m = new Msg(); m.setMsgid(rs.getInt("msgid")); m.setUsername(rs.getString("username")); m.setTitle(rs.getString("title")); m.setState(rs.getInt("state")); m.setMsgcontent(rs.getString("msgcontent")); m.setSendto(rs.getString("sendto")); m.setMsg_create_date(rs.getDate("mgs_create_date")); list.add(m); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { closeAll(con, ps, rs); } return list; } // 添加邮件(写邮件,回复邮件都调用,邮件状态为1(未读),时间为系统当前时间) public void addMsg(Msg m) { Connection conn = getConnection(); String sql = "insert into msg(username,title,msgcontent,state,sendto,msg_create_date) values(?,?,?,0,?,?)"; PreparedStatement ps = null; try { ps = conn.prepareStatement(sql); ps.setString(1, m.getUsername()); ps.setString(2, m.getTitle()); ps.setString(3, m.getMsgcontent()); ps.setString(4, m.getSendto()); ps.setDate(5, new java.sql.Date(new Date().getTime())); ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { closeAll(conn, ps, null); } } // 根据id删除邮件 public void delMsg(int id) { Connection con = getConnection(); String sql = "delete from msg where msgid=" + id; PreparedStatement ps = null; try { ps = con.prepareStatement(sql); ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { closeAll(con, ps, null); } } // 查看信息之后状态变为已读 public void updateMsg(int id) { Connection con = getConnection(); String sql = "update msg set state='1' where msgid=?"; PreparedStatement pred = null; try { pred = con.prepareStatement(sql); pred.setInt(1, id); pred.executeUpdate(); } catch (SQLException e1) { e1.printStackTrace(); } finally { closeAll(con, pred, null); } } // 查看详细信息 public Msg Cha(int msgid) { Msg m = null; Connection con = getConnection(); ResultSet rs = null; PreparedStatement ps = null; String sql = "select * from msg where msgid=?"; try { ps = con.prepareStatement(sql); ps.setInt(1, msgid); rs = ps.executeQuery(); while (rs.next()) { m = new Msg(); m.setMsgid(rs.getInt("msgid")); m.setUsername(rs.getString("username")); m.setTitle(rs.getString("title")); m.setMsgcontent(rs.getString("msgcontent")); m.setState(rs.getInt("state")); m.setSendto(rs.getString("sendto")); m.setMsg_create_date(rs.getDate("mgs_create_date")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { closeAll(con, ps, rs); } return m; } }
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> <form action="dowrite.jsp" method="post"> 收件人:<input type="text" name="sendto" value="<%=request.getParameter("reply") %>"><br> 主题: <input type="text" name="title" ><br> 内容 <textarea rows="6" cols="20" name="content"></textarea> <br> <input type="submit" value="发送"> </form> </body> </html>
<%@page import="entity.Msg"%> <%@page import="dao.MsgDao"%> <%@page import="dao.UsersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% request.setCharacterEncoding("utf-8"); String uname=(String)session.getAttribute("uname");// 发件人在session中获取 String sendto=request.getParameter("sendto"); String title=request.getParameter("title"); String content=request.getParameter("content"); Msg m=new Msg(); m.setTitle(title); m.setMsgcontent(content); m.setUsername(uname); m.setSendto(sendto); MsgDao md=new MsgDao(); md.addMsg(m); out.print("发送成功,即将跳回首页....."); response.setHeader("refresh", "3;url=main.jsp"); %>
<%@page import="entity.Msg"%> <%@page import="dao.MsgDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'index.jsp' main page</title> </head> <body> <% String uname=(String)session.getAttribute("uname"); %> 欢迎你 <% out.print(uname); %> <a href="write.jsp">写邮件</a> <table height="20px" width="100%" align="center" border="1"> <tr> <td>发件人</td> <td>主题</td> <td>状态</td> <td>时间</td> <td>操作</td> <td>操作</td> </tr> <% MsgDao md=new MsgDao(); List<Msg> list=md.getMailByReceiver(uname); for(Msg m:list) {%> <tr> <td><%=m.getUsername() %></td> <td><a href="detail.jsp?reply=<%=m.getMsgid()%>"><%=m.getTitle() %></a></td> <td><% int state=m.getState(); if(state==1){ %> <img src ="image/已读消息.png" /> <%; }else{%> <img src ="image/未读.png" /> <%; } %></td> <td><%=m.getMsg_create_date() %></td> <td><a href="write.jsp?reply=<%=m.getUsername()%>">回复</a></td> <td><a href="del.jsp?id=<%=m.getMsgid()%>">删除</a></td> </tr> <%} %> </table> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> <form action="dowrite.jsp" method="post"> 收件人:<input type="text" name="sendto" value="<%=request.getParameter("reply") %>"><br> 主题: <input type="text" name="title" ><br> 内容 <textarea rows="6" cols="20" name="content"></textarea> <br> <input type="submit" value="发送"> </form> </body> </html>
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@page import="entity.Msg"%> <%@page import="dao.MsgDao"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% String uname=(String)session.getAttribute("uname"); %> <% request.setCharacterEncoding("utf-8"); String msgid=request.getParameter("reply"); int id=Integer.parseInt(msgid); %> <table height="20px" width="100%" align="center" border="1"> <tr> <td>发件人</td> <td>主题</td> <td>内容</td> <td>时间</td> </tr> <% MsgDao md=new MsgDao(); Msg m=md.Cha(id); md.updateMsg(id); %> <tr> <td><%=m.getUsername() %></td> <td><%=m.getTitle() %></td> <td><%=m.getMsgcontent() %></td> <td><%=m.getMsg_create_date() %></td> </tr> </table> </body> </html>