JSP第十二周作业

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

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();
        }
    }
    
}
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;
    }

}
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);
        }

    }

}
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;
    }
    
    
}
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;
    }

}
<%@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>
<%@page import="dao.UserDao"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

<html>
<head>
<title>判断</title>
</head>
<body>
    <%
        request.setCharacterEncoding("utf-8");
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");

        UserDao ud = new UserDao();
        if (ud.login(uname, upwd)) {
    %>
    <script type="text/javascript">
        alert("登录成功!");
    </script>
    <%
        session.setAttribute("uname", uname);
            request.getRequestDispatcher("main.jsp").forward(request,
                    response);
        } else {
    %>
    <script type="text/javascript">
        alert("登录失败,3秒后返回登录界面");
    </script>
    <%
        response.setHeader("refresh", "3;url=login.jsp");
        }
    %>
</body>
</html>
<%@page import="dao.UserDao"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>获取</title>
</head>
<body>
<%
    String uname=request.getParameter("uname");
    String upwd=request.getParameter("upwd");
    String upwd1=request.getParameter("upwd1");

    if (upwd.equals(upwd1)) {
        UserDao sd = new UserDao();
        sd.reg(uname, upwd);
%>
<script type="text/javascript">alert("注册成功!");</script>
<%
        request.getRequestDispatcher("login.jsp").forward(request,response);
    } else {
%>
<script type="text/javascript">alert("注册失败,请重试!");</script>
<%
        request.getRequestDispatcher("reg.jsp").forward(request,response);
    }
%>
    
</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>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>登录页面</title>
<script type="text/javascript">
    function check() {
        if (loginForm.uname.value == "") {
            alert("请输入账号!");
            return;
        } else if (loginForm.upwd.value == "") {
            alert("请输入密码!");
            return;
        }
        loginForm.submit();
    }
</script>
</head>
<body>
    <form name="loginForm" action="dologin.jsp" method="post">
        用户:<input type="text" name="uname"><br> 
        密码:<input type="password" name="upwd"><br> 
        <input type="button" value="登录" onClick="check()"> 
        <a href="reg.jsp">立即注册账户</a>
    </form>
</body>
</html>
<%@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.*" pageEncoding="utf-8"%>
<html>
<head>
<title>注册页面</title>
</head>
<body>
    <form name="regForm" action="doreg.jsp" method="post">
        用户:<input type="text" name="uname"><br> 
        密码:<input type="password" name="upwd" ><br> 
        确认密码:<input type="password" name="upwd1" ><br> 
        <input type="submit" value="注册" > 
        <a href="login.jsp">已有账号?前往登录</a>
    </form>
</body>
</html>
<%@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>
<%@page import="entity.email"%>
<%@page import="dao.EmlDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>修改界面</title>
</head>
<body>

</body>
</html>
<%@ 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>

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2022-05-20 14:53  Wowbubble  阅读(10)  评论(0编辑  收藏  举报