第十二周作业
1.实现 删除 回复邮件
2.实现阅读邮件功能:在main.jsp中点击任意邮件的标题,进入到detail.jsp显示邮件详情,包括发件人,主题,内容,时间。同时需要把邮件状态修改为已读。
2.实现阅读邮件功能:在main.jsp中点击任意邮件的标题,进入到detail.jsp显示邮件详情,包括发件人,主题,内容,时间。同时需要把邮件状态修改为已读。
package com.sf.dao; 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 javax.annotation.PreDestroy; import com.sf.entity.msg; import com.sun.org.apache.regexp.internal.recompile; public class msgDao extends BaseDao{ public List<msg> listAll(String sendto) throws SQLException{ List<msg> list =new ArrayList<msg>(); Connection conn =getConnection(); String sql="select * from msg where sendto=?"; PreparedStatement pred =null; ResultSet rest=null; try { pred=conn.prepareStatement(sql); pred.setString(1, sendto); rest=pred.executeQuery(); while (rest.next()) { msg msg=new msg(); msg.setMsgid(rest.getInt("msgid")); msg.setUsername(rest.getString("username")); msg.setTitle(rest.getString("title")); msg.setMsgcontent(rest.getString("msgcontent")); msg.setState(rest.getInt("state")); msg.setSendto(rest.getString("sendto")); msg.setMsg_create_date(rest.getDate("msg_create_date")); list.add(msg); } } catch (Exception e) { // TODO: handle exception } finally{ closeAll(conn, pred, rest); } return list; } public void changestate(Integer msgid){ Connection conn =getConnection(); String sql="update msg set state=1 where msgid=?"; PreparedStatement pred=null; try { pred =conn.prepareStatement(sql); pred.setInt(1, msgid); pred.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ closeAll(conn, pred, null); } } public List<msg> showContent(Integer msgid) { List<msg> list=new ArrayList<msg>(); Connection conn=getConnection(); String sql="select * from msg where msgid=?"; PreparedStatement pred=null; ResultSet rest=null; try { pred = conn.prepareStatement(sql); pred.setInt(1, msgid); rest =pred.executeQuery(); while(rest.next()){ msg msg =new msg(); msg.setUsername(rest.getString("username")); msg.setTitle(rest.getString("title")); msg.setMsgcontent(rest.getString("msgcontent")); msg.setMsg_create_date(rest.getDate("msg_create_date")); list.add(msg); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ closeAll(conn, pred, rest); } return list; } public void delmsg(Integer msgid){ Connection conn =getConnection(); String sql="delete from msg where msgid=?"; PreparedStatement pred=null; try { pred =conn.prepareStatement(sql); pred.setInt(1, msgid); pred.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ closeAll(conn, pred, null); } } public void send(msg msg){ 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, msg.getUsername()); ps.setString(2, msg.getTitle()); ps.setString(3, msg.getMsgcontent()); ps.setString(4, msg.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); } } }
<%@page import="com.yxf.entity.Msg"%> <%@page import="com.yxf.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 ="images/sms_readed.png" /> <%; }else{%> <img src ="images/sms_unReaded.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"%> <!DOCTYPE HTML> <html> <head> <title></title> </head> <body> <form action="dowrite.jsp" method="post"> <table> <tr> <td>收件人</td> <td><input type="text" name="sendto" value="<%=request.getParameter("reply")%>"></td> </tr> <tr> <td>主题</td> <td><input type="text" name="title"></td> </tr> <tr> <td>内容</td> <td><textarea rows="6" cols="20" name="content"></textarea></td> </tr> <tr> <td><input type="submit" value="发送"></td> </tr> </table> </form> <a href="main.jsp">返回</a> </body> </html>
<%@page import="com.yxf.dao.MsgDao"%> <%@page import="com.yxf.entity.Msg"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML> <html> <head> <title></title> </head> <body> <% request.setCharacterEncoding("utf-8"); Msg e = new Msg(); String username = (String) session.getAttribute("uname"); e.setUsername(username); String sendto = request.getParameter("sendto"); e.setSendto(sendto); String title = request.getParameter("title"); e.setTitle(title); String msgcontent = request.getParameter("msgcontent"); e.setMsgcontent(msgcontent); MsgDao md = new MsgDao(); md.addMsg(e); out.print("发送成功,即将跳回首页..."); response.setHeader("refresh", "1;url=main.jsp"); %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title></title> </head> <%@ page import="com.yxf.dao.MsgDao"%> <body> <% String Id = request.getParameter("id"); int msgid = Integer.parseInt(Id); MsgDao md = new MsgDao(); md.xiugai(msgid); out.println(md.chakan(msgid)); %> <br> <a href="main.jsp">返回</a> </body> </html>
<%@page import="com.yxf.dao.MsgDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title></title> </head> <body> <% try { String msgid = request.getParameter("msgid"); int msgId = Integer.parseInt(msgid); MsgDao md = new MsgDao(); if (md.deleteMsg(msgId) != -1) { out.println("删除成功!"); out.println("即将跳转到主页面..."); response.setHeader("refresh", "1;url=main.jsp"); } else { out.println("删除失败!"); out.println("即将跳转到主页面..."); response.setHeader("refresh", "1;url=main.jsp"); } } catch (Exception e) { e.printStackTrace(); } %> </body> </html>