jsp第十一周作业

package util;

import java.sql.*;

//工具库工具类
public class DBUtil {

    // 实现驱动加载
    // 类的静态块随着jvm(Java虚拟机)加载类的字节码而开始执行
    // 静态只执行一次
    // 利用静态块的特点,完成JDBC驱动加载,而且在内存中只有一个驱动程序对象
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // 可以创建连接对象
    public static Connection getCon() {
        Connection con = null;
        String url = "jdbc:mysql://localhost/mysqldb?useSSL=true&useUnicode=true&characterEncoding=utf8";
        String user = "root";
        String password = "root";
        try {
            con = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return con;

    }

    // 可以关闭资源
    public static void close(ResultSet rs, PreparedStatement statement, Connection con) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (statement != null) {
                    try {
                        statement.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } finally {
                        if (con != null) {
                            try {
                                con.close();
                            } catch (SQLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }
                }

            }
        } else {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    if (con != null) {
                        try {
                            con.close();
                        } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }

        }
    }

}
package dao;

import util.DBUtil;

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 name,String pwd){
        boolean f=false;
        Connection conn= DBUtil.getCon();
        String sql="select * from users where uname=? and upwd=?";
        PreparedStatement ps;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, pwd);
            ResultSet rs=ps.executeQuery();
            if(rs.next())
                f=true;
            closeAll(conn, ps, rs);


        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return f;
    }
    public int reg(String uname, String upwd) {
        Connection conn = getConnection();
        int i=0;
        PreparedStatement ps = null;
        try {
            String sql = "insert into users(uname,upwd) values(?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, upwd);
            i=ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }
        return i;
    }
}

 

package dao;

import pojo.Msg;
import util.DBUtil;

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

public class MsgDao extends BaseDao{
    public List<Msg> getMailByReceiver(String name){
        List<Msg> list=new ArrayList<Msg>();
        Connection con= DBUtil.getCon();
        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 m=new Msg();
                m.setMsgid(rs.getInt("msgid"));
                m.setUsername(rs.getString("username"));
                m.setTitle(rs.getString("title"));
                m.setState(rs.getInt("state"));
                m.setMsgcontent(rs.getString("msgcontent"));
                m.setSendto(rs.getString("sendto"));
                m.setMsg_create_date(rs.getDate("mgs_create_date"));
                list.add(m);
            }

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

}

 

package pojo;

import java.util.Date;

public class Msg {
    private int msgid;
    private String username;
    private String title;
    private String msgcontent;
    private int state;
    private String sendto;
    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;
    }
}

 

package pojo;

public class User {
    private Integer id;
    private String uname;
    private String upwd;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    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;
    }
}

 

<%@page import="dao.UserDao"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String uname = request.getParameter("uname");
    String upwd =request.getParameter("upwd");
    UserDao usersDao=new UserDao();
    if(usersDao.login(uname, upwd)){
        session.setAttribute("uname", uname);
        request.getRequestDispatcher("main.jsp").forward(request, response);
    }
    else{
        out.print("登录失败,5s后跳转登录页面");
        response.setHeader("refresh", "5;url=login.jsp");
    }
%>
</body>
</html>

 

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="dao.MsgDao"%>
<%@page import="dao.UserDao"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String uname = request.getParameter("uname");
    String upwd = request.getParameter("upwd");
    UserDao usersDao = new UserDao();
    int i=usersDao.reg(uname, upwd);
    if(i>0){
        out.print("注册成功,即将跳到登录页.....");
        response.setHeader("refresh", "2;url=login.jsp");
    }else{
        out.print("注册失败,即将跳回注册页.....");
        response.setHeader("refresh", "2;url=reg.jsp");
    }
%>

</body>
</html>

 

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</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"><br>
    密码: <input  type="password" name="upwd">
    <br>
    <input type="button" value="登录" onClick="validate()">
    <a href="reg.jsp">注册</a>
</form>

</body>
</html>

 

<%@page import="pojo.Msg"%>
<%@page import="dao.MsgDao"%>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
<%
    String uname=(String)session.getAttribute("uname");
%>

<table height="20px" width="100%" align="center" border="1">
    <tr>
        <td>发件人</td>
        <td>主题</td>
        <td>状态</td>
        <td>时间</td>
    </tr>
    <%
        MsgDao md=new MsgDao();
        List<Msg> list=md.getMailByReceiver(uname);
        for(Msg m:list)
        {%>
    <tr>
        <td><%=m.getUsername() %></td>
        <td><%=m.getTitle() %></td>
        <td><%
            int state=m.getState();
            if(state==1){ %>
            <img src ="image/已读消息.png" />
            <%;
            }else{%>
            <img src ="image/未读.png" />
            <%;
            }
            %></td>
        <td><%=m.getMsg_create_date() %></td>
    </tr>
    <%}
    %>
</table>
</body>
</html>

 

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<script type="text/javascript">
    function validate(){
        if(resForm.uname.value==""){
            alert("账号不能为空!");
            return;
        }
        if(resForm.upwd.value==""){
            alert("密码不能为空!");
            return;
        }
        resForm.submit();
    }
</script>
<form name="resForm" action="doreg.jsp" method="post">
    <b>用户名</b>
    <input type="text" name="uname" />
    <br />
    <b>密码</b>
    <input type="text" name="upwd" />
    <br />
    <input type="button" value="注册" onClick="validate()">
    <a href="login.jsp">登录</a>
</form>
</body>
</html>

 

 

posted on 2022-05-14 22:36  chenyulin11  阅读(14)  评论(0编辑  收藏  举报

导航