第十一周作业

1.建库,建表2个
用户表(id,用户名,密码)
邮件表(id,发件人,收件人,标题,内容,发送时间,状态)

2.建model层
entity,dao包

3.登陆,注册,登陆后显示全部邮件

1<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 
 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 4 <html>
 5   <head>
 6     <title>登录</title>
 7   </head>
 8   <body>
 9       <script type="text/javascript">
10           function validate(){
11               if(loginForm.uname.value == ""){
12               alert("账号不能为空!");
13               return;
14               }
15               if(loginForm.upwd.value == ""){
16               alert("密码不能为空!");
17               return;
18               }
19               loginForm.submit();
20           }
21       </script>
22        <form action="dologin.jsp" name="loginForm" method="post">
23         <table  border="0"  cellpadding="10">
24             <tr><td>用户名:</td>
25                 <td><input type="text" name="uname"></td>
26             </tr>
27             <tr><td>密码:</td>
28                 <td><input type="password" name="upwd"></td>
29             </tr>
30             <tr>
31                 <td><input type="button" value="登录" onclick="validate()"></td>
32                 <td><input type="button" value="注册" onclick="location.href='register.jsp'"></td>
33             </tr>
34         </table>
35     </form>
36   </body>
37 </html>

login
1 <%@page import="com.sk.dao.MsgDao"%>
 2 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 3 <%@ page import="com.sk.dao.UsersDao" %>
 4 <%
 5     request.setCharacterEncoding("UTF-8");
 6     String uname = request.getParameter("uname");
 7     String upwd = request.getParameter("upwd");
 8     
 9     UsersDao ud = new UsersDao();
10     if(ud.login(uname, upwd)){
11         session.setAttribute("uname", uname);
12         request.getRequestDispatcher("main.jsp").forward(request, response);
13     }else{
14         out.print("登录失败,即将跳回登录页......");
15         response.setHeader("refresh", "5;url=login.jsp");
16     }
17 %>
18 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
19 <html>
20   <head>
21     <title></title>
22   </head>
23   <body>
24   
25   </body>
26 </html>

dologin
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 
 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 4 <html>
 5   <head>
 6     <title>注册</title>
 7   </head>
 8   <body>
 9        <script type="text/javascript">
10           function validate(){
11               if(regForm.uname.value == ""){
12                   alert("账号不能为空!");
13                   return;
14               }
15               if(regForm.upwd.value == ""){
16                   alert("密码不能为空!");
17                   return;
18               }
19               regForm.submit();
20           }
21       </script>
22       <form action="doreg.jsp" name="regForm" method="post">
23           <table>
24               <tr>
25                   <td>用户名:</td>
26                   <td><input type="text" name="uname"></td>
27               </tr>
28               <tr>
29                   <td>密码:</td>
30                   <td><input type="password" name="upwd"></td>
31               </tr>
32           </table>
33           <input type="button" value="提交" onclick="validate()">
34           <input type="reset" value="重置">
35       </form>
36   </body>
37 </html>

register
1 <%@page import="com.sk.dao.UsersDao"%>
 2 <%@page import="com.sk.entity.Users"%>
 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 4 
 5 <%
 6     request.setCharacterEncoding("UTF-8");
 7     response.setCharacterEncoding("UTF-8");
 8     String uname = request.getParameter("uname");
 9     String upwd = request.getParameter("upwd");
10     Users u = new Users();
11     u.setUname(uname);
12     u.setUpwd(upwd);
13     UsersDao ud = new UsersDao();
14     int r = ud.reg(u);
15     if(r >= 1){
16         request.getRequestDispatcher("login.jsp").forward(request, response);
17     }else{
18         out.print("注册失败,即将跳回注册页......");
19         response.setHeader("refresh", "3;url=register.jsp");
20     }
21 %>
22 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
23 <html>
24   <head>
25     <title></title>
26   </head>
27   <body>
28   </body>
29 </html>

doreg
1 <%@page import="com.sk.entity.Msg"%>
 2 <%@page import="com.sk.dao.MsgDao"%>
 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 4 
 5 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 6 <html>
 7 <head>
 8 <title>首页</title>
 9 </head>
10 <body>
11     <h1>欢迎进入邮箱首页!</h1>
12     <a href="send.jsp">发送邮件</a>
13     <a href="del.jsp">删除邮件</a>
14     <a href="unread.jsp">查看未读邮件</a>
15     <h3>收件箱</h3>
16     <%
17         MsgDao md = new MsgDao();
18         String uname = (String) session.getAttribute("uname");
19         List<Msg> list = md.getMailByReceiver(uname);
20     %>
21     <table border="1">
22         <tr>
23             <th>发件人</th>
24             <th>标题</th>
25             <th>状态</th>
26             <th>时间</th>
27         </tr>
28         <%
29             for (int i = 0; i < list.size(); i++) {
30         %>
31         <tr>
32             <td>
33                 <%
34                     out.print(list.get(i).getSendto() + "<br>");
35                 %>
36             </td>
37             <td>
38                 <%
39                     out.print(list.get(i).getTitle() + "<br>");
40                 %>
41             </td>
42             <td>
43                 <%
44                     out.print(list.get(i).getState() + "<br>");
45                 %>
46             </td>
47             <td>
48                 <%
49                     out.print(list.get(i).getMsg_create_date() + "<br>");
50                 %>
51             </td>
52         </tr>
53         <%
54             }
55         %>
56     </table>
57 </body>
58 </html>

main
1 package com.sk.entity;
 2 
 3 public class Users {
 4     private int id;
 5     private String uname;
 6     private String upwd;
 7     
 8     
 9     public Users() {
10         super();
11     }
12     public Users(String uname, String upwd) {
13         super();
14         this.uname = uname;
15         this.upwd = upwd;
16     }
17     public int getId() {
18         return id;
19     }
20     public void setId(int id) {
21         this.id = id;
22     }
23     public String getUname() {
24         return uname;
25     }
26     public void setUname(String uname) {
27         this.uname = uname;
28     }
29     public String getUpwd() {
30         return upwd;
31     }
32     public void setUpwd(String upwd) {
33         this.upwd = upwd;
34     }
35     
36     
37 }

Users
1 package com.sk.entity;
 2 
 3 import java.util.Date;
 4 
 5 public class Msg {
 6     private int msgid;
 7     private String username;
 8     private String title;
 9     private String msgcontent;
10     private int state;
11     private String sendto;
12     private Date msg_create_date;
13     
14     public int getMsgid() {
15         return msgid;
16     }
17     public void setMsgid(int msgid) {
18         this.msgid = msgid;
19     }
20     public String getUsername() {
21         return username;
22     }
23     public void setUsername(String username) {
24         this.username = username;
25     }
26     public String getTitle() {
27         return title;
28     }
29     public void setTitle(String title) {
30         this.title = title;
31     }
32     public String getMsgcontent() {
33         return msgcontent;
34     }
35     public void setMsgcontent(String msgcontent) {
36         this.msgcontent = msgcontent;
37     }
38     public int getState() {
39         return state;
40     }
41     public void setState(int state) {
42         this.state = state;
43     }
44     public String getSendto() {
45         return sendto;
46     }
47     public void setSendto(String sendto) {
48         this.sendto = sendto;
49     }
50     public Date getMsg_create_date() {
51         return msg_create_date;
52     }
53     public void setMsg_create_date(Date msg_create_date) {
54         this.msg_create_date = msg_create_date;
55     }
56     
57 }

Msg
1 package com.sk.dao;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 
 9 public class BaseDao {
10     public Connection conn = null;
11     PreparedStatement ps = null;
12     ResultSet rs = null;
13     
14     //获取连接
15     protected Connection getConnection(){
16         
17         try {
18             //加载驱动
19             Class.forName("com.mysql.jdbc.Driver");
20             //建立连接
21             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
22         } catch (Exception e) {
23             // TODO Auto-generated catch block
24             e.printStackTrace();
25         }
26         return conn;
27     }
28     
29     //关闭连接
30     protected void closeAll(Connection con, PreparedStatement ps, ResultSet rs){
31         try{
32             if(rs != null)
33                 rs.close();
34             if(ps != null)
35                 ps.close();
36             if(con != null)
37                 con.close();
38         }catch (SQLException e) {
39             e.printStackTrace();
40         }
41     }
42 }

BaseDao
1 package com.sk.dao;
 2 
 3 import java.sql.Connection;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 
 8 import com.sk.entity.Users;
 9 
10 public class UsersDao extends BaseDao {
11     //关于用户的增删改查
12     
13     //登录
14     public boolean login(String uname,String upwd){
15         boolean f = false;
16         Connection conn = getConnection();
17         String sql = "select * from users where uname=? and upwd=?";
18         PreparedStatement ps;
19         try {
20             ps = conn.prepareStatement(sql);
21             ps.setString(1, uname);
22             ps.setString(2, upwd);
23             ResultSet rs = ps.executeQuery();
24             if(rs.next())    //查到结果
25                 f = true;
26             closeAll(conn,ps,rs);
27         } catch (SQLException e) {
28             // TODO Auto-generated catch block
29             e.printStackTrace();
30         }
31         return f;
32     }
33     //注册
34     public int reg(Users u){
35         int i = 0;
36         Connection conn = getConnection();
37         PreparedStatement ps = null;
38         String sql = "insert into users(uname,upwd) values (?,?)";
39         try {
40             ps = conn.prepareStatement(sql);
41             ps.setString(1, u.getUname());
42             ps.setString(2, u.getUpwd());
43             i = ps.executeUpdate();
44         } catch (SQLException e) {
45             // TODO Auto-generated catch block
46             e.printStackTrace();
47         }finally{
48             closeAll(conn, ps, null);
49         }
50         return i;
51     }
52 }

UsersDao
1 package com.sk.dao;
 2 
 3 import java.sql.Connection;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 import java.util.ArrayList;
 8 import java.util.List;
 9 
10 import com.sk.entity.Msg;
11 
12 public class MsgDao extends BaseDao {
13     //关于邮件的增删改查
14     
15     //添加邮件(写邮件、回复邮件都调用,邮件状态为未读=0,时间为系统时间)
16     
17     //删除邮件(根据id)
18     
19     //修改邮件状态(根据id 已读  = 1)
20     
21     //查看邮件(根据  收件人username)
22     public List<Msg> getMailByReceiver(String uname){
23         List<Msg> list = new ArrayList<Msg>();
24         Connection con = getConnection();
25         String sql = "select * from msg where username=?";
26         PreparedStatement ps = null;
27         ResultSet rs = null;
28         try {
29             ps = con.prepareStatement(sql);
30             ps.setString(1, uname);
31             rs = ps.executeQuery();
32             while(rs.next()){
33                 //每读取一行,创建一个msg对象,对象放在list中
34                 Msg m = new Msg();
35                 m.setMsgid(rs.getInt(1));
36                 m.setUsername(rs.getString(2));
37                 m.setTitle(rs.getString(3));
38                 m.setMsgcontent(rs.getString(4));
39                 m.setState(rs.getInt(5));
40                 m.setSendto(rs.getString(6));
41                 m.setMsg_create_date(rs.getDate(7));
42                 list.add(m);
43             }
44         } catch (SQLException e) {
45             // TODO Auto-generated catch block
46             e.printStackTrace();
47         }finally{
48             closeAll(con, ps, rs);
49         }
50         
51         return list;
52     }
53     
54     //查看未读邮件(根据  收件人username)
55         public List<Msg> getUnReadMailByReceiver(String uname){
56             List<Msg> list = new ArrayList<Msg>();
57             Connection con = getConnection();
58             String sql = "select * from msg where username=? and state=0";
59             PreparedStatement ps = null;
60             ResultSet rs = null;
61             try {
62                 ps = con.prepareStatement(sql);
63                 ps.setString(1, uname);
64                 rs = ps.executeQuery();
65                 while(rs.next()){
66                     //每读取一行,创建一个msg对象,对象放在list中
67                     Msg m = new Msg();
68                     m.setMsgid(rs.getInt(1));
69                     m.setUsername(rs.getString(2));
70                     m.setTitle(rs.getString(3));
71                     m.setMsgcontent(rs.getString(4));
72                     m.setState(rs.getInt(5));
73                     m.setSendto(rs.getString(6));
74                     m.setMsg_create_date(rs.getDate(7));
75                     list.add(m);
76                 }
77             } catch (SQLException e) {
78                 // TODO Auto-generated catch block
79                 e.printStackTrace();
80             }finally{
81                 closeAll(con, ps, rs);
82             }
83             
84             return list;
85         }
86 }

MsgDao

 

posted @ 2022-05-15 18:38  无言De钉钉  阅读(21)  评论(0编辑  收藏  举报