第十二周作业

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

<%@page import="entity.email"%>
<%@page import="dao.EmlDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>主界面</title>
</head>
<body>
    <%
        String uname = (String) session.getAttribute("uname");
    %>
    <h2>首页!!欢迎你<%=uname%></h2>
    <table border="1">
        <tr>
            <th>发件人</th>
            <th>主题</th>
            <th>状态</th>
            <th>时间</th>
            <th>操作</th>

            <%
                EmlDao md = new EmlDao();
                List<email> list = md.getEmailByAddressee(uname);
                
                for (int i = 0; i < list.size(); i++) {
            %>
            <tr>
            <td><%=list.get(i).getSendername()%></td>
            <td><a href="show.jsp?eid=<%=list.get(i).getEid() %>"><%=list.get(i).getTitle() %></a></td>
            <td>
                <%
                    if (list.get(i).getState() == 1) {
                %> <img src="images/sms_unReaded.png" /> <%
                     } else {
                %> <img src="images/sms_readed.png" /> <%
                     }
                %>
            </td>
            <td><%=list.get(i).getTotime()%></td>
            <td><a href="delete.jsp?eid=<%=list.get(i).getEid()%>">删除</a></td>
            <td><a href="write.jsp?reply=<%=list.get(i).getSendername()%>">回复</a></td>            
            </tr>
            <%
                }
            %>                  
    </table>
    <br>
    <a href="write.jsp">新建邮件</a>    
</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>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.qmt.dao.UsersDao"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'dologin.jsp' starting page</title>

</head>

<body>
    <%
        request.setCharacterEncoding("utf-8");
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        UsersDao ud = new UsersDao();
        if (ud.login(uname, upwd)) {
            session.setAttribute("uname",uname);
            request.getRequestDispatcher("main.jsp").forward(
                    request,response);
        } else {
            out.print("登录失败,即将跳回登录页......");
            response.setHeader("refresh", "3;url=login.jsp");
        }
    %>
</body>
</html>
<%@page import="dao.EmlDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>删除邮件</title>
</head>
<body>
    <%
        request.setCharacterEncoding("utf-8");

        int eid = Integer.parseInt(request.getParameter("eid"));
        EmlDao ed = new EmlDao();
        ed.delEml(eid);
    %>
    <script type="text/javascript">
        alert("删除成功!");
    </script>
    <%
        response.sendRedirect("main.jsp");
    %>
</body>
</html>

 

 
package com.myh.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 com.myh.entity.Msg;



public class MsgDao extends BaseDao {
    // 1,插入邮件
    public void addMsg(Msg m) {
        Connection con = getConnection();
        String sql = "insert into msg(username,title,msgcontent,state,sendto,msg_create_date) values(?,?,?,?,?,?)";
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, m.getUsername());
            ps.setString(2, m.getTitle());
            ps.setString(3, m.getMsgcontent());
            ps.setInt(4, 1);
            ps.setString(5, m.getSendto());
            ps.setDate(6, new java.sql.Date(new Date().getTime()));// 系统当前时间
            ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(con, ps, null);
        }

    }

    // 2.删除邮件
    public void delMail(int id) {
        Connection conn = getConnection();
        String sql = "delete from msg where msgid=?";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            closeAll(conn, ps, null);
        }

    }
     //(测试删除邮件的代码是否编写成功)
     //    public static void main(String[] args) {
     //    MsgDao md=new MsgDao();
    //    md.delMail(3);
    //}


    // 3.修改邮件状态
    public void update(int id) {
        Connection con = getConnection();
        String sql = "update  msg set state='1' where msgid=?";
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, ps, null);
        }
    }

    // 4.按照接收者查询全部邮件
    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.setMsgcontent(rs.getString("msgcontent"));
                m.setState(rs.getInt("state"));
                m.setSendto(rs.getString("sendto"));
                m.setMsg_create_date(rs.getDate("msg_create_date"));
                list.add(m);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            closeAll(con, ps, rs);
        }
        return list;

    }
    //5.实现阅读邮件功能
    public Msg read(int id) {
        Connection con = getConnection();
        String sql = "select msgid,username,sendto,title,msgcontent,msg_create_date from msg where msgid=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setInt(1, id);
            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.setMsgcontent(rs.getString("msgcontent"));
                m.setSendto(rs.getString("sendto"));
                m.setMsg_create_date(rs.getDate("msg_create_date"));
                return m;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, ps, rs);
        }
        return null;
    }


}
<%@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>
package com.myh.dao;

import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UsersDao extends BaseDao {

    // 关于用户的增删改查

    // 1.登录
    public boolean login(String uname, String upwd) {
        boolean f = false;
        Connection con = getConnection();
        String sql = "select * from users where uname=? and upwd=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, upwd);
            rs = ps.executeQuery();
            if (rs.next())
                f = true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, ps, rs);
        }
        return f;
    }

    // 2.注册
    public int register(String uname, String upwd) {
        Connection con = getConnection();
        PreparedStatement ps = null;
        int x = 0;
        try {
            String sql = "insert into users(uname,upwd) values(?,?)";
            ps = con.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, upwd);
            x = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, ps, null);
        }
        return x;
    }
}

 


 

 

 

 

 

 

 


 

 
posted @ 2022-05-22 09:44  马宇航1  阅读(8)  评论(0编辑  收藏  举报