JSP第十二周作业
1.实现 删除 回复邮件
2.实现阅读邮件功能:在main.jsp中点击任意邮件的标题,进入到detail.jsp显示邮件详情,包括发件人,主题,内容,时间。同时需要把邮件状态修改为已读。
package com.sf.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.annotation.PreDestroy; import com.sf.entity.msg; import com.sun.org.apache.regexp.internal.recompile; public class msgDao extends BaseDao{ public List<msg> listAll(String sendto) throws SQLException{ List<msg> list =new ArrayList<msg>(); Connection conn =getConnection(); String sql="select * from msg where sendto=?"; PreparedStatement pred =null; ResultSet rest=null; try { pred=conn.prepareStatement(sql); pred.setString(1, sendto); rest=pred.executeQuery(); while (rest.next()) { msg msg=new msg(); msg.setMsgid(rest.getInt("msgid")); msg.setUsername(rest.getString("username")); msg.setTitle(rest.getString("title")); msg.setMsgcontent(rest.getString("msgcontent")); msg.setState(rest.getInt("state")); msg.setSendto(rest.getString("sendto")); msg.setMsg_create_date(rest.getDate("msg_create_date")); list.add(msg); } } catch (Exception e) { // TODO: handle exception } finally{ closeAll(conn, pred, rest); } return list; } public void changestate(Integer msgid){ Connection conn =getConnection(); String sql="update msg set state=1 where msgid=?"; PreparedStatement pred=null; try { pred =conn.prepareStatement(sql); pred.setInt(1, msgid); pred.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ closeAll(conn, pred, null); } } public List<msg> showContent(Integer msgid) { List<msg> list=new ArrayList<msg>(); Connection conn=getConnection(); String sql="select * from msg where msgid=?"; PreparedStatement pred=null; ResultSet rest=null; try { pred = conn.prepareStatement(sql); pred.setInt(1, msgid); rest =pred.executeQuery(); while(rest.next()){ msg msg =new msg(); msg.setUsername(rest.getString("username")); msg.setTitle(rest.getString("title")); msg.setMsgcontent(rest.getString("msgcontent")); msg.setMsg_create_date(rest.getDate("msg_create_date")); list.add(msg); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ closeAll(conn, pred, rest); } return list; } public void delmsg(Integer msgid){ Connection conn =getConnection(); String sql="delete from msg where msgid=?"; PreparedStatement pred=null; try { pred =conn.prepareStatement(sql); pred.setInt(1, msgid); pred.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ closeAll(conn, pred, null); } } public void send(msg msg){ Connection conn=getConnection(); String sql="insert into msg(username,title,msgcontent,state,sendto,msg_create_date) values(?,?,?,0,?,?)"; PreparedStatement ps=null; try { ps=conn.prepareStatement(sql); ps.setString(1, msg.getUsername()); ps.setString(2, msg.getTitle()); ps.setString(3, msg.getMsgcontent()); ps.setString(4, msg.getSendto()); ps.setDate(5, new java.sql.Date(new Date().getTime())); ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ closeAll(conn, ps, null); } } }
<%@page import="com.sf.entity.msg"%> <%@page import="com.sf.dao.msgDao"%> <%@page import="sun.security.jgss.LoginConfigImpl"%> <%@page import="com.sf.dao.usersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <% String msgid1=request.getParameter("msgid"); Integer msgid=Integer.parseInt(msgid1); msgDao msgDao =new msgDao(); msgDao.delmsg(msgid); response.sendRedirect("zhuye.jsp"); %> </body> </html>
<%@page import="sun.security.jgss.LoginConfigImpl"%> <%@page import="com.sf.dao.usersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <% String uname = request.getParameter("uname"); String upwd =request.getParameter("upwd"); String uid =request.getParameter("uid"); Integer id =Integer.parseInt(uid); usersDao usersDao=new usersDao(); if(usersDao.login(uname, upwd, id)){ session.setAttribute("uname", uname); request.getRequestDispatcher("zhuye.jsp").forward(request, response); } else{ out.print("登录失败,5s后跳转登录页面"); response.setHeader("refresh", "5;url=login.jsp"); } %> </body> </html>
<%@page import="com.sf.entity.msg"%> <%@page import="com.sf.dao.msgDao"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <% String magid =request.getParameter("msgid"); Integer msgid =Integer.parseInt(magid); msgDao msgDao =new msgDao(); msgDao.changestate(msgid); msg msg=new msg(); List<msg> list =new ArrayList<msg>(); list=msgDao.showContent(msgid); %> <table width="600px" border="2px solid red"> <tr> <th>发件人</th><th>标题</th><th>内容</th><th>发送时间</th> </tr> <% for(msg msg1:list){ %> <tr> <td><%out.print(msg1.getUsername()); %></td> <td><%out.print(msg1.getTitle()); %></td> <td><%out.print(msg1.getMsgcontent()); %></td> <td><%out.print(msg1.getMsg_create_date()); %></td> </tr> <%} %> </table> <a href="zhuye.jsp">继续浏览</a> </body> </html>
<%@page import="com.sf.entity.msg"%> <%@page import="com.sf.dao.msgDao"%> <%@page import="sun.security.jgss.LoginConfigImpl"%> <%@page import="com.sf.dao.usersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <% String uname=(String)session.getAttribute("uname"); String sendto = request.getParameter("sendto"); String title =request.getParameter("title"); String content =request.getParameter("content"); msg msg =new msg(); msg.setTitle(title); msg.setMsgcontent(content); msg.setUsername(uname); msg.setSendto(sendto); msgDao msgDao=new msgDao(); msgDao.send(msg); out.print("发送成功,即将跳回主页....."); response.setHeader("refresh", "5;url=zhuye.jsp"); %> </body> </html>
<%@page import="com.sf.dao.usersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <% String uid=request.getParameter("id"); Integer id=Integer.parseInt(uid); String uname=request.getParameter("uname"); String upwd =request.getParameter("upwd"); String upwd2 =request.getParameter("upwd2"); usersDao usersDao=new usersDao(); if(upwd.equals(upwd2)){ if(usersDao.id(id)){ out.print("已经有这个id了,即将跳回注册页....."); response.setHeader("refresh", "5;url=zhuce.jsp"); } else{ usersDao.zhuce(id, uname, upwd); out.print("注册成功,即将跳回登录页....."); response.setHeader("refresh", "5;url=login.jsp"); } } else{ out.print("两次密码不一致,即将跳回注册页....."); response.setHeader("refresh", "5;url=zhuce.jsp"); } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <script type="text/javascript"> function dologin(){ if(loginform.id.value==""){ alert("没有输入id"); return; } if(loginform.uname.value==""){ alert("没有输入用户名"); return; } if(loginform.upwd.value==""){ alert("没有输入密码"); return; } loginform.submit(); } </script> <form action="dologin.jsp" name="loginform" method="post"> id:<input type="number" value="1" name="uid"/></br> 用户名:<input type="text" value="zs" name="uname"/></br> 密码:<input type="password" value="11" name="upwd"/></br> <input type="button" value="登录" onclick="dologin()"/> <a href="zhuce.jsp">没有账号?立即注册</a> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> <% session.invalidate(); response.sendRedirect("login.jsp"); %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <script type="text/javascript"> function dologin(){ if(loginform.sendto.value==""){ alert("没有输入收件人"); return; } if(loginform.title.value==""){ alert("没有输入标题"); return; } if(loginform.content.value==""){ alert("没有输入内容"); return; } loginform.submit(); } </script> <form name="loginform" action="dosend.jsp" method="post"> 收件人:<input type="text" name="sendto" value="<%=request.getParameter("username") %>"><br> 主题: <input type="text" name="title" ><br> 内容 :<br><textarea rows="6" cols="50" name="content"></textarea> <br> <input type="submit" value="发送" onclick="dologin()"> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <script type="text/javascript"> function dozhuce(){ if(loginform2.id.value==""){ alert("没有输入id"); return; } if(loginform2.uname.value==""){ alert("没有输入用户名"); return; } if(loginform2.upwd.value==""){ alert("没有输入密码"); return; } if(loginform2.upwd2.value==""){ alert("没有确认密码"); return; } loginform2.submit(); } function backle(){ history.go(-1); } </script> <form action="dozhuce.jsp" name="loginform2" method="post"> id: <input type="number" name="id"/></br> 用户名:<input type="text" value="zs" name="uname"/></br> 密码:<input type="password" value="11" name="upwd"/></br> 确认密码:<input type="password" value="11" name="upwd2"/></br> <input type="button" value="注册" onclick="dozhuce()"/> <input type="button" value="返回登录" onclick="backle()"/> </form> </body> </html>
<%@page import="java.util.List"%> <%@page import="com.sf.dao.msgDao"%> <%@page import="com.sf.entity.msg"%> <%@page import="sun.security.jgss.LoginConfigImpl"%> <%@page import="com.sf.dao.usersDao"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <% String uname=(String)session.getAttribute("uname"); if(uname==null){ out.print("您未登录,即将跳回登陆页....."); response.setHeader("refresh", "5;url=login.jsp"); }else{ %> 欢迎你<%=uname %> <a href="loginout.jsp">退出登陆</a><br> <% msg msg=new msg(); msgDao msgDao =new msgDao(); List<msg> list =new ArrayList<msg>(); list=msgDao.listAll(uname); %> 邮件列表: <table width="600px" border="2px solid red"> <%if(list.size()==0){ out.print("你没有邮件"); }else{ %> <tr> <th>id</th><th>发件人</th><th>标题</th><th>已读未读状态</th><th>收件人</th><th>发送时间</th><th colspan="2">操作</th> </tr> <% for(msg msg1:list){ %> <tr> <td><%out.print(msg1.getMsgid()); %></td> <td><%out.print(msg1.getUsername()); %></td> <td><a href="domsgcontent.jsp?msgid=<%=msg1.getMsgid()%>"><%out.print(msg1.getTitle()); %></a></td> <td><%out.print(msg1.getState()); %></td> <td><%out.print(msg1.getSendto()); %></td> <td><%out.print(msg1.getMsg_create_date()); %></td> <td><a href="delmsg.jsp?msgid=<%=msg1.getMsgid()%>">删除</a></td> <td><a href="send.jsp?username=<%=msg1.getUsername()%>">回复</a></td> </tr> <%}}} %> </table> </body> </html>