jsp12
1.实现 删除 回复邮件
2.实现阅读邮件功能:在main.jsp中点击任意邮件的标题,进入到detail.jsp显示邮件详情,包括发件人,主题,内容,时间。同时需要把邮件状态修改为已读。
1 <%@page import="dao.EmlDao"%> 2 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 3 <html> 4 <head> 5 <title>删除邮件</title> 6 </head> 7 <body> 8 <% 9 request.setCharacterEncoding("utf-8"); 10 11 int eid = Integer.parseInt(request.getParameter("eid")); 12 EmlDao ed = new EmlDao(); 13 ed.delEml(eid); 14 %> 15 <script type="text/javascript"> 16 alert("删除成功!"); 17 </script> 18 <% 19 response.sendRedirect("main.jsp"); 20 %> 21 </body> 22 </html>
1 package com.gxl.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.gxl.entity.Msg; 12 13 14 15 public class MsgDao extends BaseDao { 16 // 1,插入邮件 17 public void addMsg(Msg m) { 18 Connection con = getConnection(); 19 String sql = "insert into msg(username,title,msgcontent,state,sendto,msg_create_date) values(?,?,?,?,?,?)"; 20 PreparedStatement ps = null; 21 try { 22 ps = con.prepareStatement(sql); 23 ps.setString(1, m.getUsername()); 24 ps.setString(2, m.getTitle()); 25 ps.setString(3, m.getMsgcontent()); 26 ps.setInt(4, 1); 27 ps.setString(5, m.getSendto()); 28 ps.setDate(6, new java.sql.Date(new Date().getTime()));// 系统当前时间 29 ps.executeUpdate(); 30 } catch (SQLException e) { 31 // TODO Auto-generated catch block 32 e.printStackTrace(); 33 } finally { 34 closeAll(con, ps, null); 35 } 36 37 } 38 39 // 2.删除邮件 40 public void delMail(int id) { 41 Connection conn = getConnection(); 42 String sql = "delete from msg where msgid=?"; 43 PreparedStatement ps = null; 44 try { 45 ps = conn.prepareStatement(sql); 46 ps.setInt(1, id); 47 ps.executeUpdate(); 48 } catch (SQLException e) { 49 // TODO Auto-generated catch block 50 e.printStackTrace(); 51 }finally{ 52 closeAll(conn, ps, null); 53 } 54 55 } 56 //(测试删除邮件的代码是否编写成功) 57 // public static void main(String[] args) { 58 // MsgDao md=new MsgDao(); 59 // md.delMail(3); 60 //} 61 62 63 // 3.修改邮件状态 64 public void update(int id) { 65 Connection con = getConnection(); 66 String sql = "update msg set state='1' where msgid=?"; 67 PreparedStatement ps = null; 68 try { 69 ps = con.prepareStatement(sql); 70 ps.setInt(1, id); 71 ps.executeUpdate(); 72 } catch (SQLException e) { 73 e.printStackTrace(); 74 } finally { 75 closeAll(con, ps, null); 76 } 77 } 78 79 // 4.按照接收者查询全部邮件 80 public List<Msg> getMailByReceiver(String name) { 81 List<Msg> list = new ArrayList<Msg>(); 82 Connection con = getConnection(); 83 String sql = "select * from msg where sendto=?"; 84 PreparedStatement ps=null; 85 ResultSet rs=null; 86 try { 87 ps = con.prepareStatement(sql); 88 ps.setString(1, name); 89 rs = ps.executeQuery(); 90 while (rs.next()) { 91 Msg m = new Msg(); 92 m.setMsgid(rs.getInt("msgid")); 93 m.setUsername(rs.getString("username")); 94 m.setTitle(rs.getString("title")); 95 m.setMsgcontent(rs.getString("msgcontent")); 96 m.setState(rs.getInt("state")); 97 m.setSendto(rs.getString("sendto")); 98 m.setMsg_create_date(rs.getDate("msg_create_date")); 99 list.add(m); 100 } 101 } catch (SQLException e) { 102 e.printStackTrace(); 103 }finally{ 104 closeAll(con, ps, rs); 105 } 106 return list; 107 108 } 109 //5.实现阅读邮件功能 110 public Msg read(int id) { 111 Connection con = getConnection(); 112 String sql = "select msgid,username,sendto,title,msgcontent,msg_create_date from msg where msgid=?"; 113 PreparedStatement ps = null; 114 ResultSet rs = null; 115 try { 116 ps = con.prepareStatement(sql); 117 ps.setInt(1, id); 118 rs = ps.executeQuery(); 119 while (rs.next()) { 120 Msg m = new Msg(); 121 m.setMsgid(rs.getInt("msgid")); 122 m.setUsername(rs.getString("username")); 123 m.setTitle(rs.getString("title")); 124 m.setMsgcontent(rs.getString("msgcontent")); 125 m.setSendto(rs.getString("sendto")); 126 m.setMsg_create_date(rs.getDate("msg_create_date")); 127 return m; 128 } 129 130 } catch (SQLException e) { 131 e.printStackTrace(); 132 } finally { 133 closeAll(con, ps, rs); 134 } 135 return null; 136 } 137 138 139 }
1 <%@page import="entity.Msg"%> 2 <%@page import="dao.MsgDao"%> 3 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 4 5 <html> 6 <head> 7 <title>My JSP 'index.jsp' main page</title> 8 </head> 9 <body> 10 <% 11 String uname=(String)session.getAttribute("uname"); 12 %> 13 欢迎你 <% out.print(uname); %> <a href="write.jsp">写邮件</a> 14 <table height="20px" width="100%" align="center" border="1"> 15 <tr> 16 <td>发件人</td> 17 <td>主题</td> 18 <td>状态</td> 19 <td>时间</td> 20 <td>操作</td> 21 <td>操作</td> 22 </tr> 23 <% 24 MsgDao md=new MsgDao(); 25 List<Msg> list=md.getMailByReceiver(uname); 26 for(Msg m:list) 27 {%> 28 <tr> 29 <td><%=m.getUsername() %></td> 30 <td><a href="detail.jsp?reply=<%=m.getMsgid()%>"><%=m.getTitle() %></a></td> 31 <td><% 32 int state=m.getState(); 33 if(state==1){ %> 34 <img src ="image/已读消息.png" /> 35 <%; 36 }else{%> 37 <img src ="image/未读.png" /> 38 <%; 39 } 40 %></td> 41 <td><%=m.getMsg_create_date() %></td> 42 <td><a href="write.jsp?reply=<%=m.getUsername()%>">回复</a></td> 43 <td><a href="del.jsp?id=<%=m.getMsgid()%>">删除</a></td> 44 </tr> 45 <%} 46 %> 47 </table> 48 </body> 49 </html>
1 package com.gxl.dao; 2 3 import java.sql.Connection; 4 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 public class UsersDao extends BaseDao { 10 11 // 关于用户的增删改查 12 13 // 1.登录 14 public boolean login(String uname, String upwd) { 15 boolean f = false; 16 Connection con = getConnection(); 17 String sql = "select * from users where uname=? and upwd=?"; 18 PreparedStatement ps = null; 19 ResultSet rs = null; 20 try { 21 ps = con.prepareStatement(sql); 22 ps.setString(1, uname); 23 ps.setString(2, upwd); 24 rs = ps.executeQuery(); 25 if (rs.next()) 26 f = true; 27 } catch (SQLException e) { 28 e.printStackTrace(); 29 } finally { 30 closeAll(con, ps, rs); 31 } 32 return f; 33 } 34 35 // 2.注册 36 public int register(String uname, String upwd) { 37 Connection con = getConnection(); 38 PreparedStatement ps = null; 39 int x = 0; 40 try { 41 String sql = "insert into users(uname,upwd) values(?,?)"; 42 ps = con.prepareStatement(sql); 43 ps.setString(1, uname); 44 ps.setString(2, upwd); 45 x = ps.executeUpdate(); 46 } catch (SQLException e) { 47 e.printStackTrace(); 48 } finally { 49 closeAll(con, ps, null); 50 } 51 return x; 52 } 53 }
1 <%@page import="entity.email"%> 2 <%@page import="dao.EmlDao"%> 3 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 4 <html> 5 <head> 6 <title>主界面</title> 7 </head> 8 <body> 9 <% 10 String uname = (String) session.getAttribute("uname"); 11 %> 12 <h2>首页!!欢迎你<%=uname%></h2> 13 <table border="1"> 14 <tr> 15 <th>发件人</th> 16 <th>主题</th> 17 <th>状态</th> 18 <th>时间</th> 19 <th>操作</th> 20 21 <% 22 EmlDao md = new EmlDao(); 23 List<email> list = md.getEmailByAddressee(uname); 24 25 for (int i = 0; i < list.size(); i++) { 26 %> 27 <tr> 28 <td><%=list.get(i).getSendername()%></td> 29 <td><a href="show.jsp?eid=<%=list.get(i).getEid() %>"><%=list.get(i).getTitle() %></a></td> 30 <td> 31 <% 32 if (list.get(i).getState() == 1) { 33 %> <img src="images/sms_unReaded.png" /> <% 34 } else { 35 %> <img src="images/sms_readed.png" /> <% 36 } 37 %> 38 </td> 39 <td><%=list.get(i).getTotime()%></td> 40 <td><a href="delete.jsp?eid=<%=list.get(i).getEid()%>">删除</a></td> 41 <td><a href="write.jsp?reply=<%=list.get(i).getSendername()%>">回复</a></td> 42 </tr> 43 <% 44 } 45 %> 46 </table> 47 <br> 48 <a href="write.jsp">新建邮件</a> 49 </body> 50 </html>
1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <%@page import="entity.Msg"%> 4 <%@page import="dao.MsgDao"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 <% 13 String uname=(String)session.getAttribute("uname"); 14 %> 15 <% 16 request.setCharacterEncoding("utf-8"); 17 String msgid=request.getParameter("reply"); 18 int id=Integer.parseInt(msgid); 19 %> 20 <table height="20px" width="100%" align="center" border="1"> 21 <tr> 22 <td>发件人</td> 23 <td>主题</td> 24 <td>内容</td> 25 <td>时间</td> 26 </tr> 27 <% 28 MsgDao md=new MsgDao(); 29 Msg m=md.Cha(id); 30 md.updateMsg(id); 31 %> 32 <tr> 33 <td><%=m.getUsername() %></td> 34 <td><%=m.getTitle() %></td> 35 <td><%=m.getMsgcontent() %></td> 36 <td><%=m.getMsg_create_date() %></td> 37 </tr> 38 </table> 39 </body> 40 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <%@ page import="com.gxl.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 8 </head> 9 10 <body> 11 <% 12 request.setCharacterEncoding("utf-8");
13 String uname = request.getParameter("uname"); 14 String upwd = request.getParameter("upwd"); 15 UsersDao ud = new UsersDao(); 16 if (ud.login(uname, upwd)) { 17 session.setAttribute("uname",uname); 18 request.getRequestDispatcher("main.jsp").forward( 19 request,response); 20 } else { 21 out.print("登录失败,即将跳回登录页......"); 22 response.setHeader("refresh", "3;url=login.jsp"); 23 } 24 %> 25 </body> 26 </html>