jsp第八次作业

邮箱登录、注册、查看邮箱,写邮箱,删除, 回复

 registe.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>



</head>

<body>
    <form action="doregiste.jsp">
        用户名:<input type="text" name="username" /> </br> 密 码 <input type="password"
            name="password" /> </br> 邮箱 <input type="text" name="email" /> </br> <input
            type="submit" value="注册" /> <input type="button" value="登录"
            onclick="fun1()" />
    </form>
</body>
<script type="text/javascript">
    function fun1() {
        location.href = "index.jsp"
    }
</script>
</html>

doregiste.jsp

<%@page import="com.email.dao.UsersDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String email = request.getParameter("email");
    UsersDao ud = new UsersDao();
    int i = ud.insert(username, password, email);
    if (i > 0) {
        response.sendRedirect("index.jsp");
    } else {
        response.sendRedirect("register.jsp");
    }
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


</head>

<body>

</body>
</html>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

</head>

<body>
    <form action="dologin.jsp">
        用户名:<input type="text" name="username" /> </br> 密码:<input type="password"
            name="password" /> </br> <input type="submit" value="登录" /> <input
            type="button" value="注册" onclick="fun1()" />
    </form>
</body>
<script type="text/javascript">
    function fun1() {
        location.href = "register.jsp"
    }
</script>
</html>

dologin.jsp

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


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>




<%
    String username = request.getParameter("username");

    String password = request.getParameter("password");
    System.out.println(username + " " + password);
    UsersDao ud = new UsersDao();
    boolean flag = ud.login(username, password);
    if (flag) {
        session.setAttribute("username", username);
        session.setAttribute("password", password);
        response.sendRedirect("success.jsp");
    } else {
        response.sendRedirect("index.jsp");
    }
%>

</head>

<body>

</body>
</html>

success.jsp

<%@page import="com.email.bean.msg"%>
<%@page import="com.email.dao.MsgDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


</head>
<body>

    
        
            <%
        MsgDao md = new MsgDao();
        String uname = (String) session.getAttribute("username");
        List<msg> list = md.selectMyAll(uname);
    %>
    欢迎你<%=uname%> <a href="write.jsp">写邮件</a>
    <table border="1">
    <tr>
    
    <td>标题    </td>
    
    <td>是否已读</td>
    <td>发件人</td>
    <td>时间</td>    
    <td>操作</td>
    <td>操作</td>
    </tr>
<%for(int i=0;i<list.size();i++){ %>    
    <tr>
    
    <td><a href="show.jsp?msgid=<%=list.get(i).getMsgid()%>"><%=list.get(i).getTitle() %></a></td>
    
    <td><%int x=list.get(i).getState();
            if(x==0){%>
            未读
            <%}else{ %>
            已读
            <%} %>
    </td>
    <td><%=list.get(i).getUsername() %></td>
    <td><%=list.get(i).getMsg_create_date() %></td>
    <td><a href="delete.jsp?msgid=<%=list.get(i).getMsgid()%>">删除</a>    </td>
    <td><a href="replay.jsp?username=<%=list.get(i).getUsername()%>">回复</a></td>

    </tr>
    
<%} %>
    </table>
</body>
</html>

write.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>
</head>
<body>
<form action="insertwrite.jsp">
    名字:<input type="text" name="sendto">
    </br>
    主题<input type="text" name="title">
    </br>
    内容:</br><textarea rows="10" cols="30" name="content"></textarea>
    <input type="submit" value="发送">
    </form>
</body>
</html>

insertwrite.jsp

<%@page import="com.email.dao.MsgDao"%>
<%@page import="com.email.bean.msg"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>
</head>
<body>
    <%
        msg m = new msg();
        String sendto = request.getParameter("sendto");
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String username = (String) session.getAttribute("username");
        Date d = new Date();
        long time = d.getTime();
        m.setMsg_create_date(new java.sql.Date(time));
        m.setUsername(username);
        m.setSendto(sendto);
        m.setTitle(title);
        m.setMsgcontent(content);
        m.setState(0);
        MsgDao md = new MsgDao();
        int i = md.insertWrite(m);
        if(i>0){
            response.sendRedirect("success.jsp");
        } else {
            response.sendRedirect("write.jsp");
        }
        
    %>
</body>
</html>

replay.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>
</head>
<%
    String sendto = request.getParameter("username");//收件人
    String username = session.getAttribute("username").toString();//发件人
    

%>
<body>
    <form action="doreplay.jsp">
    收件人:<input type="text" value="<%=request.getParameter("username")%>" name="username"></br>
    标题:    <input type="text" name = "title"></br>
    内容:</br><textarea rows="10" cols="30" name="content"></textarea>
    <input type="submit" value="发送">
    </form>
</body>
</html>

doreplay.jsp

<%@page import="com.email.dao.MsgDao"%>
<%@page import="com.email.bean.msg"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>
</head>
<body>
<%
    String sendto = request.getParameter("username");//收件人
    String username = session.getAttribute("username").toString();//发件人
    String title = request.getParameter("title");//收件人
    String content = request.getParameter("content");//收件人
    long time = new Date().getTime();
    msg m = new msg(username,title,content,0,sendto,new java.sql.Date(time));
    MsgDao md = new MsgDao();
    int i = md.insertWrite(m);
    if (i > 0) {
        response.sendRedirect("success.jsp");
    } else {
        response.sendRedirect("replay.jsp");
    }
%>
</body>
</html>

show.jsp

<%@page import="com.email.dao.MsgDao"%>
<%@page import="com.email.bean.msg"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>
</head>
<body>
<%
    String sendto = request.getParameter("username");//收件人
    String username = session.getAttribute("username").toString();//发件人
    String title = request.getParameter("title");//收件人
    String content = request.getParameter("content");//收件人
    long time = new Date().getTime();
    msg m = new msg(username,title,content,0,sendto,new java.sql.Date(time));
    MsgDao md = new MsgDao();
    int i = md.insertWrite(m);
    if (i > 0) {
        response.sendRedirect("success.jsp");
    } else {
        response.sendRedirect("replay.jsp");
    }
%>
</body>
</html>

delete.jsp

<%@page import="com.email.dao.MsgDao"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>
</head>
<body>
<%
    int msgid  = Integer.parseInt(request.getParameter("msgid"));
    System.out.println(msgid);
    MsgDao md = new MsgDao();
    int i =md.delete(msgid);
    if (i > 0) {
        response.sendRedirect("success.jsp");
    } else {
        response.sendRedirect("register.jsp");
    }
%>
</body>
</html>

msg.java

package com.email.bean;

import java.sql.Date;

public class msg {
    private int msgid;
    private String username;
    private String title;
    private String msgcontent;
    private int state;
    private String sendto;
    private Date msg_create_date;
    
    public msg() {
        super();
    }
    
    public msg(String username, String title, String msgcontent, int state, String sendto, Date msg_create_date) {
        super();
        this.username = username;
        this.title = title;
        this.msgcontent = msgcontent;
        this.state = state;
        this.sendto = sendto;
        this.msg_create_date = msg_create_date;
    }

    public msg(int msgid,String username, String title, String msgcontent, int state,
            String sendto, Date msg_create_date) {
        super();
        this.msgid = msgid;
        this.username = username;
        this.title = title;
        this.msgcontent = msgcontent;
        this.state = state;
        this.sendto = sendto;
        this.msg_create_date = msg_create_date;
    }
    public int getMsgid() {
        return msgid;
    }
    public void setMsgid(int msgid) {
        this.msgid = msgid;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getMsgcontent() {
        return msgcontent;
    }
    public void setMsgcontent(String msgcontent) {
        this.msgcontent = msgcontent;
    }
    public int getState() {
        return state;
    }
    public void setState(int state) {
        this.state = state;
    }
    public String getSendto() {
        return sendto;
    }
    public void setSendto(String sendto) {
        this.sendto = sendto;
    }
    public Date getMsg_create_date() {
        return msg_create_date;
    }
    public void setMsg_create_date(Date msg_create_date) {
        this.msg_create_date = msg_create_date;
    }
    
    
}

users.java

    package com.email.bean;
    
    public class users {
        private String username;
        private String password;
        private String email;
        public users() {
            super();
        }
        public users(String username, String password, String email) {
            super();
            this.username = username;
            this.password = password;
            this.email = email;
        }
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
        
        
    }

BaseDao.java

package com.email.dao;

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

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", "root");
                conn.setAutoCommit(false);
            } 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();
        }
    }

}

MsgDao.java

package com.email.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.email.bean.msg;

public class MsgDao extends BaseDao{
    public List<msg> selectMyAll(String username){
         Connection conn = null;
         PreparedStatement ps =null;
         ResultSet rs = null;
         List<msg> list = new ArrayList<msg>();
         try {
             conn = getConnection();
             ps = conn.prepareStatement("select * from msg where sendto=? ");
             ps.setString(1, username);
    
             rs = ps.executeQuery();
                
             while(rs.next()){
                 list.add(new msg(rs.getInt("msgid"),rs.getString("username"),rs.getString("title"),rs.getString("msgcontent"),rs.getInt("state"), rs.getString("sendto"),rs.getDate("msg_create_date")));
                 System.out.println(rs.getInt("msgid"));
             }
             return list;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            closeAll(conn, ps, rs);
        }
         return list;
        }
    
        public int insertWrite(msg m) {
             Connection conn = null;
             PreparedStatement ps =null;
             int i = 0;
             try {
                 conn = getConnection();
                 ps = conn.prepareStatement("insert into msg (username,title,msgcontent,state,sendto,msg_create_date)values(?,?,?,?,?,?)");
                 ps.setString(1, m.getUsername());
                 ps.setString(2, m.getTitle());
                 ps.setString(3, m.getMsgcontent());
                 ps.setInt(4, m.getState());
                 ps.setString(5, m.getSendto());
                 ps.setDate(6, m.getMsg_create_date());
                 i = ps.executeUpdate();
                 conn.commit();
                 return i;
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                closeAll(conn, ps, null);
            }
             return i;
        }
        
        public int delete(int msgid) {
             Connection conn = null;
             PreparedStatement ps =null;
             int i = 0;
             try {
                 conn = getConnection();
                 ps = conn.prepareStatement("delete from msg where msgid = ?");
                 ps.setInt(1, msgid);
                 i = ps.executeUpdate();
                 conn.commit();
                 return i;
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                closeAll(conn, ps, null);
            }
             return i;
        }
        
        
        public msg getMsgById(int id){
            msg m=new msg();
            Connection conn = getConnection();
            String sql = "SELECT * from msg where msgid=?";
            PreparedStatement ps=null;
            ResultSet rs=null;
            try {
                ps = conn.prepareStatement(sql);
                ps.setInt(1, id);
                 rs=ps.executeQuery();
                while(rs.next()){
                    
                    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));                
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                closeAll(conn, ps, rs);
            }
            
            
            return m;
            
            
            
        }
        
        
        
        
        
        //修改邮件的已读状态  把1改0
        public void updateMsg(int id){
            Connection con = null;
            PreparedStatement ps = null;
            try {
                 con = getConnection();
                String sql="update msg set state=1 where msgid=?";
                 ps = con.prepareStatement(sql);
                ps.setInt(1, id);//给sql语句的问号赋值
                ps.executeUpdate();
                con.commit();
                
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                closeAll(con, ps, null);
            }
            
            
            
        }    
        

    }

UsersDao.java

package com.email.dao;

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


public class UsersDao  extends BaseDao{
    public int insert(String username,String password,String email){
     Connection conn = null;
     PreparedStatement ps =null;
     int i = 0;
     try {
         conn = getConnection();
         ps = conn.prepareStatement("insert into users (username,password,email)values(?,?,?)");
         ps.setString(1, username);
         ps.setString(2, password);
         ps.setString(3, email);
         i = ps.executeUpdate();
         conn.commit();
         return i;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        closeAll(conn, ps, null);
    }
     return i;
    }
    
    
    public boolean login(String username,String password){
         Connection conn = null;
         PreparedStatement ps =null;
         ResultSet rs = null;
    
         try {
             conn = getConnection();
             ps = conn.prepareStatement("select username,password from users where username = ? and password = ?");
             ps.setString(1, username);
             ps.setString(2, password);
             rs = ps.executeQuery();
             if(rs.next()){
                 return true;
             }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            closeAll(conn, ps, rs);
        }
         return false;
        }
}

 

 

 

posted @ 2021-04-28 13:09  ~~_~~  阅读(72)  评论(0编辑  收藏  举报