第十二周作业

1.实现 删除 回复邮件

package com.ymz;

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 com.gd.entity.Msg;

public class MsgDao extends BaseDao {

	// 插入邮件
	public void addMsg(Msg m) {
		Connection conn = getConnection();
		String sql = "insert into msg(username,title,msgcontent,state,sendto,msg_create_date) values(?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, m.getUsername());
			ps.setString(2, m.getTitle());
			ps.setString(3, m.getMsgcontent());
			ps.setInt(4, 1);
			ps.setString(5, m.getSendto());
			ps.setDate(6, new java.sql.Date(new Date().getTime()));// 系统当前时间
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closeAll(conn, ps, null);
		}

	}

	// 删除邮件
	public void delMail(int id) {
		Connection conn = getConnection();
		String sql = "delete from msg where msgid=?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeAll(conn, ps, null);
		}

	}
	
//	public static void main(String[] args) {
//		MsgDao md=new MsgDao();
//		md.delMail(3);
//	}

	// 修改邮件状态

	// 按照接收者查询全部邮件
	public List<Msg> getMailByReceiver(String name) {
		List<Msg> list = new ArrayList<Msg>();
		Connection conn = getConnection();
		String sql = "select * from msg where sendto=?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			rs = ps.executeQuery();
			while (rs.next()) {
				Msg m = new Msg();
				m.setMsgid(rs.getInt(1));
				m.setUsername(rs.getString(2));
				m.setTitle(rs.getString(3));
				m.setMsgcontent(rs.getString(4));
				m.setState(rs.getInt(5));
				m.setSendto(rs.getString(6));
				m.setMsg_create_date(rs.getDate(7));
				list.add(m);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closeAll(conn, ps, rs);
		}
		return list;
	}

}

  

<%@page import="com.ymz.MsgDao"%>
<%@page import="com.ymz.Msg"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>




<title>My JSP 'index.jsp' starting page</title>

</head>

<body>
<%String uname=(String)session.getAttribute("uname"); %>
首页!!欢迎你<%=uname %>
<a href="write.jsp">写邮件</a>
<table border="1">
<tr>
<td>发件人</td><td>主题</td><td>状态</td><td>时间</td>
<td>操作</td><td>操作</td>

<%
	MsgDao md=new MsgDao();
	List<Msg> list=md.getMailByReceiver(uname);	
	for(int i=0;i<list.size();i++){
	%>
	<tr>
	<td><%=list.get(i).getUsername() %></td>
	<td><%=list.get(i).getTitle() %></td>
	<td><%if(list.get(i).getState()==1){%>
	<img src="images/sms_unReaded.png"/>
	<%}else{ %>	
	<img src="images/sms_readed.png"/>
	<%} %>
	</td>
	<td><%=list.get(i).getMsg_create_date()%></td>
	<td><a href="delete.jsp?id=<%=list.get(i).getMsgid()%>">删除</a></td>
	<td><a href="write.jsp?reply=<%=list.get(i).getUsername() %>">回复</a></td>
	
	
	</tr>
	
	
<%	
	}
 %>

</table>






</body>
</html>

  

<%@page import="com.ymz.MsgDao"%>
<%@page import="com.ymz.Msg"%>
<%@page import="com.ymz.UsersDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	request.setCharacterEncoding("utf-8");
	 
	int id=Integer.parseInt(request.getParameter("id"));
	MsgDao md=new MsgDao();
	md.delMail(id);	
//	out.print("刪除成功.....");
//	response.setHeader("refresh", "2;url=main.jsp");
	response.sendRedirect("main.jsp");
	
	
 %>

  

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
	<form action="dowrite.jsp" method="post">	
	收件人:<input type="text" name="receiver"  value="<%=request.getParameter("reply")%>"><br> 
	主题: <input  type="text" name="title" ><br>
	内容<textarea rows="6" cols="20" name="content"></textarea>
<br>
<input type="submit" value="发送"> 
	</form>
</body>
</html>

  

<%@page import="com.ymz.MsgDao"%>
<%@page import="com.ymz.Msg"%>
<%@page import="com.ymz.UsersDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	request.setCharacterEncoding("utf-8");
	String uname=(String)session.getAttribute("uname");
	String sendto=request.getParameter("receiver");
	String title=request.getParameter("title");
	String content=request.getParameter("content");
	
	Msg m=new Msg();
	m.setMsgcontent(content);
	m.setUsername(uname);
	m.setSendto(sendto);
	m.setTitle(title);
	
	MsgDao md=new MsgDao();
	md.addMsg(m);
	
	out.print("发送成功.....");
	response.setHeader("refresh", "5;url=main.jsp");
	
	
 %>

  

 

 

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

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="com.ymz.MsgDao"%>
<%@ page import="com.ymz.Msg"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.ArrayList"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<style>
table {
    border-collapse: collapse;
    width:300px;
}
 
table, table tr th, table tr td {
    border: 1px solid #000000;
}
table td{
    text-align:center;
}
img{
width:30px;
 
}
a{
text-decoration: none;
}
a:link{
color:black;
}
</style>
</head>
<body>
<table border="1">
<tr>
<td>发件人</td><td>主题</td><td>状态</td><td>时间</td><td>操作</td><td>操作</td>
 
<%
int id=Integer.parseInt(request.getParameter("id"));
    MsgDao md = new MsgDao();
    Msg m = md.getMailById(id);
        out.print("<tr><td>"+ m.getSender() + "</td>");
        out.print("<td><a href=\"detail.jsp?id="+m.getEid() +"\">" + m.getTitle() + "</a></td>");
        if (m.getState() == 1) {
            out.print("<td><img src=\"img/wd.png\"/></td>");
        } else {
            out.print("<td><img src=\"img/yd.png\"/></td>");
        }
        out.print("<td>" + m.getSending_time() + "</td>");
        out.print("<td><a href=\"delete.jsp?id="+m.getEid()+"\">删除</a></td>");
        out.print("<td><a href=\"write.jsp?reply="+m.getEid()+"\">回复</a></td></tr>");
%>
<form action="main.jsp" method="post">
<input type="submit" value="返回主页">
</form>
</tr>
</table>
</body>
</html>

  

 

posted @ 2022-05-22 21:29  樽梦  阅读(5)  评论(0编辑  收藏  举报