jsp第七次作业

<body>
    <form action="dologin.jsp" method="post">
        用户名:<input type="text" name="uname" value="kitty" /><Br> 密码 :<input
            type="password" name="upwd" value="777" /><br> <input
            type="submit" value="登录">

    </form>
</body>

index.jsp
<%
    request.setCharacterEncoding("utf-8");
    String uname = request.getParameter("uname");
    String upwd = request.getParameter("upwd");
    UsersDao a = new UsersDao();
    if (a.login(uname, upwd)) {
        request.getRequestDispatcher("main.jsp").forward(request, response);
    } else {
        response.sendRedirect("index.jsp");
    }
%>

dologin.jsp
<body>
    <%
        request.setCharacterEncoding("utf-8");
        String uname = request.getParameter("uname");
        session.setAttribute("uname", uname);
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //加载驱动
            String url = "jdbc:mysql://localhost:3306/users";
            String user = "root";
            String password = "root";
            conn = DriverManager.getConnection(url, user, password);
            // 连接对象
            //conn = BaseDao.getConnection();
    %>
    <table align="center">
        <tr>
            <td>username &nbsp;&nbsp;&nbsp;</td>
            <td>title&nbsp;&nbsp;&nbsp;</td>
            <td>msgcontent&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
            <td>state&nbsp;&nbsp;&nbsp;</td>
            <td>sendto&nbsp;&nbsp;&nbsp;</td>
            <td>msg_create_date</td>
        </tr>
        <%
            Msg msg = new Msg();
                ps = conn.prepareStatement("select * from msg where username=?");
                ps.setString(1, uname);
                rs = ps.executeQuery();
                while (rs.next()) {
        %>
        <tr>
            <td><%=rs.getString("username")%></td>
            <td><%=rs.getString("title")%></td>
            <td><%=rs.getString("msgcontent")%></td>

            <td>
                <%
                    if (rs.getString("state").equals("1")) {
                %> <%
     out.print("未读");
 %> <%
     } else {
 %> <%
     out.print("已读");
 %> <%
     }
 %>
            </td>
            <td><%=rs.getString("sendto")%></td>
            <td><%=rs.getString("msg_create_date")%></td>
        </tr>
        <%
            }
        %>
    </table>
    <form action="content.jsp" method="post">
        按标题查询:<input type="text" name="contitle"> <input type="submit"
            value="查询">
    </form>
    <br>

    <%
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //BaseDao.closeAll(conn, ps, rs);
            try {
                if (rs != null) {
                    rs.close();
                }
            } finally {
                try {
                    if (ps != null) {
                        ps.close();
                    }
                } finally {
                    if (conn != null) {
                        conn.close();
                    }
                }
            }
        }
    %>

</body>

main.jsp
<body>
    <%
        request.setCharacterEncoding("utf-8");
        String uname = (String) session.getAttribute("uname");
        String newstate = null;
        //获取邮件列表页面输入的标题
        String contitle = request.getParameter("contitle");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //加载驱动
            String url = "jdbc:mysql://localhost:3306/users";
            String user = "root";
            String password = "root";
            conn = DriverManager.getConnection(url, user, password);
            // 连接对象
            //conn = BaseDao.getConnection();
    %>
    <%
        Msg msg = new Msg();
            ps = conn.prepareStatement("select * from msg where title=?and username=?");
            ps.setString(1, contitle);
            ps.setString(2, uname);
            rs = ps.executeQuery();
            while (rs.next()) {
    %>

    <form action="" name="huifu" method="post">
        sendto &nbsp;<%=rs.getString("sendto")%><br> <br>标题:<%=rs.getString("title")%><br>
        <br> 正文:<%=rs.getString("msgcontent")%><br>
        <%
            //更新阅读状态
                    if (rs.getInt("state") == 1) {
                        ps = conn.prepareStatement("update msg set state=? where state=? and title=?");
                        // 执行sql语句
                        ps.setInt(1, 0);
                        ps.setInt(2, 1);
                        ps.setString(3, contitle);
                        ps.executeUpdate();

                        break;
                    }
        %>
        <%
            }
        %>



    </form>
    <form action="insert.jsp" method="post">
        <%
            session.setAttribute("uuname", uname);
            session.setAttribute("newcontitle", contitle);
        %><input type="submit" value="回复">&nbsp;
    </form>
    <form action="main2.jsp" method="post">
        <%
            session.setAttribute("uuname", uname);
        %>
        <input type="submit" value="返回邮件列表" name="newyjlb">
    </form>
    <form action="delete.jsp" method="post">
        <%
            session.setAttribute("uuname", uname);
                session.setAttribute("newcontitle", contitle);
        %>
        <input type="submit" value="删除邮件" name="deleteyj">
    </form>



    <%
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //BaseDao.closeAll(conn, ps, rs);
            try {
                if (rs != null) {
                    rs.close();
                }
            } finally {
                try {
                    if (ps != null) {
                        ps.close();
                    }
                } finally {
                    if (conn != null) {
                        conn.close();
                    }
                }
            }
        }
    %>

</body>

content
<body>
    <%
        request.setCharacterEncoding("utf-8");
        String uuname = (String) session.getAttribute("uuname");
        //获取邮件列表页面输入的标题
        String contitle = (String) session.getAttribute("newcontitle");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //加载驱动
            String url = "jdbc:mysql://localhost:3306/users";
            String user = "root";
            String password = "root";
            conn = DriverManager.getConnection(url, user, password);
            // 连接对象
            //conn = BaseDao.getConnection();
    %>
    <%
        Msg msg = new Msg();
            ps = conn.prepareStatement("select * from msg where title=?and username=?");
            ps.setString(1, contitle);
            ps.setString(2, uuname);
            rs = ps.executeQuery();
            while (rs.next()) {
    %>
    <form action="insert2.jsp" name="huifu" method="post">
        sendto &nbsp;<%=rs.getString("sendto")%><br> <br> 
        
        标题:<input type="text" name="ctitle"><br> <br> 
        
        正文:<input type="text" name="ccontent"><br>
        <input type="submit" value="发送">
    </form>
    <%
        }
    %>
    <%
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //BaseDao.closeAll(conn, ps, rs);
            try {
                if (rs != null) {
                    rs.close();
                }
            } finally {
                try {
                    if (ps != null) {
                        ps.close();
                    }
                } finally {
                    if (conn != null) {
                        conn.close();
                    }
                }
            }
        }
    %>




    <br>
</body>

insert.jsp
<body>
    <%
        request.setCharacterEncoding("utf-8");
        String uuname = (String) session.getAttribute("uuname");
        String ctitle = request.getParameter("ctitle");
        String ccontent = request.getParameter("ccontent");
        //获取邮件列表页面输入的标题
        String contitle = (String) session.getAttribute("newcontitle");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //加载驱动
            String url = "jdbc:mysql://localhost:3306/users";
            String user = "root";
            String password = "root";
            conn = DriverManager.getConnection(url, user, password);
            // 连接对象
            //conn = BaseDao.getConnection();
    %>
    <%
        Msg msg = new Msg();
            ps = conn.prepareStatement("select * from msg where title=?and username=?");
            ps.setString(1, contitle);
            ps.setString(2, uuname);
            rs = ps.executeQuery();
            while (rs.next()) {
            ps = conn.prepareStatement("insert into msg(username,title,msgcontent,sendto,state,msg_create_date)" + "values('"
                    + rs.getString("sendto") + "','" + ctitle + "','" + ccontent + "','"
                    + uuname + "','" + "1" + "','"+"2020-04-27"+"')");
                    ps.executeUpdate();
                    request.getRequestDispatcher("index.jsp").forward(request, response);
    %>
    <%
        }
    %>
    <%
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //BaseDao.closeAll(conn, ps, rs);
            try {
                if (rs != null) {
                    rs.close();
                }
            } finally {
                try {
                    if (ps != null) {
                        ps.close();
                    }
                } finally {
                    if (conn != null) {
                        conn.close();
                    }
                }
            }
        }
    %>




    <br>
</body>

insert2.jsp
<body>
    <%
        request.setCharacterEncoding("utf-8");
        String uuname = (String) session.getAttribute("uuname");
        //获取邮件列表页面输入的标题
        //从content.jsp session传过来的
        String contitle = (String) session.getAttribute("newcontitle");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //加载驱动
            String url = "jdbc:mysql://localhost:3306/users";
            String user = "root";
            String password = "root";
            conn = DriverManager.getConnection(url, user, password);
            // 连接对象
            //conn = BaseDao.getConnection();
    %>
    <table align="center">
        <tr>
            <td>username &nbsp;&nbsp;&nbsp;</td>
            <td>title&nbsp;&nbsp;&nbsp;</td>
            <td>msgcontent&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
            <td>state&nbsp;&nbsp;&nbsp;</td>
            <td>sendto&nbsp;&nbsp;&nbsp;</td>
            <td>msg_create_date</td>
        </tr>
        <%
            Msg msg = new Msg();
                ps = conn.prepareStatement("delete from msg where title=?and username=?");
                // 执行sql语句
                ps.setString(1, contitle);
                ps.setString(2, uuname);
                ps.executeUpdate();
                ps = conn.prepareStatement("select * from msg where username=?");
                ps.setString(1, uuname);
                rs = ps.executeQuery();
                while (rs.next()) {
        %>
        <tr>
            <td><%=rs.getString("username")%></td>
            <td><%=rs.getString("title")%></td>
            <td><%=rs.getString("msgcontent")%></td>

            <td>
                <%
                    if (rs.getString("state").equals("1")) {
                %> <%
     out.print("未读");
 %> <%
     } else {
 %> <%
     out.print("已读");
 %> <%
     }
 %>
            </td>
            <td><%=rs.getString("sendto")%></td>
            <td><%=rs.getString("msg_create_date")%></td>
        </tr>
        <%
            }
        %>
    </table>
    <%
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //BaseDao.closeAll(conn, ps, rs);
            try {
                if (rs != null) {
                    rs.close();
                }
            } finally {
                try {
                    if (ps != null) {
                        ps.close();
                    }
                } finally {
                    if (conn != null) {
                        conn.close();
                    }
                }
            }
        }
    %>
    <br>
</body>

delete.jsp
<body>
    <%
        //查询邮件列表
        request.setCharacterEncoding("utf-8");
        String uuname = (String) session.getAttribute("uuname");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //加载驱动
            String url = "jdbc:mysql://localhost:3306/users";
            String user = "root";
            String password = "root";
            conn = DriverManager.getConnection(url, user, password);
            // 连接对象
            //conn = BaseDao.getConnection();
    %>
    <table align="center">
        <tr>
            <td>username &nbsp;&nbsp;&nbsp;</td>
            <td>title&nbsp;&nbsp;&nbsp;</td>
            <td>msgcontent&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
            <td>state&nbsp;&nbsp;&nbsp;</td>
            <td>sendto&nbsp;&nbsp;&nbsp;</td>
            <td>msg_create_date</td>
        </tr>
        <%
        //查询语句(返回的邮件列表)
            Msg msg = new Msg();
                ps = conn.prepareStatement("select * from msg where username=?");
                ps.setString(1, uuname);
                rs = ps.executeQuery();
                while (rs.next()) {
        %>
        <!-- 把输入内容放在table里 -->
        <tr>
            <td><%=rs.getString("username")%></td>
            <td><%=rs.getString("title")%></td>
            <td><%=rs.getString("msgcontent")%></td>

            <td>
                <%
                //判断是否读取“1”为未读“0”已读
                    if (rs.getString("state").equals("1")) {
                %> <%
     out.print("未读");
 %> <%
     } else {
 %> <%
     out.print("已读");
 %> <%
     }
 %>
            </td>
            <td><%=rs.getString("sendto")%></td>
            <td><%=rs.getString("msg_create_date")%></td>
        </tr>
        <%
            }
        %>

    </table>
        <form action="content.jsp" method="post">
        按标题查询:<input type="text" name="contitle"> <input type="submit"
            value="查询">
    </form>
    <br>

    <%
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //BaseDao.closeAll(conn, ps, rs);
            try {
                if (rs != null) {
                    rs.close();
                }
            } finally {
                try {
                    if (ps != null) {
                        ps.close();
                    }
                } finally {
                    if (conn != null) {
                        conn.close();
                    }
                }
            }
        }
    %>

</body>

main2.jsp

 

 

 

 

 

posted @ 2020-05-01 10:42  张子健  阅读(137)  评论(0编辑  收藏  举报