JSP第十次作业

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

com.gd.dao

 1 package com.gd.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     //获取连接
11     protected Connection getConnection(){
12         Connection conn=null;
13             try {
14                 Class.forName("com.mysql.jdbc.Driver");
15                 // 2.建立连接
16                 conn = DriverManager.getConnection(
17                         "jdbc:mysql://localhost:3306/test", "root", "123456");
18             } catch (Exception e) {
19                 e.printStackTrace();
20             } 
21             return conn;
22     }    
23     //关闭连接
24     protected void closeAll(Connection con,PreparedStatement ps,ResultSet rs){        
25     try {
26         if(rs != null)
27             rs.close();
28         if(ps != null)
29             ps.close();
30         if(con != null)
31             con.close();
32         
33         } catch (SQLException e) {
34             e.printStackTrace();
35         }
36     }
37 
38 }
  1 package com.gd.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.Date;
  9 import java.util.List;
 10 
 11 import com.gd.entity.Msg;
 12 
 13 public class MsgDao extends BaseDao {
 14     //根据收件人查看全部邮件
 15     public List<Msg> getMailByReceiver(String name){
 16         List<Msg> list=new ArrayList<Msg>();
 17         Connection con=getConnection();
 18         String sql="select * from msg where sendto=?";
 19         PreparedStatement ps=null;
 20         ResultSet rs=null;
 21         try {
 22             ps = con.prepareStatement(sql);
 23             ps.setString(1, name);
 24              rs=ps.executeQuery();
 25             while(rs.next()){
 26                 //每读取一行,创建一个msg对象,对象放到集合中
 27                 Msg m=new Msg();
 28                 m.setMsgid(rs.getInt(1));
 29                 m.setUsername(rs.getString(2));
 30                 m.setTitle(rs.getString(3));
 31                 m.setMsgcontent(rs.getString(4));
 32                 m.setState(rs.getInt(5));
 33                 m.setSendto(rs.getString(6));
 34                 m.setMsg_create_date(rs.getDate(7));
 35                 list.add(m);                
 36             }            
 37             
 38         } catch (SQLException e) {
 39             // TODO Auto-generated catch block
 40             e.printStackTrace();
 41         }finally{
 42             closeAll(con, ps, rs);
 43         }        
 44         return list;
 45     }
 46     //关于邮件的增删改查    
 47         //添加邮件(写邮件,回复邮件都调用,邮件状态为1(未读),时间为系统当前时间)
 48         public void addMsg(Msg m){
 49             Connection conn=getConnection();
 50             String sql="insert into msg(username,title,msgcontent,state,sendto,msg_create_date) values(?,?,?,1,?,?)";
 51             PreparedStatement ps=null;
 52             try {
 53                  ps=conn.prepareStatement(sql);
 54                  ps.setString(1, m.getUsername());
 55                  ps.setString(2, m.getTitle());
 56                  ps.setString(3, m.getMsgcontent());
 57                  ps.setString(4, m.getSendto());
 58                  ps.setDate(5, new java.sql.Date(new Date().getTime()));
 59                  ps.executeUpdate();
 60             } catch (SQLException e) {
 61                 // TODO Auto-generated catch block
 62                 e.printStackTrace();
 63             }finally{
 64                 closeAll(conn, ps, null);
 65             }        
 66         }
 67     
 68         //根据id删除邮件
 69         public void delMsg(int id){
 70             Connection con=getConnection();
 71             String sql="delete from msg where msgid="+id;
 72             PreparedStatement ps=null;
 73             try {
 74                 ps=con.prepareStatement(sql);
 75                 ps.executeUpdate();
 76             } catch (SQLException e) {
 77                 // TODO Auto-generated catch block
 78                 e.printStackTrace();
 79             }finally{
 80                 closeAll(con, ps, null);
 81             }
 82         }
 83         public void updateMsg(int id) {
 84             Connection con = getConnection();
 85             String sql = "update  msg set state='1' where msgid=?";
 86             PreparedStatement pred = null;
 87             try {
 88                 pred = con.prepareStatement(sql);
 89                 pred.setInt(1, id);
 90                 pred.executeUpdate();
 91             } catch (SQLException e1) {
 92                 e1.printStackTrace();
 93             } finally {
 94                 closeAll(con, pred, null);
 95             }
 96         }
 97 
 98          public Msg read(int id) {
 99                 Connection con = getConnection();
100                 String sql = "select msgid,username,sendto,title,msgcontent,msg_create_date from msg where msgid=?";
101                 PreparedStatement ps = null;
102                 ResultSet rs = null;
103                 try {
104                     ps = con.prepareStatement(sql);
105                     ps.setInt(1, id);
106                     rs = ps.executeQuery();
107                     while (rs.next()) {
108                         Msg m = new Msg();
109                         m.setMsgid(rs.getInt("msgid"));
110                         m.setUsername(rs.getString("username"));
111                         m.setTitle(rs.getString("title"));
112                         m.setMsgcontent(rs.getString("msgcontent"));
113                         m.setSendto(rs.getString("sendto"));
114                         m.setMsg_create_date(rs.getDate("msg_create_date"));
115                         return m;
116                     }
117 
118                 } catch (SQLException e) {
119                     e.printStackTrace();
120                 } finally {
121                     closeAll(con, ps, rs);
122                 }
123                 return null;
124         }
125 }
 1 package com.gd.dao;
 2 
 3 import java.sql.Connection;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 
 8 public class UsersDao extends BaseDao {
 9     // 登陆
10     public boolean login(String uname, String upwd) {
11         boolean f = false;
12         Connection conn = getConnection();
13         String sql = "select * from users where uname=? and upwd=?";
14         PreparedStatement ps = null;
15         ResultSet rs = null;
16         try {
17             ps = conn.prepareStatement(sql);
18             ps.setString(1, uname);// 第一个?赋值为name
19             ps.setString(2, upwd);
20             rs = ps.executeQuery();
21             if (rs.next())// 查到结果了
22                 f = true;
23         } catch (SQLException e) {
24             // TODO Auto-generated catch block
25             e.printStackTrace();
26         } finally {
27             closeAll(conn, ps, rs);
28         }
29         return f;
30     }
31     // 注册
32     public int reg(String uname,String upwd){
33         int i=-1;
34         PreparedStatement pred=null;
35         Connection con=getConnection();
36         String sql="insert into users(uname,upwd)values(?,?)";
37         try {
38             pred= con.prepareStatement(sql);
39             pred.setString(1, uname);
40             pred.setString(2, upwd);
41             i=pred.executeUpdate();
42         } catch (SQLException e) {
43             e.printStackTrace();
44         }finally{
45             closeAll(con, pred, null);
46         }
47         return i;
48     }
49 }

com.gd.entity

 1 package com.gd.entity;
 2 
 3 import java.util.Date;
 4 
 5 public class Msg {
 6     private Integer 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 Msg() {
15         super();
16         // TODO Auto-generated constructor stub
17     }
18 
19     public Msg(Integer msgid, String username, String title, String msgcontent,
20             int state, String sendto, Date msg_create_date) {
21         super();
22         this.msgid = msgid;
23         this.username = username;
24         this.title = title;
25         this.msgcontent = msgcontent;
26         this.state = state;
27         this.sendto = sendto;
28         this.msg_create_date = msg_create_date;
29     }
30 
31     public Integer getMsgid() {
32         return msgid;
33     }
34 
35     public void setMsgid(Integer msgid) {
36         this.msgid = msgid;
37     }
38 
39     public String getUsername() {
40         return username;
41     }
42 
43     public void setUsername(String username) {
44         this.username = username;
45     }
46 
47     public String getTitle() {
48         return title;
49     }
50 
51     public void setTitle(String title) {
52         this.title = title;
53     }
54 
55     public String getMsgcontent() {
56         return msgcontent;
57     }
58 
59     public void setMsgcontent(String msgcontent) {
60         this.msgcontent = msgcontent;
61     }
62 
63     public int getState() {
64         return state;
65     }
66 
67     public void setState(int state) {
68         this.state = state;
69     }
70 
71     public String getSendto() {
72         return sendto;
73     }
74 
75     public void setSendto(String sendto) {
76         this.sendto = sendto;
77     }
78 
79     public Date getMsg_create_date() {
80         return msg_create_date;
81     }
82 
83     public void setMsg_create_date(Date msg_create_date) {
84         this.msg_create_date = msg_create_date;
85     }
86     
87     
88 }
 1 package com.gd.entity;
 2 
 3 public class Users {
 4     private Integer id;
 5     private String uname;
 6     private String upwd;
 7     
 8     public Users() {
 9         super();
10         // TODO Auto-generated constructor stub
11     }
12 
13     public Users(Integer id, String uname, String upwd) {
14         super();
15         this.id = id;
16         this.uname = uname;
17         this.upwd = upwd;
18     }
19 
20     public Integer getId() {
21         return id;
22     }
23 
24     public void setId(Integer id) {
25         this.id = id;
26     }
27 
28     public String getUname() {
29         return uname;
30     }
31 
32     public void setUname(String uname) {
33         this.uname = uname;
34     }
35 
36     public String getUpwd() {
37         return upwd;
38     }
39 
40     public void setUpwd(String upwd) {
41         this.upwd = upwd;
42     }
43     
44 }

jsp

reg.jsp

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

doreg.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@page import="com.gd.dao.UsersDao"%>
 3 <%
 4     request.setCharacterEncoding("utf-8");
 5     response.setCharacterEncoding("utf-8");
 6 %>
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9 <head>
10 <title>My JSP 'doreg.jsp' starting page</title>
11 </head>
12 <body>
13     <%
14         String uname = request.getParameter("uname");
15         String upwd = request.getParameter("upwd");
16         UsersDao ud = new UsersDao();
17         int i = ud.reg(uname, upwd);
18         if (i > 0) {
19             out.print("注册成功,即将跳到登录页.....");
20             response.setHeader("refresh", "2;url=denglu.jsp");
21         } else {
22             out.print("注册失败,即将跳回注册页.....");
23             response.setHeader("refresh", "2;url=reg.jsp");
24         }
25     %>
26 </body>
27 </html>

denglu.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4 <head>
 5 <title>My JSP 'denglu.jsp' starting page</title>
 6 </head>
 7 
 8 <body>
 9     <h1>登录</h1>
10     <script type="text/javascript">
11         function validate() {
12             if (loginForm.uname.value == "") {
13                 alert("账号不能为空!");
14                 return;
15             }
16             if (loginForm.upwd.value == "") {
17                 alert("密码不能为空!");
18                 return;
19             }
20             loginForm.submit();
21         }
22     </script>
23     <form name="loginForm" action="dologin.jsp" method="post">
24         用户名:<input type="text" name="uname"><br> 密码: <input
25             type="password" name="upwd" value="123456"><br> <input
26             type="button" value="登录" onClick="validate()"> <a
27             href="reg.jsp">立即注册</a>
28     </form>
29 </body>
30 </html>

dologin.jsp

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

main.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@page import="com.gd.entity.Msg"%>
 3 <%@page import="com.gd.dao.MsgDao"%>
 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 5 <html>
 6 <head>
 7 <title>My JSP 'main.jsp' starting page</title>
 8 </head>
 9 <body>
10     <%
11         MsgDao md = new MsgDao();
12         String uname = (String) session.getAttribute("uname");
13         List<Msg> list = md.getMailByReceiver(uname);
14     %>
15     欢迎你<%=uname%>
16     <a href="write.jsp">写邮件</a>
17     <a href="exit.jsp">退出登录</a>
18     <table border="1">
19         <tr>
20             <td>发件人</td>
21             <td>主题</td>
22             <td>状态</td>
23             <td>时间</td>
24             <td>操作</td>
25             <td>操作</td>
26         </tr>
27 
28         <%
29             for (int i = 0; i < list.size(); i++) {
30         %>
31         <tr>
32             <td><%=list.get(i).getUsername()%></td>
33             <td><a href="detail.jsp?id=<%=list.get(i).getMsgid()%>"> 
34             <%out.print(list.get(i).getTitle().toString());%>
35             </a>
36             </td>
37             <td>
38                 <%
39                     if (list.get(i).getState() == 1) {
40                 %> <img src="images/sms_unReaded.png" /> <%
41      } else {
42  %> <img src="images/sms_readed.png" /> <%
43      }
44  %>
45             </td>
46             <td><%=list.get(i).getMsg_create_date()%></td>
47             <td><a href="write.jsp?reply=<%=list.get(i).getUsername()%>">回复</a>
48             </td>
49             <td><a href="del.jsp?id=<%=list.get(i).getMsgid()%>">删除</a>
50             </td>
51         </tr>
52         <%
53             }
54         %>
55     </table>
56 
57 </body>
58 </html>

write.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3     request.setCharacterEncoding("utf-8");
 4     response.setCharacterEncoding("utf-8");
 5 %>
 6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 7 <html>
 8   <head>   
 9     <title>My JSP 'write.jsp' starting page</title>
10   </head>  
11   <body>
12    <form action="dowrite.jsp" method="post">        
13     收件人:<input type="text" name="sendto" value="<%=request.getParameter("reply") %>"><br> 
14     主题: <input  type="text" name="title" ><br>
15     内容:<textarea rows="6" cols="20" name="content"></textarea>
16 <br>
17 <input type="submit" value="发送"> 
18     </form>
19   </body>
20 </html>

dowrite.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@page import="com.gd.entity.Msg"%>
 3 <%@page import="com.gd.dao.MsgDao"%>
 4 <%@page import="com.gd.dao.UsersDao"%>
 5 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 6 <html>
 7   <head>    
 8     <title>My JSP 'dowrite.jsp' starting page</title>
 9   </head>
10   <body>
11    <%
12     request.setCharacterEncoding("utf-8");
13     
14     String uname=(String)session.getAttribute("uname");// 发件人在session中获取
15     String sendto=request.getParameter("sendto");
16     String title=request.getParameter("title");
17     String content=request.getParameter("content");
18     
19     Msg m=new Msg();
20     m.setTitle(title);
21     m.setMsgcontent(content);
22     m.setUsername(uname);
23     m.setSendto(sendto);
24     
25     MsgDao md=new MsgDao();
26     md.addMsg(m);
27     
28     out.print("发送成功,即将跳回首页.....");
29     response.setHeader("refresh", "3;url=main.jsp");
30  %>
31   </body>
32 </html>

detail.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@page import="com.gd.dao.MsgDao"%>
 3 <%@page import="com.gd.entity.Msg"%>
 4 <%
 5     request.setCharacterEncoding("utf-8");
 6     response.setCharacterEncoding("utf-8");
 7 %>
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10 <head>
11 <title>My JSP 'detail.jsp' starting page</title>
12 </head>
13 <body>
14     <%
15         request.setCharacterEncoding("utf-8");
16         String msgid = request.getParameter("id");
17         int idd = Integer.parseInt(msgid);
18         MsgDao md = new MsgDao();
19         md.updateMsg(idd);
20         Msg m = md.read(idd);
21     %>
22     <table>
23         <tr>
24             <td>发件人:</td>
25             <td><input type="text" name="username" style="border: none"
26                 value="<%=m.getUsername()%>"></td>
27         </tr>
28         <tr>
29             <td>主题:</td>
30             <td><input type="text" name="title" style="border: none"
31                 value="<%=m.getTitle()%>"></td>
32         </tr>
33         <tr>
34             <td>时间:</td>
35             <td><input type="text" name="msg_create_date"
36                 style="border: none" value="<%=m.getMsg_create_date()%>"></td>
37         </tr>
38         <tr>
39             <td>收件人:</td>
40             <td><input type="text" name="sendto" style="border: none"
41                 value="<%=m.getSendto()%>"></td>
42         </tr>
43         <tr>
44             <td>内容:</td>
45             <td><div style="border: none;outline: none;overflow: inherit;">
46                     <%=m.getMsgcontent()%></div></td>
47         </tr>
48     </table>
49     <a href="main.jsp">返回</a>
50 </body>
51 </html>

del.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@page import="com.gd.entity.Msg"%>
 3 <%@page import="com.gd.dao.MsgDao"%>
 4 <%@page import="com.gd.dao.UsersDao"%>
 5 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 6 <html>
 7   <head>   
 8     <title>My JSP 'del.jsp' starting page</title>
 9   </head>
10   <body>
11     <%
12     request.setCharacterEncoding("utf-8");
13     int id=Integer.parseInt(request.getParameter("id"));
14     MsgDao md=new MsgDao();
15     md.delMsg(id);
16     out.print("删除成功、、、、、、、");
17     response.sendRedirect("main.jsp");
18  %>
19   </body>
20 </html>

exit.jsp

 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>My JSP 'exit.jsp' starting page</title>
 7 </head>
 8 
 9 <body>
10     <%
11         session.invalidate();
12         response.sendRedirect("reg.jsp");
13     %>
14 </body>
15 </html>

 

 

 

 

 删除

 

 回复:

 

 查看内容:

 

 

 

 

posted @ 2022-05-19 14:58  MXT16  阅读(20)  评论(0编辑  收藏  举报