JSP 第八周课后作业

邮件系统 分层实现 1登录 2注册  3显示邮件列表 3个功能

数据库数据

user

msg

java 实体类

user

package com.lty.bean;

public class User {

    private String username;
    private String password;
    private String email;

    public User() {
    }

    public User(String username, String password, String email) {
        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;
    }
}

Msg

package com.lty.bean;

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

数据访问层

获取数据库连接

package com.lty.dao;

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

public class BaseDao{

        // 获取连接
        protected static Connection getConnection() throws Exception {
            Class.forName("com.mysql.jdbc.Driver");
            // 建立连接
            Connection conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/msg?useUnicode=true&characterEncoding=utf8", "root", "root");

            return conn;
        }

        // 关闭连接
        public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs) throws Exception {
            if (rs != null)
                rs.close();
            if (ps != null)
                ps.close();
            if (conn != null)
                conn.close();
        }

}

用户注册及登录  userDao

package com.lty.dao;

import com.lty.bean.User;

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

public class userDao extends BaseDao{

    public int UserRegister(User user) throws Exception {
            int i = 0;
            Connection conn = getConnection();
            String sql = "insert into user values(?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getEmail());
            return i = ps.executeUpdate();
        }


        public int UserLogin(String username ,String password) throws Exception {
            int flag = 0;
            Connection conn = getConnection();
            String sql = "select * from user where username = ? and password = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            ResultSet rs = ps.executeQuery();

            while (rs.next() == true){
                flag = 1;
            }

            return flag;
        }
    }

显示邮件列表 MsgDao

package com.lty.dao;

import com.lty.bean.Msg;

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

public class msgDao extends BaseDao {


    public List<Msg> ShowAllMsg(String username) {

        List<Msg> list = new ArrayList<Msg>();
        try {
            Connection conn = getConnection();
            PreparedStatement ps = conn.prepareStatement("select * from msg where username = ? ");
            ps.setString(1, username);

            ResultSet 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")));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    }

首页面 index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>$Title$</title>
</head>

<script type="text/javascript">
  function changeActionToRegister(){
    var s = document.getElementById("idForm")
    s.setAttribute("action", "doRegister.jsp");
  }
  function changeActionToLogin(){
    var s = document.getElementById("idForm")
    s.setAttribute("action", "doLogin.jsp");
  }
</script>

<body>

<form id="idForm" method="post">
  用户名:<input type="text" name="username"/>
  <br>
  密码:<input type="password"  name="password"/>
  <br>
  <input type="submit" onclick="changeActionToLogin()" value="登录">
  <input type="submit" onclick="changeActionToRegister()" value="注册">
</form>

</body>
</html>

登录页面 doLogin.jsp

<%@ page import="com.lty.dao.userDao" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    userDao userDao = new userDao();
    int i = userDao.UserLogin(username,password);
    if (i != 0){
        request.getSession().setAttribute("username",username);
        request.getSession().setAttribute("password",password);
        request.getRequestDispatcher("ok.jsp").forward(request,response);
    }else{
        response.sendRedirect("error.jsp");
    }

%>

</body>
</html>

登录成功页面 ok.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>

<script>
    function changeActionToShowMsg(){
        var s = document.getElementById("idForm")
        s.setAttribute("action", "doShowMsg.jsp");
    }
</script>
<body>
<%
    String username = (String) request.getSession().getAttribute("username");
    String password = (String) request.getSession().getAttribute("password");
%>

<p>
    欢迎您 !!! 用户名:<%=username%> 密码:<%=password%>
</p>

<form id="idForm" method="post">
    <input type="submit" onclick="changeActionToShowMsg()" value="显示邮件列表">
</form>

</body>
</html>

登录失败页面 error.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<p>对不起,您的输入账号信息错误!!!请重新登陆。。。</p>
</body>
</html>

显示邮件列表 doShowMsg.jsp

<%@ page import="com.lty.dao.msgDao" %>
<%@ page import="java.util.List" %>
<%@ page import="com.lty.bean.Msg" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>

<body>

    <table border="1" cellspacing="0">
<%
    List<Msg> list = new ArrayList<Msg>();
    String username = (String)request.getSession().getAttribute("username");
    msgDao msgDao = new msgDao();
    list = msgDao.ShowAllMsg(username);

    for (int i = 0; i < list.size(); i++) {

        out.print("<tr>");

        out.print("<td>" + list.get(i).getMsgid() + "</td>");
        out.print("<td>" + list.get(i).getUsername() + "</td>");
        out.print("<td>" + list.get(i).getTitle() + "</td>");
        out.print("<td>" + list.get(i).getMsgcontent() + "</td>");
        out.print("<td>" + list.get(i).getState() + "</td>");
        out.print("<td>" + list.get(i).getSendto() + "</td>");
        out.print("<td>" + list.get(i).getMsg_create_date() + "</td>");

        out.print("</tr>");
    }

%>
    </table>
</body>
</html>

注册页面 doRegister.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<script type="text/javascript">
    function changeActionToRegister(){
        var s = document.getElementById("idForm")
        s.setAttribute("action", "register.jsp");
    }
</script>
<body>
<form id="idForm" method="post">
    用户名:<input type="text" name="username"/>
    <br>
    密码:<input type="password"  name="password"/>
    <br>
    邮箱:<input type="email" name="email"/>
    <br>
    <input type="submit" onclick="changeActionToRegister()" value="注册">
</form>
</body>
</html>
<%@ page import="com.lty.dao.userDao" %>
<%@ page import="com.lty.bean.User" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    userDao userDao = new userDao();
    User user = new User();
    user.setUsername(request.getParameter("username"));
    user.setPassword(request.getParameter("password"));
    user.setEmail(request.getParameter("email"));
    int i = userDao.UserRegister(user);
    if(i != 0){
        request.getRequestDispatcher("succeed.jsp").forward(request,response);
    }
%>
</body>
</html>

注册成功提示页面 succeed.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
注册信息成功!!!请返回进行登录...
</body>
</html>

原文地址:https://www.cnblogs.com/lilbetter03/p/14716269.html