第十二周Jsp作业
package com.yym.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.lmq.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.yym.entity.msg"%> <%@page import="com.yym
<%@page import="com.yym.entity.msg"%> <%@page import="com.yym.Dao.msgDao"%> <%@page import="sun.security.jgss.LoginConfigImpl"%> <%@page import="com.yym.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>
.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="java.util.List"%> <%@page import="com.yym.Dao.msgDao"%> <%@page import="com.yym.Dao.msgDao"%> <%@page import="sun.security.jgss.LoginConfigImpl"%> <%@page import="com.yym.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>
<%@ 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>