JSP第十三周作业

public class mail {
 
    private Integer id;
    private String Sender;
    private String addressee;
    private String title;
    private String content;
    private String time;
    private Integer state;
 
    public Integer getId() {
        return id;
    }
 
    public void setId(Integer id) {
        this.id = id;
    }
 
    public String getSender() {
        return Sender;
    }
 
    public void setSender(String sender) {
        Sender = sender;
    }
 
    public String getAddressee() {
        return addressee;
    }
 
    public void setAddressee(String addressee) {
        this.addressee = addressee;
    }
 
    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 String getTime() {
        return time;
    }
 
    public void setTime(String time) {
        this.time = time;
    }
 
    public Integer getState() {
        return state;
    }
 
    public void setState(Integer state) {
        this.state = state;
    }
 
    @Override
    public String toString() {
        return "mail{" +
                "id=" + id +
                ", Sender='" + Sender + '\'' +
                ", addressee='" + addressee + '\'' +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", time='" + time + '\'' +
                ", state=" + state +
                '}';
    }
}

  

public class MailUsers {
    private int id;
    private String username;
    private String password;
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    @Override
    public String toString() {
        return "MailUsers{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

  

import java.sql.*;
 
public class mail_BaseDao {
    protected Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/mailbox", "root", "123456");
        } 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();
        }
    }
}

  

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class mail_UserDao extends mail_BaseDao{
    //--------------------用户登录---------------------
    public boolean login(String name, String pwd) {
        boolean f = false;
        Connection conn = getConnection();
        String sql = "select * from tb_user where username=? and password=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, pwd);
            rs = ps.executeQuery();
            if (rs.next())
                f = true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, rs);
        }
        return f;
    }
 
    //--------------------用户注册--------------------
    public void reg(String username, String password) {
        Connection conn = getConnection();
        PreparedStatement ps = null;
        try {
            String sql = "insert into tb_user(username,password) values(?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }
 
    }
}

  

import com.own.mywebdemo.Week5_Login_connecor.BaseDao;
import org.jetbrains.annotations.NotNull;
 
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;
 
public class MailDao extends mail_BaseDao {
    //--------------------查询所有邮件--------------------
    public List<mail> getMailByReceiver(String name){
        List<mail> list=new ArrayList<mail>();
        Connection conn=getConnection();
        String sql="select * from tb_mail where addressee=?";
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            rs=ps.executeQuery();
            while(rs.next()){
                mail m=new mail();
                m.setId(rs.getInt(1));
                m.setSender(rs.getString(2));
                m.setAddressee(rs.getString(3));
                m.setTitle(rs.getString(4));
                m.setContent(rs.getString(5));
                m.setTime(rs.getString(6));
                m.setState(rs.getInt(7));
                list.add(m);
            }
 
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            closeAll(conn, ps, rs);
        }
        return list;
    }
 
    // --------------------插入邮件--------------------
    public void addMail(mail m) {
        Connection conn = getConnection();
        String sql = "insert into tb_mail(Sender,addressee,title,content,time,state) values(?,?,?,?,?,?)";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, m.getSender());
            ps.setString(2, m.getAddressee());
            ps.setString(3, m.getTitle());
            ps.setString(4, m.getContent());
            ps.setDate(5, new java.sql.Date(new Date().getTime()));// 系统当前时间
            ps.setInt(6, 0);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }
    }
 
    // --------------------删除邮件--------------------
    public void deleteMail(int id) {
        Connection conn = getConnection();
        String sql = "delete from tb_mail where id=?";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            closeAll(conn, ps, null);
        }
 
    }
 
    //--------------------更新邮件状态--------------------
    public void updataMail(int id){
        Connection conn = getConnection();
        String sql = "update tb_mail set state=1 where id=?";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            closeAll(conn, ps, null);
        }
    }
 
}

  

import com.own.mywebdemo.Week5_Login_connecor.BaseDao;
import org.jetbrains.annotations.NotNull;
 
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;
 
public class MailDao extends mail_BaseDao {
    //--------------------查询所有邮件--------------------
    public List<mail> getMailByReceiver(String name){
        List<mail> list=new ArrayList<mail>();
        Connection conn=getConnection();
        String sql="select * from tb_mail where addressee=?";
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            rs=ps.executeQuery();
            while(rs.next()){
                mail m=new mail();
                m.setId(rs.getInt(1));
                m.setSender(rs.getString(2));
                m.setAddressee(rs.getString(3));
                m.setTitle(rs.getString(4));
                m.setContent(rs.getString(5));
                m.setTime(rs.getString(6));
                m.setState(rs.getInt(7));
                list.add(m);
            }
 
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            closeAll(conn, ps, rs);
        }
        return list;
    }
 
    // --------------------插入邮件--------------------
    public void addMail(mail m) {
        Connection conn = getConnection();
        String sql = "insert into tb_mail(Sender,addressee,title,content,time,state) values(?,?,?,?,?,?)";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, m.getSender());
            ps.setString(2, m.getAddressee());
            ps.setString(3, m.getTitle());
            ps.setString(4, m.getContent());
            ps.setDate(5, new java.sql.Date(new Date().getTime()));// 系统当前时间
            ps.setInt(6, 0);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }
    }
 
    // --------------------删除邮件--------------------
    public void deleteMail(int id) {
        Connection conn = getConnection();
        String sql = "delete from tb_mail where id=?";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            closeAll(conn, ps, null);
        }
 
    }
 
    //--------------------更新邮件状态--------------------
    public void updataMail(int id){
        Connection conn = getConnection();
        String sql = "update tb_mail set state=1 where id=?";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            closeAll(conn, ps, null);
        }
    }
 
}

  

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String account=request.getParameter("account");
    String psw=request.getParameter("psw");
    mail_UserDao mail_userDao = new mail_UserDao();
    if (mail_userDao.login(account,psw)){
        session.setAttribute("account",account);
        request.getRequestDispatcher("LogSuccess.jsp").forward(request,response);
    }else {
        request.getRequestDispatcher("LogFail.jsp").forward(request,response);
    }
%>
</body>
</html>

  

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注册</title>
</head>
<body>
<script type="text/javascript">
    function check() {
        if (login.account.value==""){
            alert("请输入账号!");
            return;
        }else if(login.psw.value=="") {
            alert("请输入密码!");
            return;
        }else if(login.psw1.value=="") {
            alert("请再次输入密码!");
            return;
        }
        var pwd = document.getElementById("pwd").value;
        var pwd1 = document.getElementById("pwd1").value;
        if (pwd==pwd1){
            alert("注册成功!");
            login.submit();
        }else {
            alert("两次输入密码不一样!");
            return;
        }
    }
</script>
<form action="checkReg.jsp" method="post" name="login">
    请输入账号:<input type="text" name="account"><br>
    请输入密码:<input type="password" name="psw" id="pwd"><br>
    请再次输入密码:<input type="password" name="psw1" id="pwd1"><br>
    <input type="button" value="注册" onclick="check()">
</form>
<form action="Login.jsp" method="post">
    <input type="submit" value="已有账号?前往登录">
</form>
</body>
</html>

  

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String account=request.getParameter("account");
    String psw=request.getParameter("psw");
    String psw1=request.getParameter("psw1");
    if (psw.equals(psw1)) {
        mail_UserDao mail_userDao = new mail_UserDao();
        mail_userDao.reg(account,psw);
        request.getRequestDispatcher("Login.jsp").forward(request,response);
    } else {
        request.getRequestDispatcher("Register.jsp").forward(request,response);
    }
%>
</body>
</html>

  

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登陆失败</title>
</head>
<body>
<h1>登陆失败</h1>
</body>
</html>

  

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>我的邮件</title>
</head>
<body>
<%
    String account = (String) session.getAttribute("account");
    MailDao mailDao = new MailDao();
    List<mail> mails = mailDao.getMailByReceiver(account);
%>
<h1>欢迎!<%=account%>
</h1>
<form action="write.jsp" method="post">
    <input type="submit" value="写邮件">
</form>
<table border="1">
    <tr>
        <td>发件人</td>
        <td>标题</td>
        <td>状态</td>
        <td> </td>
        <td> </td>
        <td> </td>
        <td>时间</td>
    </tr>
    <%
        for (int i = 0; i < mails.size(); i++) {
            mail m = mails.get(i);
    %>
    <tr>
        <td><%out.print(m.getSender());%></td>
        <td><%out.print(m.getTitle());%></td>
        <td><%
            if (m.getState() == 1) {
                out.print("已读");
            } else {
                out.print("未读");
            }
        %></td>
        <td><a href="DetailMail.jsp?sender=<%=m.getSender()%>&title=<%=m.getTitle()%>&content=<%=m.getContent()%>&time=<%=m.getTime()%>&mailId=<%=m.getId()%>">查看</a></td>
        <td><a href="write.jsp?reply=<%=m.getSender()%>">回复</a></td>
        <td><a href="DeleteMail.jsp?mailId=<%=m.getId()%>">删除</a></td>
        <td><%out.print(m.getTime());%></td>
    </tr>
    <%}%>
</table>
</body>
</html>

  

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>写邮件</title>
</head>
<body>
<%
    String account = (String) session.getAttribute("account");
%>
<h1>欢迎!<%=account%></h1>
<form action="checkWrite.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>

  

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        String sender = (String) session.getAttribute("account");
        String addressee=request.getParameter("receiver");
        String title=request.getParameter("title");
        String content=request.getParameter("content");
        mail m = new mail();
        m.setSender(sender);
        m.setAddressee(addressee);
        m.setTitle(title);
        m.setContent(content);
 
        MailDao md = new MailDao();
        md.addMail(m);
 
        out.print("发送成功!3秒后返回");
        response.setHeader("refresh", "3;url=LogSuccess.jsp");
    %>
</body>
</html>

  

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>邮件内容</title>
</head>
<body>
<%
    int mailId=Integer.parseInt(request.getParameter("mailId"));
    String sender = request.getParameter("sender");
    String title = request.getParameter("title");
    String content = request.getParameter("content");
    String time = request.getParameter("time");
    MailDao md = new MailDao();
    md.updataMail(mailId);
%>
 
发件人:<%out.print(sender);%><br>
主题:<%out.print(title);%><br>
内容:<%out.print(content);%><br>
时间:<%out.print(time);%><br>
 
<a href="LogSuccess.jsp">返回主界面</a>
</body>
</html>
复制代码
复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    int mailId=Integer.parseInt(request.getParameter("mailId"));
    MailDao md = new MailDao();
    md.deleteMail(mailId);
    response.sendRedirect("LogSuccess.jsp");
%>
</body>
</html>

  

 

 

 

 

 

posted @ 2022-05-29 12:26  兰佳淇  阅读(4)  评论(0编辑  收藏  举报