第十次上机作业

1.用户/邮件(entity包)

package entity;

public class emailuser {
    int uid;
    String uname;
    String upwd;
    public int getId() {
        return uid;
    }
    public void setId(int uid) {
        this.uid = uid;
    }
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getUpwd() {
        return upwd;
    }
    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }

}
package entity;

import java.util.Date;

public class email {
    private int eid;
    private String sendername;//发送者
    private String addresseename;//接收者
    private String title;
    private String content;
    Date totime;
    private int state;
    
    public int getEid() {
        return eid;
    }
    public void setEid(int eid) {
        this.eid = eid;
    }
    public String getSendername() {
        return sendername;
    }
    public void setSendername(String sendername) {
        this.sendername = sendername;
    }
    public String getAddresseename() {
        return addresseename;
    }
    public void setAddresseename(String addresseename) {
        this.addresseename = addresseename;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public Date getTotime() {
        return totime;
    }
    public void setTotime(Date totime) {
        this.totime = totime;
    }
    public int getState() {
        return state;
    }
    public void setState(int state) {
        this.state = state;
    }
    
    
}

2.连接数据库(dao包)

package dao;

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

public class BaseDao {

    //获取连接
    protected Connection getConnection(){
        Connection conn=null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                // 2.建立连接
                conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/emails", "root", "001010Chen");
            } catch (Exception e) {
                e.printStackTrace();
            } 
            return conn;
    }    
    
    //关闭连接
    protected void closeAll(Connection con,PreparedStatement ps,ResultSet rs){        
    try {
        if(rs != null)
            rs.close();
        if(ps != null)
            ps.close();
        if(con != null)
            con.close();        
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
}

用户.java(实现用户登录/注册的方法)

package dao;

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

public class UserDao extends BaseDao {
    //用户登录验证
    public boolean login(String uname, String upwd) {
        boolean f = false;
        Connection conn = getConnection();
        String sql = "select * from emailuser where uname=? and upwd=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, upwd);
            rs = ps.executeQuery();
            if (rs.next())
                f = true;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, rs);
        }
        return f;
    }
    //用户注册信息保存到数据库
    public void reg(String uname, String upwd) {
        Connection conn = getConnection();
        PreparedStatement ps = null;
        try {
            String sql = "insert into emailuser(uname,upwd) values(?,?)"; // 2个占位符
            // 4.执行SQL语句
            ps = conn.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, upwd);
            ps.executeUpdate();// 增删改都用这个
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }

    }

}

邮件.java(实现对邮件增删改查的方法)

package 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 entity.email;

public class EmlDao extends BaseDao {

    // 插入邮件
    public void addEml(email m) {
        Connection conn = getConnection();
        String sql = "insert into email(sendername,title,content,addresseename,totime,state) values(?,?,?,?,?,?)";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, m.getSendername());
            ps.setString(2, m.getTitle());
            ps.setString(3, m.getContent());
            ps.setString(4, m.getAddresseename());
            ps.setDate(5, new java.sql.Date(new Date().getTime()));// 系统当前时间
            ps.setInt(6, 1);
            ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }
    }
    
    // 删除邮件
    public void delEml(int eid) {
        Connection conn = getConnection();
        String sql = "delete from email where eid=?";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, eid);
            ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }
    }

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

    // 按照接收者查询全部邮件
    public List<email> getEmailByAddressee(String uname) {
        List<email> list = new ArrayList<email>();
        Connection conn = getConnection();
        String sql = "select * from email where addresseename=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, uname);
            rs = ps.executeQuery();
            while (rs.next()) {
                email m = new email();
                m.setEid(rs.getInt(1));
                m.setSendername(rs.getString(2));
                m.setTitle(rs.getString(3));
                m.setContent(rs.getString(4));
                m.setAddresseename(rs.getString(5));
                m.setTotime(rs.getDate(6));
                m.setState(rs.getInt(7));
                list.add(m);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, rs);
        }
        return list;
    }
    
    //查看邮件具体信息
    public email showEml(int eid) {
        Connection con = getConnection();
        String sql = "select eid,sendername,title,content,addresseename,totime from email where eid=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setInt(1, eid);
            rs = ps.executeQuery();
            while (rs.next()) {
                email m = new email();
                m.setEid(rs.getInt("eid"));
                m.setSendername(rs.getString("sendername"));
                m.setTitle(rs.getString("title"));
                m.setContent(rs.getString("content"));
                m.setAddresseename(rs.getString("addresseename"));
                m.setTotime(rs.getDate("totime"));
                return m;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, ps, rs);
        }
        return null;
    }

}

3.(登录/注册的写过,就忽略了)主页,显示用户邮箱的邮件

<%@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>

登录邮箱(以anny为例)

登录成功,进入主页,显示用户邮箱的邮件

3.1 查看邮件(点击邮件主题,查看邮件具体信息)

<%@page import="entity.email"%>
<%@page import="dao.EmlDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>查看邮件界面</title>
</head>
<body>
    <%
        request.setCharacterEncoding("utf-8");
        
        String eid = request.getParameter("eid");
        int id = Integer.parseInt(eid);
        EmlDao ed = new EmlDao();
        ed.updateEml(id);
        email m=ed.showEml(id);    
    %>
    <h2>查看邮件</h2>
    <table>
        <tr>
            <td>发件人:</td>
            <td><input type="text" name="sendername" value="<%=m.getSendername()%>"></td>
        </tr>
        <tr>
            <td>发件时间:</td>
            <td><input type="text" name="totime" value="<%=m.getTotime()%>"></td>
        </tr>
        <tr>
            <td>主题:</td>
            <td><input type="text" name="title"    value="<%=m.getTitle()%>"></td>
        </tr>
        <tr>
            <td>内容:</td>
            <td><input type="text" name="content" value="<%=m.getContent()%>"></td>
        </tr>
    </table>

    <a href="main.jsp">返回</a>
</body>
</html>

 

返回首页时,邮件状态由未读变为已读(邮件图标改变)

 

 

 3.2 回复邮件,并发送,在数据库中查看一下是否发送成功

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>    
    <title>写邮件</title>
  </head>  
  <body>
  <h2>写邮件</h2>
  <form action="dowrite.jsp">
  收件人:<input name="addresseename" type="text" size="28" value="<%=request.getParameter("reply")%>"><br>
  主题:<input name="title" type="text" size="30"><br>
  内容:<br>
  <textarea name="content" rows="10" cols="34"></textarea><br>
  <input type="submit" value="发送邮件">
  <p>
  <a href="main.jsp">返回</a>
  </p>
  </form>
  
  </body>
</html>
<%@page import="entity.email"%>
<%@page import="dao.EmlDao"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

<html>
<head>
<title></title>
</head>
<body>
    <%
    request.setCharacterEncoding("utf-8");
    String sendername=(String)session.getAttribute("uname");
    String addresseename=request.getParameter("addresseename");
    String title=request.getParameter("title");
    String content=request.getParameter("content");
    
    email m=new email();
    m.setSendername(sendername);
    m.setAddresseename(addresseename);
    m.setTitle(title);
    m.setContent(content);
    
    EmlDao ed=new EmlDao();
    ed.addEml(m);
    
    out.print("发送成功,3秒后返回首页");
    response.setHeader("refresh", "3;url=main.jsp");    
 %>
</body>
</html>

 

3.3 删除邮件

<%@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>

 

posted @ 2022-05-18 18:05  小猪哏哏  阅读(21)  评论(0编辑  收藏  举报