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;
import java.sql.SQLException;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class BaseDao {


	//获取连接
	protected Connection getConnection(){
		Connection conn=null;
			try {
				Class.forName("com.mysql.jdbc.Driver");
				// 2.建立连接
				conn = DriverManager.getConnection(
						"jdbc:mysql://localhost:3306/test", "root", "123456");
			} catch (Exception e) {
				e.printStackTrace();
			} 
			return conn;
	}	
	

	
	
	//关闭连接
	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 (SQLException e) {
			e.printStackTrace();
		}
	}
	
}

  

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

public class MsgDao extends BaseDao{
    //关于邮件的增删改查    
    //添加邮件(写邮件,回复邮件都调用,邮件状态为1(未读),时间为系统当前时间)    
    public void addMsg(Msg m){
        Connection conn=getConnection();
        String sql="insert into msg(username,title,msgcontent,state,sendto,msg_create_date) values(?,?,?,1,?,?)";
        PreparedStatement ps=null;
        try {
             ps=conn.prepareStatement(sql);
             ps.setString(1, m.getUsername());
             ps.setString(2, m.getTitle());
             ps.setString(3, m.getMsgcontent());
             ps.setString(4, m.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);
        }        
    }
    public static void main(String[] args) {
        MsgDao md=new MsgDao();
        md.delMsg(4);
    }    
    
    //根据id删除邮件
      public void delMsg(int id) {
            Connection con = getConnection();
            String sql = "delete from msg where msgid=" + id;
            PreparedStatement ps = null;
            try {
                ps = con.prepareStatement(sql);
                ps.executeUpdate();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                closeAll(con, ps, null);
            }
        }
    
      // 根据id修改邮件状态(统一改成1)
           public Msg chakan(int msgid) {
               Msg m = null;
               Connection con = getConnection();
               ResultSet rs = null;
               PreparedStatement ps = null;
               String sql = "select * from msg where msgid=?";
               try {
                   ps = con.prepareStatement(sql);
                   ps.setInt(1, msgid);
                   rs = ps.executeQuery();
                   while (rs.next()) {
                       m = new Msg();
                       m.setMsgid(rs.getInt("msgid"));
                       m.setUsername(rs.getString("username"));
                       m.setTitle(rs.getString("title"));
                       m.setMsgcontent(rs.getString("msgcontent"));
                       m.setState(rs.getInt("state"));
                       m.setSendto(rs.getString("sendto"));
                       m.setMsg_create_date(rs.getDate("msg_create_date"));
                   }
               } catch (SQLException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               } finally {
                   closeAll(con, ps, rs);
               }
               return m;
           }
       
           public int xiugai(int msgid) {
               int t = -1;
               Connection con = getConnection();
               PreparedStatement ps = null;
               String sql = "update msg set state=1 where msgid=?";
               try {
                   ps = con.prepareStatement(sql);
                   ps.setInt(1, msgid);
                   t = ps.executeUpdate();
               } catch (SQLException e) {
                 // TODO Auto-generated catch block
                   e.printStackTrace();
              } finally {
                  closeAll(con, ps, null);
             }
             return t;
         }
     
     
    
    //根据收件人查看全部邮件
    public List<Msg> getMailByReceiver(String name){
        List<Msg> list=new ArrayList<Msg>();
        Connection con=getConnection();
        String sql="select * from msg where sendto=?";
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, name);
             rs=ps.executeQuery();
            while(rs.next()){
                //每读取一行,创建一个msg对象,对象放到集合中
                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(con, ps, rs);
        }        
        return list;
    }
    
    
    
    

}
package com.gd.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gd.entity.Users;

public class UsersDao extends BaseDao{
    //关于用户的增删改查,用到什么功能鞋什么
    
    //登陆
    public boolean login(String name,String pwd){
        boolean f=false;
        Connection conn=getConnection();
        String sql="select * from users where uname=? and upwd=?";
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);//第一个?赋值为name
            ps.setString(2, pwd);
            rs=ps.executeQuery();
            if(rs.next())//查到结果了
                f=true;            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            closeAll(conn, ps, rs);
        }
        return f;
    }    
    
    //注册
    public int Register(String name,String pwd){
        int i=-1;
        PreparedStatement pred=null;
        Connection con=getConnection();
        String sql="insert into users(uname,password)values(?,?)";
        try {
            pred= con.prepareStatement(sql);
            pred.setString(1, name);
            pred.setString(2, pwd);
            i=pred.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            closeAll(con, pred, null);
        }
        return i;
    }
    

}
<%@page import="com.gd.entity.Msg"%>
<%@page import="com.gd.dao.MsgDao"%>
<%@page import="com.gd.dao.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.delMsg(id);
    
    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>
<script type="text/javascript">
        function validate(){
            if(loginForm.uname.value==""){
                alert("账号不能为空!");
                return;
            }
            if(loginForm.upwd.value==""){
                alert("密码不能为空!");
                return;
            }
            loginForm.submit();
        }
    </script>


    <form name="loginForm" action="dologin.jsp" method="post">
        
    用户名:<input type="text" name="uname" value="zs"><br> 
    密码: <input  type="password" name="upwd"  value="zs">
    
        <input type="button" value="登录" onClick="validate()">    



    </form>

<a href="reg.jsp">立即注册</a>


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

<html>
<head>

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

</head>

<body>
<script type="text/javascript">
        function validate(){
            if(loginForm.uname.value==""){
                alert("账号不能为空!");
                return;
            }
            if(loginForm.upwd.value==""){
                alert("密码不能为空!");
                return;
            }
            loginForm.submit();
        }
    </script>


    <form name="loginForm" action="dologin.jsp" method="post">
        
    用户名:<input type="text" name="uname" value="zs"><br> 
    密码: <input  type="password" name="upwd"  value="zs">
    
        <input type="button" value="登录" onClick="validate()">    



    </form>

<a href="reg.jsp">立即注册</a>


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

<html>
<head>

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

</head>

<body>
<script type="text/javascript">
        function validate(){
            if(loginForm.uname.value==""){
                alert("账号不能为空!");
                return;
            }
            if(loginForm.upwd.value==""){
                alert("密码不能为空!");
                return;
            }
            loginForm.submit();
        }
    </script>


    <form name="loginForm" action="dologin.jsp" method="post">
        
    用户名:<input type="text" name="uname" value="zs"><br> 
    密码: <input  type="password" name="upwd"  value="zs">
    
        <input type="button" value="登录" onClick="validate()">    



    </form>

<a href="reg.jsp">立即注册</a>


</body>
</html>
<%@page import="com.gd.entity.Msg"%>
<%@page import="com.gd.dao.MsgDao"%>
<%@page import="com.gd.dao.UsersDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    request.setCharacterEncoding("utf-8");
    
    String uname=(String)session.getAttribute("uname");// 发件人在session中获取
    String sendto=request.getParameter("sendto");
    String title=request.getParameter("title");
    String content=request.getParameter("content");
    
    Msg m=new Msg();
    m.setTitle(title);
    m.setMsgcontent(content);
    m.setUsername(uname);
    m.setSendto(sendto);
    MsgDao md=new MsgDao();
    md.addMsg(m);    
    out.print("发送成功,即将跳回首页.....");
    response.setHeader("refresh", "3;url=main.jsp");        
 %>
<%@page import="com.gd.entity.Msg"%>
<%@page import="com.gd.dao.MsgDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>


</head>

<body>
<%
    MsgDao md=new MsgDao();
    String uname=(String)session.getAttribute("uname");
    List<Msg> list=md.getMailByReceiver(uname);
%>
欢迎你<%=uname %>    <a href="write.jsp">写邮件</a>

<table border="1px">
<tr>
<td>发件人</td><td>主题</td><td>状态</td><td>时间</td>
<td>操作</td><td>操作</td>
</tr>

<%
    for(int i=0;i<list.size();i++){
 %>
<tr>
<td><%=list.get(i).getUsername() %></td>
<td><a href="detail.jsp?id=<%=list.get(i).getMsgid()%>"><%=list.get(i).getSendto()%></a> </td>           </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="write.jsp?reply=<%=list.get(i).getUsername()%>">回复</a></td>
<td><a href="del.jsp?id=<%=list.get(i).getMsgid()%>">删除</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">
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
 <base href="<%=basePath%>">
  <body>
 <h1>注册</h1>
    <script>
        function yz() {
            if (form.uname.value == "") {
                alert('用户名不能为空');
                return;
            }
            if (form.password.value == "") {
                alert('密码不能为空');
                return;
            }
            form.submit();
        }
    </script>
    <form action="doregister.jsp" method="post" name="form">
        <table>
            <tr>
                <td>用户名</td>
                <td><input type="text" name="uname">
                </td>
            </tr>
            <tr>
                <td>密码</td>
                <td><input type="password" name="password">
                </td>
            </tr>
            <tr>
                <td><input type="button" value="注册" onclick="yz()">
                </td>
                <td><a href="denglu.jsp">登录</a>
                </td>
            </tr>
        </table>
    </form>

  </body>
</html>
<%@ 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="sendto" 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>

 

 

 

 

 

 

posted on 2022-05-22 10:28  Hna  阅读(9)  评论(0编辑  收藏  举报