JSP第九次上机作业

1.建库,建表2个
用户表(id,用户名,密码)

 

邮件表(id,发件人,收件人,标题,内容,发送时间,状态)

 

2.建model层
dao包

package 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/emails", "root", "001010Chen");
            } 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 dao;

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

public class UserDao extends BaseDao {
    //用户登录验证
    public boolean login(String uname, String upwd) {
        boolean f = false;
        Connection conn = getConnection();
        String sql = "select * from emailuser where uname=? and upwd=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, upwd);
            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 void reg(String uname, String upwd) {
        Connection conn = getConnection();
        PreparedStatement ps = null;
        try {
            String sql = "insert into emailuser(uname,upwd) values(?,?)"; // 2个占位符
            // 4.执行SQL语句
            ps = conn.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, upwd);
            ps.executeUpdate();// 增删改都用这个
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }

    }

}
package dao;

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

import entity.email;

public class EmlDao extends BaseDao{    
    //插入邮件
    //删除邮件
    //修改邮件状态

    //按照接收者查询全部邮件    
    public List<email> getEmailByAddressee(String uname){
        List<email> list=new ArrayList<email>();
        Connection conn=getConnection();
        String sql="select * from email where addresseename=?";
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, uname);
            rs=ps.executeQuery();
            while(rs.next()){
                email m=new email();
                m.setEid(rs.getInt(1));
                m.setSendername(rs.getString(2));                
                m.setTitle(rs.getString(3));
                m.setContent(rs.getString(4));                
                m.setAddresseename(rs.getString(5));
                m.setTotime(rs.getDate(6));
                m.setState(rs.getInt(7));
                list.add(m);
            }            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            closeAll(conn, ps, rs);
        }
        return list;
    }    

}

entity包

package entity;

public class emailuser {
    int uid;
    String uname;
    String upwd;
    public int getId() {
        return uid;
    }
    public void setId(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 entity;

import java.util.Date;

public class email {
    private int eid;
    private String sendername;
    private String addresseename;
    private String title;
    private String content;
    Date totime;
    private int state;
    
    public int getEid() {
        return eid;
    }
    public void setEid(int eid) {
        this.eid = eid;
    }
    public String getSendername() {
        return sendername;
    }
    public void setSendername(String sendername) {
        this.sendername = sendername;
    }
    public String getAddresseename() {
        return addresseename;
    }
    public void setAddresseename(String addresseename) {
        this.addresseename = addresseename;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public Date getTotime() {
        return totime;
    }
    public void setTotime(Date totime) {
        this.totime = totime;
    }
    public int getState() {
        return state;
    }
    public void setState(int state) {
        this.state = state;
    }
    
    
}

3.登陆,注册,登陆后显示全部邮件

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>登录页面</title>
<script type="text/javascript">
    function check() {
        if (loginForm.uname.value == "") {
            alert("请输入账号!");
            return;
        } else if (loginForm.upwd.value == "") {
            alert("请输入密码!");
            return;
        }
        loginForm.submit();
    }
</script>
</head>
<body>
    <form name="loginForm" action="dologin.jsp" method="post">
        用户:<input type="text" name="uname"><br> 
        密码:<input type="password" name="upwd"><br> 
        <input type="button" value="登录" onClick="check()"> 
        <a href="reg.jsp">立即注册账户</a>
    </form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>注册页面</title>
</head>
<body>
    <form name="regForm" action="doreg.jsp" method="post">
        用户:<input type="text" name="uname"><br> 
        密码:<input type="password" name="upwd"><br> 
        确认密码:<input type="password" name="upwd1"><br> 
        <input type="submit" value="注册" > 
        <a href="login.jsp">已有账号?前往登录</a>
    </form>
</body>
</html>
<%@page import="dao.UserDao"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

<html>
<head>
<title>判断</title>
</head>
<body>
    <%
        request.setCharacterEncoding("utf-8");
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");

        UserDao ud = new UserDao();
        if (ud.login(uname, upwd)) {
    %>
    <script type="text/javascript">
        alert("登录成功!");
    </script>
    <%
        session.setAttribute("uname", uname);
            request.getRequestDispatcher("main.jsp").forward(request,
                    response);
        } else {
    %>
    <script type="text/javascript">
        alert("登录失败,3秒后返回登录界面");
    </script>
    <%
        response.setHeader("refresh", "3;url=login.jsp");
        }
    %>
</body>
</html>
<%@page import="dao.UserDao"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>获取</title>
</head>
<body>
<%
    String uname=request.getParameter("uname");
    String upwd=request.getParameter("upwd");
    String upwd1=request.getParameter("upwd1");

    if (upwd.equals(upwd1)) {
        UserDao sd = new UserDao();
        sd.reg(uname, upwd);
%>
<script type="text/javascript">alert("注册成功!");</script>
<%
        request.getRequestDispatcher("login.jsp").forward(request,response);
    } else {
%>
<script type="text/javascript">alert("注册失败,请重试!");</script>
<%
        request.getRequestDispatcher("reg.jsp").forward(request,response);
    }
%>
    
</body>
</html>
<%@page import="entity.email"%>
<%@page import="dao.EmlDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>主界面</title>
</head>
<body>
    <%
        String uname = (String) session.getAttribute("uname");
    %>
    首页!!欢迎你<%=uname%><br>
    <table border="1">
        <tr>
            <th id="tom">发件人</th>
            <th id="tom">主题</th>
            <th id="tom">状态</th>
            <th id="tom">时间</th>

            <%
                EmlDao md = new EmlDao();
                List<email> list = md.getEmailByAddressee(uname);

                for (int i = 0; i < list.size(); i++) {
                    out.print("<tr>");
                    out.print("<td id=tom>" + list.get(i).getSendername() + "</td>");
                    out.print("<td id=tom>" + list.get(i).getTitle() + "</td>");
                    out.print("<td id=tom>" + list.get(i).getState() + "</td>");
                    out.print("<td id=tom>" + list.get(i).getTotime() + "</td>");
                    out.print("</tr>");
                }
            %>
        
    </table>
    <a id=tom href="writ.jsp">写邮件</a>
    <a id=tom href="delete.jsp">删除邮件</a>
</body>
</html>

结果截图:

 

 

 

posted @ 2022-05-13 12:02  小猪哏哏  阅读(15)  评论(0编辑  收藏  举报