JSP第十二周作业

1.实现 删除 回复邮件
2.实现阅读邮件功能:在main.jsp中点击任意邮件的标题,进入到detail.jsp显示邮件详情,包括发件人,主题,内容,时间。同时需要把邮件状态修改为已读。

  1 package com.lwk.dao;
  2 
  3 import java.sql.Connection;
  4 import java.sql.PreparedStatement;
  5 import java.sql.ResultSet;
  6 import java.sql.SQLException;
  7 import java.util.ArrayList;
  8 import java.util.Date;
  9 import java.util.List;
 10 
 11 import com.lwk.entity.Msg;
 12 
 13 public class MsgDao extends BaseDao {
 14 
 15     /**
 16      * 发送邮件,添加到数据库
 17      * @param m
 18      */
 19     public void addMsg(Msg m) {
 20         Connection conn = getConnection();
 21         String sql = "insert into msg(uname,title,msgcontent,state,sendto,msg_create_date) values(?,?,?,?,?,?)";
 22         PreparedStatement ps = null;
 23         try {
 24             ps = conn.prepareStatement(sql);
 25             ps.setString(1, m.getUsername());
 26             ps.setString(2, m.getTitle());
 27             ps.setString(3, m.getMsgcontent());
 28             ps.setInt(4, 1);
 29             ps.setString(5, m.getSendto());
 30             ps.setDate(6, new java.sql.Date(new Date().getTime()));// 系统当前时间
 31             ps.executeUpdate();
 32         } catch (SQLException e) {
 33             // TODO Auto-generated catch block
 34             e.printStackTrace();
 35         } finally {
 36             closeAll(conn, ps, null);
 37         }
 38 
 39     }
 40 
 41     /**
 42      * 删除邮件
 43      * @param id
 44      */
 45     public void delMail(int id) {
 46         Connection conn = getConnection();
 47         String sql = "delete from msg where eid=?";
 48         PreparedStatement ps = null;
 49         try {
 50             ps = conn.prepareStatement(sql);
 51             ps.setInt(1, id);
 52             ps.executeUpdate();
 53         } catch (SQLException e) {
 54             // TODO Auto-generated catch block
 55             e.printStackTrace();
 56         }finally{
 57             closeAll(conn, ps, null);
 58         }
 59 
 60     }
 61 
 62     /**
 63      * 修改邮件状态
 64      * @param id
 65      */
 66     public void updateStateById(int id){
 67         Connection conn = getConnection();
 68         String sql = "update msg set state = 1 where eid = ?";
 69         PreparedStatement ps = null;
 70         try {
 71             ps = conn.prepareStatement(sql);
 72             ps.setInt(1, id);
 73             ps.executeUpdate();
 74         } catch (SQLException e) {
 75             // TODO Auto-generated catch block
 76             e.printStackTrace();
 77         }
 78     }
 79 
 80     /**
 81      * 查询全部邮件
 82      * @param name
 83      * @return
 84      */
 85     public List<Msg> getMailByReceiver(String name) {
 86         List<Msg> list = new ArrayList<Msg>();
 87         Connection conn = getConnection();
 88         String sql = "select * from msg where sendto=?";
 89         PreparedStatement ps = null;
 90         ResultSet rs = null;
 91         try {
 92             ps = conn.prepareStatement(sql);
 93             ps.setString(1, name);
 94             rs = ps.executeQuery();
 95             while (rs.next()) {
 96                 Msg m = new Msg();
 97                 m.setMsgid(rs.getInt("eid"));
 98                 m.setUsername(rs.getString("uname"));
 99                 m.setTitle(rs.getString("title"));
100                 m.setMsgcontent(rs.getString("msgcontent"));
101                 m.setState(rs.getInt("state"));
102                 m.setSendto(rs.getString("sendto"));
103                 m.setMsg_create_date(rs.getDate("msg_create_date"));
104                 list.add(m);
105             }
106 
107         } catch (SQLException e) {
108             // TODO Auto-generated catch block
109             e.printStackTrace();
110         } finally {
111             closeAll(conn, ps, rs);
112         }
113         return list;
114     }
115 
116 }
 1 <%@page import="com.lwk.dao.MsgDao"%>
 2 <%@page import="com.lwk.entity.Msg"%>
 3 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 4 
 5 <html>
 6 <head>
 7 
 8 
 9 
10 
11 <title>My JSP 'index.jsp' starting page</title>
12 
13 </head>
14 
15 <body>
16     <%
17         String uname = (String) session.getAttribute("uname");
18     %>
19     首页!!欢迎你<%=uname%>
20     <a href="write.jsp">写邮件</a>
21     <table border="1">
22         <tr>
23             <td>发件人</td>
24             <td>主题</td>
25             <td>状态</td>
26             <td>时间</td>
27             <td>操作</td>
28             <td>操作</td>
29             <td>操作</td>
30             <%
31                 MsgDao md = new MsgDao();
32                 List<Msg> list = md.getMailByReceiver(uname);
33                 for (int i = 0; i < list.size(); i++) {
34             %>
35         
36         <tr>
37             <td><%=list.get(i).getUsername()%></td>
38             <td><%=list.get(i).getTitle()%></td>
39             <td>
40                 <%
41                     if (list.get(i).getState() == 1) {
42                 %> <img src="images/sms_unReaded.png" /> <%
43      } else {
44  %> <img src="images/sms_readed.png" /> <%
45      }
46  %>
47             </td>
48             <td><%=list.get(i).getMsg_create_date()%></td>
49             <td><a href="delete.jsp?id=<%=list.get(i).getMsgid()%>">删除</a></td>
50             <td><a href="write.jsp?reply=<%=list.get(i).getUsername()%>">回复</a>
51             </td>
52             <td><a href="detail.jsp?<%=getMsg(list.get(i))%>">查看</a></td>
53 
54 
55         </tr>
56 
57 
58         <%
59             }
60         %>
61 
62     </table>
63     <%!String getMsg(Msg msg) {
64         return "msgid=" + msg.getMsgid() + "&uname=" + msg.getUsername()
65                 + "&time=" + msg.getMsg_create_date() + "&title="
66                 + msg.getTitle() + "&msgcontent=" + msg.getMsgcontent();
67     }%>
68 
69 
70 
71 
72 
73 </body>
74 </html>
 1 <%@page import="com.lwk.dao.MsgDao"%>
 2 <%@page import="com.lwk.entity.Msg"%>
 3 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 4 
 5 <html>
 6 <head>
 7 
 8 
 9 
10 
11 <title>My JSP 'index.jsp' starting page</title>
12 
13 </head>
14 
15 <body>
16     <%
17         String uname = (String) session.getAttribute("uname");
18     %>
19     首页!!欢迎你<%=uname%>
20     <a href="write.jsp">写邮件</a>
21     <table border="1">
22         <tr>
23             <td>发件人</td>
24             <td>主题</td>
25             <td>状态</td>
26             <td>时间</td>
27             <td>操作</td>
28             <td>操作</td>
29             <td>操作</td>
30             <%
31                 MsgDao md = new MsgDao();
32                 List<Msg> list = md.getMailByReceiver(uname);
33                 for (int i = 0; i < list.size(); i++) {
34             %>
35         
36         <tr>
37             <td><%=list.get(i).getUsername()%></td>
38             <td><%=list.get(i).getTitle()%></td>
39             <td>
40                 <%
41                     if (list.get(i).getState() == 1) {
42                 %> <img src="images/sms_unReaded.png" /> <%
43      } else {
44  %> <img src="images/sms_readed.png" /> <%
45      }
46  %>
47             </td>
48             <td><%=list.get(i).getMsg_create_date()%></td>
49             <td><a href="delete.jsp?id=<%=list.get(i).getMsgid()%>">删除</a></td>
50             <td><a href="write.jsp?reply=<%=list.get(i).getUsername()%>">回复</a>
51             </td>
52             <td><a href="detail.jsp?<%=getMsg(list.get(i))%>">查看</a></td>
53 
54 
55         </tr>
56 
57 
58         <%
59             }
60         %>
61 
62     </table>
63     <%!String getMsg(Msg msg) {
64         return "msgid=" + msg.getMsgid() + "&uname=" + msg.getUsername()
65                 + "&time=" + msg.getMsg_create_date() + "&title="
66                 + msg.getTitle() + "&msgcontent=" + msg.getMsgcontent();
67     }%>
68 
69 
70 
71 
72 
73 </body>
74 </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="receiver"  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>
 1 <%@page import="com.lwk.dao.MsgDao"%>
 2 <%@page import="com.lwk.entity.Msg"%>
 3 <%@page import="com.lwk.dao.UsersDao"%>
 4 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 5 <%
 6     request.setCharacterEncoding("utf-8");
 7     String uname=(String)session.getAttribute("uname");
 8     String sendto=request.getParameter("receiver");
 9     String title=request.getParameter("title");
10     String content=request.getParameter("content");
11     
12     Msg m=new Msg();
13     m.setMsgcontent(content);
14     m.setUsername(uname);
15     m.setSendto(sendto);
16     m.setTitle(title);
17     
18     MsgDao md=new MsgDao();
19     md.addMsg(m);
20     
21     out.print("发送成功.....");
22     response.setHeader("refresh", "5;url=main.jsp");
23     
24     
25  %>
 1 <%@page import="com.lwk.dao.MsgDao"%>
 2 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 3 <%
 4     request.setCharacterEncoding("utf-8");
 5      
 6     int id=Integer.parseInt(request.getParameter("id"));
 7     MsgDao md=new MsgDao();
 8     md.delMail(id);    
 9     response.sendRedirect("main.jsp");
10     
11     
12  %>
 1 <%@page import="com.lwk.dao.MsgDao"%>
 2 <%@page import="com.lwk.entity.Msg"%>
 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 4 <%
 5 request.setCharacterEncoding("UTF-8");
 6 response.setCharacterEncoding("UTF-8");
 7  %>
 8 <!DOCTYPE HTML>
 9 <html>
10 <head>
11 <title>My JSP 'msg.jsp' starting page</title>
12 </head>
13 <%
14     int id = Integer.parseInt(request.getParameter("msgid"));
15     String uname = new String(request.getParameter("uname").getBytes("ISO-8859-1"),"UTF-8");
16     String title = new String(request.getParameter("title").getBytes("ISO-8859-1"),"UTF-8");
17     String time = new String(request.getParameter("time").getBytes("ISO-8859-1"),"UTF-8");
18     String msgcontent = new String(request.getParameter("msgcontent").getBytes("ISO-8859-1"),"UTF-8");
19     MsgDao msgDao = new MsgDao();
20     msgDao.updateStateById(id);
21  %>
22 <body>
23     发件人:<input type="text" name="receiver" value="<%=uname %>" readonly="readonly"><br> 
24     时间:<input type="text" name="title" value="<%=time %>" readonly="readonly"><br> 
25     主题:<input type="text" name="title" value="<%=title %>" readonly="readonly"><br> 
26     内容:<textarea rows="6" cols="20" name="content" readonly="readonly"><%=msgcontent %></textarea><br>
27     <a href="main.jsp">返回</a>
28 </body>
29 </html>

 

 

 

 

posted @ 2022-05-22 13:00  Lwk36  阅读(8)  评论(0编辑  收藏  举报