jsp第七次作业

CREATE DATABASE email;
USE email;
CREATE TABLE users(
username VARCHAR(20) PRIMARY KEY,
PASSWORD VARCHAR(20),
email VARCHAR(20)
)
CREATE TABLE `msg` (
  `msgid` INT(11) NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(20) NOT NULL,
  `title` VARCHAR(20) NOT NULL,
  `msgcontent` VARCHAR(100) NOT NULL,
  `state` INT(11) NOT NULL,
  `sendto` VARCHAR(20) NOT NULL,
  `msg_create_date` DATETIME NOT NULL,
  PRIMARY KEY (`msgid`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO users VALUES('zqb','1111','4396@qq.com');
INSERT INTO users VALUES('赵启伯','777','1126@qq.com');INSERT INTO msg(username,title,msgcontent,state,sendto,msg_create_date)
VALUES('zqb','你好','空洞骑士平史低仅需24元,非常优秀的2D横板游戏,喜欢的朋友可以支持一波~',1,'赵启伯','2020-04-24');

INSERT INTO msg(username,title,msgcontent,state,sendto,msg_create_date)
VALUES('zqb','Steam客服','您的Steam账户:来自新电脑的访问',1,'赵启伯','2020-04-24');

INSERT INTO msg(username,title,msgcontent,state,sendto,msg_create_date)
VALUES('zqb','县长宣言','我来鹅城只办三件事,公平,公平,还是他妈的公平',1,'赵启伯','2019-04-24');

INSERT INTO msg(username,title,msgcontent,state,sendto,msg_create_date)
VALUES('赵启伯','收作业','最晚星期五交',1,'zqb','2019-04-24');

index

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
   <form action="dologin.jsp" method="post">
用户名:<input type="text" name="uname" value="zqb" /><Br>
密码 :<input type="password" name="upwd" value="1111"/><br>

<input type="submit" value="登录">

</form>
  </body>
</html>

dologin&main

<%@page import="dao.UsersDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    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("userName", uname);
        request.getRequestDispatcher("main.jsp").forward(request, response);
    }
    else{        
        response.sendRedirect("index.jsp");
        }
%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
<%
 String name=(String)session.getAttribute("userName");
 session.setAttribute("userName", name);
   %>
您好<%=name %>,欢迎您访问邮箱<br>

    <form style="float:left" action="insert.jsp" method="post">
       <input type="submit" value="写信">&nbsp;&nbsp;&nbsp;
    </form>
    <form action="select.jsp" method="post">     
        <input type="submit" value="收信">
    </form>
  </body>
</html>

insert&doinsert

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'insert.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

</head>

<body>
    <form action="doinsert.jsp" method="post">
        收件人:<input name="sendto" type="text" size="150" /><br />
        <br /> &nbsp &nbsp&nbsp&nbsp主题:<input name="title" type="text"
            size="150" /><br />
        <br /> &nbsp &nbsp &nbsp正文:<input name="msgcontent" type="text"
            size="150" /><br /> <input type="submit" value="发布">
    </form>
</body>


</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@page import="java.sql.SQLException"%>
<%@page import="dao.BaseDao"%>
<%@page import="entity.Msg"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>

<%@page import="java.sql.Connection"%>
<!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>doinsert</title>
</head>
<body>
    <%
    request.setCharacterEncoding("utf-8");
    String username=(String)session.getAttribute("userName");  
    String title = request.getParameter("title");
    String msgcontent = request.getParameter("msgcontent");
    int state=0;
    String sendto = request.getParameter("sendto"); 
    String msg_create_date="2020-05-04";      
   try {
            // 1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 2.建立连接
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/email", "root", "ROOT");
            // 3.编写SQL语句
            String sql = "insert into msg(username,title,msgcontent,state,sendto,msg_create_date) values(?,?,?,?,?,?)";            
            PreparedStatement ps = con.prepareStatement(sql);// PS是用来执行SQL语句和给问号赋值的
            ps.setString(1, username);
            ps.setString(2, title);
            ps.setString(3, msgcontent);
            ps.setInt(4, state);
            ps.setString(5, sendto);
            ps.setString(6, msg_create_date);
            
            // 4.执行SQL语句
            ps.executeUpdate();            
            // 执行查询
            // 5.处理查询结果(查询的时候需要)
            // 6.关闭连接
            ps.close();
            con.close();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        

%>
    发送成功
    </br>
    <a href="main.jsp">【返回】 
</body>
</html>

select&show

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@page import="java.sql.SQLException"%>
<%@page import="dao.BaseDao"%>
<%@page import="entity.Msg"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>

<%@page import="java.sql.Connection"%>
<!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 name=(String)session.getAttribute("userName");    
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //加载驱动 

            String url = "jdbc:mysql://localhost:3306/email";
            String user = "root";
            String password = "ROOT";
            conn = DriverManager.getConnection(url, user, password);
            ps = conn.prepareStatement("select * from msg where sendto=?");
            ps.setString(1, name);
            rs = ps.executeQuery();
    %>
    <table border="1">
        <tr>
            <td>编辑</td>
            <td>发件人</td>
            <td>标题</td>
            <td>正文</td>
            <td>发送时间</td>

        </tr>
        <%
            while (rs.next()) {
        %>
        <tr>
            <td>
                <%
                    if (rs.getString("state").equals("1")) {
                       String msgid=rs.getString("msgid");
                       session.setAttribute("msgid", msgid);
                %> <input type="button" value="点击查看"
                onclick="window.location.href='show.jsp';" /> <%
     } else {
 %>
                <div align="center">
                    <%
                        out.print("已查看");
                    %>
                </div> <%
     }
 %>
            </td>
            <td><%=rs.getString("username")%></td>
            <td><%=rs.getString("title")%></td>
            <td><%=rs.getString("msgcontent")%></td>
            <td><%=rs.getString("msg_create_date")%></td>
        </tr>
        <%
            }
        %>
    </table>
    <br>

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

</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@page import="java.sql.SQLException"%>
<%@page import="dao.BaseDao"%>
<%@page import="entity.Msg"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>

<%@page import="java.sql.Connection"%>
<!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>show</title>
</head>
<body>
    <%
        request.setCharacterEncoding("utf-8");
        String msgid = (String) session.getAttribute("msgid");
        int id = Integer.valueOf(msgid);
        try {
            // 1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 2.建立连接
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/email", "root", "ROOT");
            // 3.编写SQL语句
            String sql1 = "select * from msg where msgid=?";
            String sql2 = "update msg set state=0 where msgid=?";
            PreparedStatement ps1 = con.prepareStatement(sql1);// PS是用来执行SQL语句和给问号赋值的
            PreparedStatement ps2 = con.prepareStatement(sql2);
            ps1.setInt(1, id);
            ps2.setInt(1, id);
            // 4.执行SQL语句            
            ResultSet rs = ps1.executeQuery();
            ps2.executeUpdate();
            while (rs.next()) {// 读取下一行。第一次执行读取第一行
                String username = rs.getString("username");
                String title = rs.getString("title");
                String msgcontent = rs.getString("msgcontent");
    %>
    发件人:<%=username%>
    </br>标题:<%=title%>
    </br>正文:<%=msgcontent%>
    <%
        }
    %>


    <%
            ps1.close();
            ps2.close();
            con.close();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    %>

    </br>
    <a href="select.jsp">【返回】 
</body>
</html>

 

posted @ 2020-05-06 14:21  Rescum  阅读(190)  评论(0编辑  收藏  举报