jsp第十三周作业

1.第十二周上机作业(邮件功能)的控制层代码改用为servlet实现。

package Email.entityPackage;

import java.util.Date;

public class Email {
    private int eid;            //id
    private String ename;       //收件人
    private String etitle;      //标题
    private String econtent;    //内容
    private int estate;         //状态
    private String efa;         //发件人
    private Date etime;         //时间
    
    public int getEid() {
        return eid;
    }
    public void setEid(int eid) {
        this.eid = eid;
    }
    public String getEname() {
        return ename;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public String getEtitle() {
        return etitle;
    }
    public void setEtitle(String etitle) {
        this.etitle = etitle;
    }
    public String getEcontent() {
        return econtent;
    }
    public void setEcontent(String econtent) {
        this.econtent = econtent;
    }
    public int getEstate() {
        return estate;
    }
    public void setEstate(int estate) {
        this.estate = estate;
    }
    public String getEfa() {
        return efa;
    }
    public void setEfa(String efa) {
        this.efa = efa;
    }
    public Date getEtime() {
        return etime;
    }
    public void setEtime(Date etime) {
        this.etime = etime;
    }
    

}
package Email.entityPackage;

public class Users {

    private int uid;
    private String uname;
    private String upwd;
    
    public int getUid() {
        return uid;
    }
    public void setUid(int uid) {
        this.uid = uid;
    }
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getUpwd() {
        return upwd;
    }
    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }
}
package Email.daoPackage;

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

public class BaseDao {
    //连接数据库
    public Connection getConnection(){
        Connection con=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }
    
    //关闭数据库
    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 (Exception e) {
            e.printStackTrace();
        }
        
    }

}
package Email.daoPackage;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import Email.entityPackage.Email;

public class EmailDao extends BaseDao{
    // 关于邮件的增删改查    
    // 1.添加邮件(写邮件,回复邮件都调用,邮件状态为0(未读),时间为系统当前时间)
    public void addEmail(Email email){
        Connection con=getConnection();
        String sql="insert into email(ename,etitle,econtent,estate,efa,etime) values(?,?,?,0,?,?)";
        PreparedStatement ps=null;
        try {
            ps=con.prepareStatement(sql);
            ps.setString(1, email.getEname());
            ps.setString(2, email.getEtitle());
            ps.setString(3, email.getEcontent());
            ps.setString(4, email.getEfa());
            ps.setDate(5, new java.sql.Date(new Date().getTime()));
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            closeAll(con, ps, null);
        }
    }
    
    public static void main(String[] args) {
        EmailDao ed=new EmailDao();
        //ed.delEmail(6);
    }

    // 2.根据id删除邮件
    public void delEmail(int id){
        Connection con=getConnection();
        String sql="delete from email where eid="+id;
        PreparedStatement ps=null;
        try {
            ps=con.prepareStatement(sql);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            closeAll(con, ps, null);
        }
    }

    // 3.修改邮件状态
    public void GauBian(int id){
        Connection con=getConnection();
        String sql="update email set estate='1' where eid=?";
        PreparedStatement ps=null;
        try {
            ps=con.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            closeAll(con, ps, null);
        }
    }

    // 4.按照接收者查询全部邮件
    public List<Email> getAllEmail(String name){
        List<Email> EmailList=new ArrayList<Email>();
        Connection con=getConnection();
        String sql="select * from email where ename=?";
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps=con.prepareStatement(sql);
            ps.setString(1, name);
            rs=ps.executeQuery();
            while(rs.next()){
                Email em=new Email();
                em.setEid(rs.getInt(1));
                em.setEname(rs.getString(2));
                em.setEtitle(rs.getString(3));
                em.setEcontent(rs.getString(4));
                em.setEstate(rs.getInt(5));
                em.setEfa(rs.getString(6));
                em.setEtime(rs.getDate(7));
                EmailList.add(em);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            closeAll(con, ps, rs);
        }
        return EmailList;
    }
    
    // 5.查看一封邮件
    public Email getOneEmail(int id){
        Connection con=getConnection();
        String sql="select eid,ename,etitle,econtent,efa,etime from email where eid=?";
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps=con.prepareStatement(sql);
            ps.setInt(1, id);
            rs=ps.executeQuery();
            while (rs.next()) {
                Email em=new Email();
                em.setEid(rs.getInt(1));
                em.setEname(rs.getString(2));
                em.setEtitle(rs.getString(3));
                em.setEcontent(rs.getString(4));
                em.setEfa(rs.getString(5));
                em.setEtime(rs.getDate(6));
                return em;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            closeAll(con, ps, rs);
        }
        return null;
    }

}
package Email.daoPackage;

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

public class UsersDao extends BaseDao{
    //登录
    public boolean DengLu(String name,String pwd) {
        boolean f=false;
        Connection con=getConnection();
        String sql="select * from users where uname=? and upwd=?";
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps=con.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, pwd);
            rs=ps.executeQuery();
            if(rs.next())
                f=true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            closeAll(con, ps, rs);
        }
        return f;
    }
    
    //注册
    public int ZhuCe(String name,String pwd){
        int i=-1;
        Connection con=getConnection();
        String sql="insert into users(uname,upwd) values(?,?)";
        PreparedStatement ps=null;
        try {
            ps=con.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, pwd);
            i=ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            closeAll(con, ps, null);
        }
        return i;
    }

}

登录

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

<body>
    <form action="panduan1.jsp" method="post" name="one">
        请输入用户名:<input name="name" type="text"><br>
        请输入密码:&nbsp;&nbsp;&nbsp;&nbsp;<input name="password" type="password"><br>
        <input type="button" value="登录" onclick="denglu()"> 
        <a href="Email2.jsp">注册新用户 </a>
        <script type="text/javascript">
            function denglu() {
                if (one.name.value == "") {
                    alert("用户名不能为空!");
                    return;
                }
                if (one.password.value == "") {
                    alert("密码不能为空!");
                    return;
                }
                one.submit();
            }
        </script>
    </form>
</body>
</html>

注册

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

<body>
    <%
        request.setCharacterEncoding("utf-8");
        String uname = request.getParameter("name");
        String upassword = request.getParameter("password");
        UsersDao ud = new UsersDao();
        if (ud.DengLu(uname, upassword)) {
            session.setAttribute("user", uname);
            request.getRequestDispatcher("main.jsp").forward(request,response);
        } else {
            out.print("登陆失败,3秒后返回登录页!");
            response.setHeader("refresh", "3;url=Email1.jsp");
        }
    %>
</body>
</html>
<%@page import="Email.daoPackage.UsersDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>panduan2.jsp</title>
</head>

<body>
    <%
        request.setCharacterEncoding("utf-8");
        String uname = request.getParameter("name");
        String upassword1 = request.getParameter("password1");

        UsersDao ud = new UsersDao();
        int i = ud.ZhuCe(uname, upassword1);
        if (i > 0) {
            out.print("注册成功,3秒后跳转登录页!");
            response.setHeader("refresh", "3;url=Email1.jsp");
        } else {
            out.print("注册失败,3秒后返回注册页!");
            response.setHeader("refresh", "3;url=Email2.jsp");
        }
    %>
</body>
</html>

主页

<%@page import="Email.entityPackage.Email"%>
<%@page import="Email.daoPackage.EmailDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>  
    <title>main.jsp</title>
  </head>
  
  <body>
    <%
    EmailDao mail=new EmailDao();
    String user=(String) session.getAttribute("user");
    List<Email> MailList=mail.getAllEmail(user);
     %>
 
    欢迎你:<%=user %> &nbsp;&nbsp;&nbsp;&nbsp;<a href="Write.jsp">写邮件</a>
    <br><br>
    <table border="1px" cellpadding="15px" align="center">
    <tr>
     <td>编号</td>
     <td>收件人</td>
     <td>标题</td>
     <td>状态</td>
     <td>发件人</td>
     <td>时间</td>
     <td>操作</td>
     <td>操作</td>
    </tr>
    <%for(int i=0;i<MailList.size();i++){ %>
    <tr>
     <td><%out.print(MailList.get(i).getEid()); %></td>
     <td><%out.print(MailList.get(i).getEname());%></td>
     <td><a href="Look.jsp?id=<%=MailList.get(i).getEid()%>"><%out.print(MailList.get(i).getEtitle()); %></a></td>
     <td>
     <%if(MailList.get(i).getEstate()==0){ %>
     <img src="../images/weidu.png"/>
     <%}else{ %>
     <img src="../images/yidu.png"/>
     <%} %>
     </td>
     <td><%out.print(MailList.get(i).getEfa()); %></td>
     <td><%out.print(MailList.get(i).getEtime()); %></td>
     <td><a href="Write.jsp?reply=<%=MailList.get(i).getEfa()%>">回复</a></td>
     <td><a href="Del.jsp?id=<%=MailList.get(i).getEid()%>">删除</a></td>
    </tr>
    <%} %>
    </table>
    
    <br><a href="bye.jsp">退出登录</a>
  </body>
</html>

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

<body>
    <form action="Wpanduan.jsp" method="post" >
        收件人:<input type="text" name="shou" value="<%=request.getParameter("reply")%>"><br> 
        主 题:&nbsp;&nbsp;<input type="text" name="head"><br> 
        内 容:<br>
        <textarea rows="10" cols="30" name="body"></textarea>
        <br> <input type="submit" value="发送邮件">
        <a href="main.jsp">返回</a>
    </form>
</body>
</html>
<%@page import="Email.daoPackage.EmailDao"%>
<%@page import="Email.entityPackage.Email"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
    <title>Wpanduan.jsp</title>
  </head>
  
  <body>
    <%
    request.setCharacterEncoding("utf-8");
    
    String name=(String)session.getAttribute("user");// 发件人在session中获取
    String shou=request.getParameter("shou");        //收件人
    String title=request.getParameter("head");
    String content=request.getParameter("body");
    
    Email e=new Email();
    e.setEfa(name);
    e.setEtitle(title);
    e.setEcontent(content);
    e.setEname(shou);
    
    EmailDao ed=new EmailDao();
    ed.addEmail(e);
    
    out.print("发送成功,即将跳转到首页!");
    response.setHeader("refresh", "3;url=main.jsp");
     %>
  </body>
</html>

<%@page import="Email.entityPackage.Email"%>
<%@page import="Email.daoPackage.EmailDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'Look.jsp' starting page</title>
</head>

<body>
    <%
        String id = request.getParameter("id");
        int eid = Integer.parseInt(id);

        EmailDao ed = new EmailDao();
        ed.GauBian(eid);
        Email email = ed.getOneEmail(eid);
    %>
    
    <h3>邮件详情</h3>
    <table border="1px" cellpadding="15px">
        <tr>
            <td>发件人:</td>
            <td><%=email.getEfa()%></td>
        </tr>
        <tr>
            <td>主题:</td>
            <td><%=email.getEtitle()%></td>
        </tr>
        <tr>
            <td>内容:</td>
            <td><%=email.getEcontent()%></td>
        </tr>
        <tr>
            <td>时间:</td>
            <td><%=email.getEtime()%></td>
        </tr>
    </table><br>
    <a href="main.jsp">返回</a>
</body>
</html>

<%@page import="Email.daoPackage.EmailDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Del.jsp</title>
  </head>
  
  <body>
    <%
    request.setCharacterEncoding("utf-8");
    
    int emailId=Integer.parseInt(request.getParameter("id"));
    EmailDao ed=new EmailDao();
    ed.delEmail(emailId);
    
    response.sendRedirect("main.jsp");
     %>
  </body>
</html>

退

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
    <title>My JSP 'bye.jsp' starting page</title>
  </head>
  
  <body>
    <%
    out.print("退出成功,3秒后返回登录页!");
    session.invalidate();
    response.setHeader("refresh", "3;url=Email1.jsp");
     %>
  </body>
</html>
posted @ 2022-05-28 22:33  满目星河☆  阅读(5)  评论(0编辑  收藏  举报