第11周jsp

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

   <table border="1px" cellpadding="20px" cellspacing="0px">
       <tr>
       <th>发件人</th>
       <th>收件人</th>
       <th>标题</th>
       <th>内容</th>
       <th>创建日期</th>
       <th>邮件状态</th>
       </tr>
       <%
           for(Email email : (List<Email>)request.getAttribute("emailList")) {%>
       <tr>
           <td><%=email.getSender()%></td>
           <td><%=email.getAddressee()%></td>
           <td><%=email.getTitle()%></td>
           <td><%=email.getContent()%></td>
           <td><%=email.getTime()%></td>
           <td><%=email.getStatus()%></td>
       </tr>
       <%
           }
       %>
   </table>

</body>
</html>

  

1.建库,建表2个
用户表(id,用户名,密码)
邮件表(id,发件人,收件人,标题,内容,发送时间,状态)

2.建model层
entity,dao包

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

ackage shool.work.pojo;


import java.util.Date;

public class Email {

  private Integer id;
  private String sender;
  private String addressee;
  private String title;
  private String content;
  private Date time;
  private String status;

  public Email() {
  }

  public Email(Integer id, String sender, String addressee, String title, String content, Date time, String status) {
    this.id = id;
    this.sender = sender;
    this.addressee = addressee;
    this.title = title;
    this.content = content;
    this.time = time;
    this.status = status;
  }

  public Email(String sender, String addressee, String title, String content, Date time, String status) {
    this.sender = sender;
    this.addressee = addressee;
    this.title = title;
    this.content = content;
    this.time = time;
    this.status = status;
  }

  public Integer getId() {
    return id;
  }

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

  public String getSender() {
    return sender;
  }

  public void setSender(String sender) {
    this.sender = sender;
  }

  public String getAddressee() {
    return addressee;
  }

  public void setAddressee(String addressee) {
    this.addressee = addressee;
  }

  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 getTime() {
    return time;
  }

  public void setTime(Date time) {
    this.time = time;
  }

  public String getStatus() {
    return status;
  }

  public void setStatus(String status) {
    this.status = status;
  }

  @Override
  public String toString() {
    return "Email{" +
            "id=" + id +
            ", sender='" + sender + '\'' +
            ", addressee='" + addressee + '\'' +
            ", title='" + title + '\'' +
            ", content='" + content + '\'' +
            ", time=" + time +
            ", status='" + status + '\'' +
            '}';
  }
}

  

package shool.work;

import shool.util.DBUtil;
import shool.work.pojo.Email;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/EmailShow")
public class EmailShow extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Email> emailList = new ArrayList<>();
        try {
            conn = DBUtil.getConnection();
            String sql = "select*from email";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                String sender = rs.getString("Sender");
                String addressee = rs.getString("Addressee");
                String title = rs.getString("Title");
                String content = rs.getString("Content");
                Date time = rs.getDate("Time");
                String status = rs.getString("Status");
                Email email = new Email(sender,addressee,title,content,time,status);
                emailList.add(email);
            }
            req.setAttribute("emailList",emailList);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,rs);
        }
        req.getRequestDispatcher("/email.jsp").forward(req, resp);
    }
}

  

package shool.work;

import shool.util.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.WebEndpoint;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@WebServlet("/login")
public class Login extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        boolean login = false;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "select*from user where username=? and password=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            rs = ps.executeQuery();
            if (rs.next()) {
                login = true;
            }else {
                login=false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, ps, rs);
        }
        if (login) {
            req.getRequestDispatcher("/EmailShow").forward(req,resp);
        }else {
            req.setAttribute("flag", "您的用户名或者密码错误");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }

    }
}

  

package shool.work;

import shool.util.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@WebServlet("/Registration")
public class Registration extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count = 0;
        try {
            conn = DBUtil.getConnection();
            String sql = "insert into user (username,password) values(?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, ps, rs);
        }
        if (count == 0) {
            req.getRequestDispatcher("/error.jsp").forward(req, resp);
        } else {
            req.getRequestDispatcher("/UI.jsp").forward(req, resp);
        }

    }
}

  

html>
<head>
    <title>注册</title>
</head>
<body>
  <form>
      用户名
    <input type="text" maxlength="15" name="username"><br><br>
      密码
      <input type="text"maxlength="15" name="password"><br><br>
      <input type="submit" value="注册">
  </form>
<a href="UI.jsp">进入登录 </a>
</body>
</html>

  

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
   <form action="${pageContext.request.contextPath}/login" method="post">
       用户名
    <input type="text" name="username" maxlength="15"/><br/><br/>
       密码
       <input type="text" name="password" maxlength="15"/><br/><br/>
       <input type="submit" value="登录"/>
   </form>
<a href="email.jsp"></a>
</body>
</html>

  

 

 

 

posted @ 2022-05-15 21:43  monster丶易  阅读(1)  评论(0编辑  收藏  举报