第十一周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;

public class BaseDao {
    // 获取连接
    protected Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            // 2.建立连接
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/zuoye", "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.List;

import com.gd.entity.Mail;

public class MailDao extends BaseDao {
        public List<Mail> listAll(String username) throws SQLException{
            List<Mail> list=new ArrayList<Mail>();
            Connection conn = getConnection();
            String sql="select * from msg where username=?";
            PreparedStatement pred =null;
            ResultSet rest=null;
            try {
                pred = conn.prepareStatement(sql);
                pred.setString(1, username);
                rest=pred.executeQuery();
                while (rest.next()) {
                    Mail mail = new Mail();
                    mail.setMailid(rest.getInt("mailid"));
                    mail.setUsername(rest.getString("username"));
                    mail.setTitle(rest.getNString("title"));
                    mail.setMailcontent(rest.getString("mailcontent"));
                    mail.setState(rest.getInt("state"));
                    mail.setSendto(rest.getString("sendto"));
                    mail.setMail_create_date(rest.getDate("mail_create_date"));
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
            finally{
                closeAll(conn, pred, rest);
            }
            return list;
        }
}
package com.gd.dao;

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

public class UsersDao extends BaseDao{
     public boolean login(String uname ,String upwd)throws SQLException{
        boolean f = false;
        Connection conn = getConnection();
        PreparedStatement pred = null;
        ResultSet rest = null;
         String sql = "select * from users where uname=? and upwd=?";
         try {
             while (rest.next()) {
                    f=true;
                }
        } catch (Exception e) {
            // TODO: handle exception
        }finally {
            closeAll(conn, pred, rest);
        }
         return f;
     }
     public boolean id(Integer id ) throws SQLException{
         boolean f = false;
         Connection conn = getConnection();
         PreparedStatement pred = null;
         ResultSet rest = null;
         String sql = "select * from users where id=?";
         try {
             pred = conn.prepareStatement(sql);
             pred.setInt(1, id);
             rest = pred.executeQuery();
            while(rest.next()) {
                f=true;
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        finally {
            closeAll(conn, pred, rest);
        } 
         return f; 
     }
     public void zhuce(Integer id, String uname ,String upwd) {
         Connection conn = getConnection();
         PreparedStatement pred = null;
         try {
             String sql = "insert into users(id,uname,upwd) values(?,?,?)";
             pred = conn.prepareStatement(sql);
             pred.setInt(1, id);
             pred.setString(2, uname);
             pred.setString(3, upwd);
        } catch (Exception e) {
            // TODO: handle exception
        }
        finally {
            closeAll(conn, pred, null);
        }
     }
}
package com.gd.entity;

import java.util.Date;

public class Mail {
         private Integer mailid;
        private String username;
        private String title;
        private String mailcontent;
        private Integer state;
        private String sendto;
        private Date mail_create_date;
        public Integer getMailid() {
            return mailid;
        }
        public void setMailid(Integer mailid) {
            this.mailid = mailid;
        }
        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 getMailcontent() {
            return mailcontent;
        }
        public void setMailcontent(String mailcontent) {
            this.mailcontent = mailcontent;
        }
        public Integer getState() {
            return state;
        }
        public void setState(Integer state) {
            this.state = state;
        }
        public String getSendto() {
            return sendto;
        }
        public void setSendto(String sendto) {
            this.sendto = sendto;
        }
        public Date getMail_create_date() {
            return mail_create_date;
        }
        public void setMail_create_date(Date mail_create_date) {
            this.mail_create_date = mail_create_date;
        }
        @Override
        public String toString() {
            return "Mail [mailid=" + mailid + ", username=" + username + ", title=" + title + ", mailcontent="
                    + mailcontent + ", state=" + state + ", sendto=" + sendto + ", mail_create_date=" + mail_create_date
                    + "]";
        }
        public Mail(Integer mailid, String username, String title, String mailcontent, Integer state, String sendto,
                Date mail_create_date) {
            super();
            this.mailid = mailid;
            this.username = username;
            this.title = title;
            this.mailcontent = mailcontent;
            this.state = state;
            this.sendto = sendto;
            this.mail_create_date = mail_create_date;
        }
        public Mail() {
            super();
            // TODO Auto-generated constructor stub
        }
        
}
package com.gd.entity;

public class Users {
    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 language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@page import="com.gd.dao.UsersDao" %>
    <%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 <%
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");
        UsersDao ud = new UsersDao();
        if (ud.login(uname, upwd)) {
            session.setAttribute("uname", uname);
            request.getRequestDispatcher("index.jsp").forward(request,
                    response);
        } else {
            out.print("登录失败,5s后跳转登录页面");
            response.setHeader("refresh", "5;url=login.jsp");
        }
    %>
</body>
</html>
<%@page import="org.apache.catalina.connector.OutputBuffer"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
%>
<%@page import="com.gd.dao.UsersDao"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
    String uid = request.getParameter("id");
    Integer id = Integer.parseInt(uid);
    String uname = request.getParameter("uname");
    String upwd = request.getParameter("upwd");
    String upwd2 = request.getParameter("upwd2");
    UsersDao usersDao = new UsersDao();
    if (upwd.equals(upwd2)) {
        if (usersDao.id(id)) {
            out.print("已经有这个id了,即将跳回注册界面");
            response.setHeader("rafresh", "5;url=zhuce.jsp");
        } else {
            usersDao.zhuce(id, uname, upwd);
            out.print("注册成功了,即将跳回登录界面");
            response.setHeader("refresh", "5;url=login.jsp");
        }
    } else {
        out.print("两次密码不一致,即将跳回注册界面");
        response.setHeader("refresh", "5;url=zhuce.jsp");
    }
    %>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
%>
 <%@page import="java.util.List" import="java.util.*"%>
<%@page import="sun.security.jgss.LoginConfigImpl"%>
<%@page import="com.gd.dao.UsersDao"%>
<%@page import="com.gd.dao.MailDao" %>
<%@page import="com.gd.entity.Mail"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        String uname = (String) session.getAttribute("uname");
    if (uname == null) {
        out.print("您未登录,即将跳回登陆页.....");
        response.setHeader("refresh", "5;url=login.jsp");
    } else {
    %>
    欢迎你<%=uname%>



    <%
        Mail mail = new Mail();
    MailDao mailDao = new MailDao();
    List<Mail> list = new ArrayList<Mail>();
    list = mailDao.listAll(uname);
    %>
    邮件列表:
    <table width="600px" border="2px solid blue">
        <tr>
            <th>id</th>
            <th>发件人</th>
            <th>标题</th>
            <th>内容</th>
            <th>已读未读状态</th>
            <th>收件人</th>
            <th>发送时间</th>
        </tr>
        <%
            if (list.size() == 0) {
            out.print("你没有邮件");
        } else {
            for (Mail mail1 : list) {
        %>
        <tr>
            <td>
                <%
                    out.print(mail1.getMailid());
                %>
            </td>
            <td>
                <%
                    out.print(mail1.getUsername());
                %>
            </td>
            <td>
                <%
                    out.print(mail1.getTitle());
                %>
            </td>
            <td>
                <%
                    out.print(mail1.getMailcontent());
                %>
            </td>
            <td>
                <%
                    int state = mail1.getState();
                if (state == 1) {
                %> <img src="image/yidu.png" /> <%
     ;
 } else {
 %> <img src="image/weidu.png" /> <%
     ;
 }
 %>
            </td>
            <td>
                <%
                    out.print(mail1.getSendto());
                %>
            </td>
            <td>
                <%
                    out.print(mail1.getMail_create_date());
                %>
            </td>
        </tr>
        <%
            }
        }
        }
        %>
    </table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
function dologin() {
    if (loginform.uname.value == "") {
        alert("没有输入用户名");
        return;
    }
    if (loginform.upwd.value == "") {
        alert("没有输入密码");
        return;
    }
    loginform.submit();
}
</script>
<form action="dologin.jsp" name="loginfrom" method="post">
用户名:<input type = "text" name="uname"/><br>

密码:<input type = "password" name="upwd"/><br>

<input type ="button" value="登录" onclick="dologin()">
<a href="zhuce.jsp">立即注册<a/>


</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Insert title here</title>
    </head>
    <body>
        <script type="text/javascript">
            function dozhuce(){
                if(loginfrom2.id.value == ""){
                    alert("没有输入id");
                    return;
                }
                if(loginfrom2.uname.value == ""){
                    alert("没有输入用户名");
                    return;
                }
                if (loginform2.upwd.value == "") {
                    alert("没有输入密码");
                    return;
                }
                if (loginform2.upwd2.value == "") {
                        alert("没有确认密码");
                        return;
                }
                loginform2.submit();
            }
        </script>
        <form action="dozhuce.jsp" name="loginform2" method="post">
        id: <input type="number" name="id" /><br>
         用户名:<input type="text" name="uname" /> <br>
         密码:<input type="password" name="upwd" /> <br>
         确认密码:<input type="password" name="upwd2" /><br>
        <input type="button" value="注册" onclick="dozhuce()" />
    </form>
    </body>
</html>

 

 

 

 

 

 

 

posted on 2022-05-15 20:31  不顾倾世、只倾她一  阅读(6)  评论(0编辑  收藏  举报

导航