jsp第十二周作业
1.实现 删除 回复邮件
2.实现阅读邮件功能:在main.jsp中点击任意邮件的标题,进入到detail.jsp显示邮件详情,包括发件人,主题,内容,时间。同时需要把邮件状态修改为已读。
package com.gd.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class BaseDao { //连接数据库 public Connection getConnection(){ Connection con=null; try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ttestt?characterEncoding=utf-8","root","123456"); } catch (Exception e) { e.printStackTrace(); } return con; } //关闭数据库 protected void closeAll(Connection con,PreparedStatement ps,ResultSet rs) { try { if(rs!=null) rs.close(); if(ps!=null) ps.close(); if(con!=null) con.close(); } catch (Exception e) { e.printStackTrace(); } } }
package com.gd.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.gd.entity.Email; public class EmailDao extends BaseDao{ // 关于邮件的增删改查 // 1.添加邮件(写邮件,回复邮件都调用,邮件状态为0(未读),时间为系统当前时间) public void addEmail(Email email){ Connection con=getConnection(); String sql="insert into email(shou,biao,nei,zhuang,fa,time) values(?,?,?,0,?,?)"; PreparedStatement ps=null; try { ps=con.prepareStatement(sql); ps.setString(1, email.getShou()); ps.setString(2, email.getBiao()); ps.setString(3, email.getNei()); ps.setString(4, email.getFa()); ps.setDate(5, new java.sql.Date(new Date().getTime())); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); }finally{ closeAll(con, ps, null); } } // 2.根据id删除邮件 public void delEmail(int id){ Connection con=getConnection(); String sql="delete from email where id="+id; PreparedStatement ps=null; try { ps=con.prepareStatement(sql); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); }finally{ closeAll(con, ps, null); } } // 3.修改邮件状态 public void GauBian(int id){ Connection con=getConnection(); String sql="update email set zhuang='1' where id=?"; PreparedStatement ps=null; try { ps=con.prepareStatement(sql); ps.setInt(1, id); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); }finally{ closeAll(con, ps, null); } } // 4.按照接收者查询全部邮件 public List<Email> getAllEmail(String name){ List<Email> EmailList=new ArrayList<Email>(); Connection con=getConnection(); String sql="select * from email where shou=?"; PreparedStatement ps=null; ResultSet rs=null; try { ps=con.prepareStatement(sql); ps.setString(1, name); rs=ps.executeQuery(); while(rs.next()){ Email em=new Email(); em.setId(rs.getInt(1)); em.setShou(rs.getString(2)); em.setBiao(rs.getString(3)); em.setNei(rs.getString(4)); em.setZhuang(rs.getInt(5)); em.setFa(rs.getString(6)); em.setTime(rs.getDate(7)); EmailList.add(em); } } catch (Exception e) { e.printStackTrace(); }finally{ closeAll(con, ps, rs); } return EmailList; } // 5.查看一封邮件 public Email getOneEmail(int id){ Connection con=getConnection(); String sql="select id,shou,biao,nei,fa,time from email where id=?"; PreparedStatement ps=null; ResultSet rs=null; try { ps=con.prepareStatement(sql); ps.setInt(1, id); rs=ps.executeQuery(); while (rs.next()) { Email em=new Email(); em.setId(rs.getInt(1)); em.setShou(rs.getString(2)); em.setBiao(rs.getString(3)); em.setNei(rs.getString(4)); em.setFa(rs.getString(5)); em.setTime(rs.getDate(6)); return em; } } catch (Exception e) { e.printStackTrace(); }finally{ closeAll(con, ps, rs); } return null; } }
package com.gd.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; public class UsersDao extends BaseDao{ //登录 public boolean DengLu(String name,String pwd) { boolean f=false; Connection con=getConnection(); String sql="select * from users where uname=? and upwd=?"; PreparedStatement ps=null; ResultSet rs=null; try { ps=con.prepareStatement(sql); ps.setString(1, name); ps.setString(2, pwd); rs=ps.executeQuery(); if(rs.next()) f=true; } catch (Exception e) { e.printStackTrace(); }finally{ closeAll(con, ps, rs); } return f; } //注册 public int ZhuCe(String name,String pwd){ int i=-1; Connection con=getConnection(); String sql="insert into users(uname,upwd) values(?,?)"; PreparedStatement ps=null; try { ps=con.prepareStatement(sql); ps.setString(1, name); ps.setString(2, pwd); i=ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); }finally{ closeAll(con, ps, null); } return i; } }
package com.gd.entity; import java.util.Date; public class Email { private int id; private String shou; private String biao; private String nei; private int zhuang; private String fa; private Date time; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getShou() { return shou; } public void setShou(String shou) { this.shou = shou; } public String getBiao() { return biao; } public void setBiao(String biao) { this.biao = biao; } public String getNei() { return nei; } public void setNei(String nei) { this.nei = nei; } public int getZhuang() { return zhuang; } public void setZhuang(int zhuang) { this.zhuang = zhuang; } public String getFa() { return fa; } public void setFa(String fa) { this.fa = fa; } public Date getTime() { return time; } public void setTime(Date time) { this.time = time; } }
package com.gd.entity; public class Users { private int uid; private String uname; private String upwd; public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(String upwd) { this.upwd = upwd; } }
<%@page import="com.gd.entity.Email"%> <%@page import="com.gd.dao.EmailDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>du.jsp</title> </head> <body> <% String id = request.getParameter("id"); int eid = Integer.parseInt(id); EmailDao ed = new EmailDao(); ed.GauBian(eid); Email email = ed.getOneEmail(eid); %> <table border="1px"> <tr> <td>发件人:</td> <td><%=email.getFa()%></td> </tr> <tr> <td>标题:</td> <td><%=email.getBiao()%></td> </tr> <tr> <td>内容:</td> <td><%=email.getNei()%></td> </tr> <tr> <td>时间:</td> <td><%=email.getTime()%></td> </tr> </table><br> <a href="index.jsp">返回</a> </body> </html>
<%@page import="com.gd.entity.Email"%> <%@page import="com.gd.dao.EmailDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>index.jsp</title> </head> <body> <% EmailDao mail = new EmailDao(); String user = (String) session.getAttribute("user"); List<Email> MailList = mail.getAllEmail(user); %> 欢迎你:<%=user%> <br> <br> <table border="1px"> <tr> <td>编号</td> <td>收件人</td> <td>标题</td> <td>状态</td> <td>发件人</td> <td>时间</td> <td>操作</td> <td>操作</td> </tr> <% for (int i = 0; i < MailList.size(); i++) { %> <tr> <td> <% out.print(MailList.get(i).getId()); %> </td> <td> <% out.print(MailList.get(i).getShou()); %> </td> <td><a href="du.jsp?id=<%=MailList.get(i).getId()%>"> <% out.print(MailList.get(i).getBiao()); %> </a> </td> <td> <% if (MailList.get(i).getZhuang() == 0) { %> <img src="./images/sms_unReaded.png" /> <% } else { %> <img src="./images/sms_readed.png" /> <% } %> </td> <td> <% out.print(MailList.get(i).getFa()); %> </td> <td> <% out.print(MailList.get(i).getTime()); %> </td> <td><a href="xie.jsp?reply=<%=MailList.get(i).getFa()%>">回复</a> </td> <td><a href="shan.jsp?id=<%=MailList.get(i).getId()%>">删除</a> </td> </tr> <% } %> </table> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>MyJsp.jsp</title> </head> <body> <form action="pan.jsp" method="post" name="form1"> 用户名:<input name="name" type="text"><br> <br> 密码: <input name="password" type="password"><br> <br> <input type="button" value="登录" onclick="d()"> <a href="MyJsp2.jsp">注册 </a> <script type="text/javascript"> function d() { if (form1.name.value == "") { alert("用户名不能为空!"); return; } if (form1.password.value == "") { alert("密码不能为空!"); return; } form1.submit(); } </script> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>MyJsp2.jsp</title> </head> <body> <form action="pan2.jsp" method="post" name="form2"> 用户名:<input name="name" type="text"><br><br> 密码: <input name="password1" type="password"><br><br> 确认密码:<input name="password2" type="password"><br><br> <input type="button" value="注册" onclick="zc()"> <a href="MyJsp.jsp">返回</a> <script type="text/javascript"> function zc() { if (form2.name.value == "") { alert("用户名不能为空!"); return; } if (form2.password1.value == "") { alert("密码不能为空!"); return; } if (form2.password2.value == "") { alert("确认密码不能为空!"); return; } if (form2.password1.value != form2.password2.value) { alert("密码不一致!"); return; } form2.submit(); } </script> </form> </body> </html>
<%@page import="com.gd.dao.UsersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>pan.jsp</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String name = request.getParameter("name"); String password = request.getParameter("password"); UsersDao d = new UsersDao(); if (d.DengLu(name, password)) { session.setAttribute("user", name); request.getRequestDispatcher("index.jsp").forward(request,response); } else { out.print("登陆失败!"); response.setHeader("refresh", "1;url=MyJsp.jsp"); } %> </body> </html>
<%@page import="com.gd.dao.UsersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>pan2.jsp</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String name = request.getParameter("name"); String password1 = request.getParameter("password1"); UsersDao d = new UsersDao(); int i = d.ZhuCe(name, password1); if (i > 0) { out.print("注册成功!"); response.setHeader("refresh", "1;url=MyJsp.jsp"); } else { out.print("注册失败!"); response.setHeader("refresh", "1;url=MyJsp2.jsp"); } %> </body> </html>
<%@page import="com.gd.dao.EmailDao"%> <%@page import="com.gd.entity.Email"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>pan3.jsp</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String name = (String) session.getAttribute("user"); String shou = request.getParameter("shou"); String biao = request.getParameter("biaoti"); String nei = request.getParameter("nei"); Email e = new Email(); e.setFa(name); e.setBiao(biao); e.setNei(nei); e.setShou(shou); EmailDao ed = new EmailDao(); ed.addEmail(e); out.print("发送成功!"); response.setHeader("refresh", "1;url=index.jsp"); %> </body> </html>
<%@page import="com.gd.dao.EmailDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>shan.jsp</title> </head> <body> <% request.setCharacterEncoding("utf-8"); int emailId = Integer.parseInt(request.getParameter("id")); EmailDao ed = new EmailDao(); ed.delEmail(emailId); response.sendRedirect("index.jsp"); %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>xie.jsp</title> </head> <body> <form action="pan3.jsp" method="post" > 收件人:<input type="text" name="shou" value="<%=request.getParameter("reply")%>"><br><br> 标题: <input type="text" name="biaoti"><br><br> 内 容:<br><br> <textarea rows="10" cols="30" name="nei"></textarea> <br> <input type="submit" value="发送"> <a href="index.jsp">返回</a> </form> </body> </html>