JSP十一周作业

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

2.建model层
entity,dao包

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

 

package com.gd.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;




public class BaseDao {
    //获取连接
    protected Connection getConnection(){
        Connection conn=null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","lin0520...");
            
        }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.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;




public class BaseDao {
    //获取连接
    protected Connection getConnection(){
        Connection conn=null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","lin0520...");
            
        }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;
public class usersDao extends BaseDao{
//关于用户的增删改查
    public boolean denglu(String uname,String upwd){
        boolean f=false;
        Connection conn=getConnection();
        String sql=" select * from users where unmae=? 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 (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally{
            closeAll(conn,ps,rs);
        }
        return f;                
    }
    
    public int login(String uname,String upwd){
        int i=-1;
        Connection conn=getConnection();
        PreparedStatement ps=null;
        String sql ="insert into uers values (uname=? and upwd=?)";
        try {                
            ps =conn.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, upwd);
            i=ps.executeUpdate();
        } catch (SQLException e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
            closeAll(conn,ps,null);
        }
        return i;
    }

}
package com.gd.entity;

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 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;
    }
    
    public msg() {
        super();
        this.msgid= msgid;
        this.username = username;
        this.sendto = sendto;
        this.title = title;
        this.msg_create_date = msg_create_date;
        this.state = state;
        this.msgcontent = msgcontent;
    }
}
package com.gd.entity;

public class users {
    int id;
    String uname;
    String upwd;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return uname;
    }
    public void setName(String uname) {
        this.uname = uname;
    }
    public String getUpwd() {
        return upwd;
    }
    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }
    
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
  <head>
  <base href="<%=basePath%>">
      </head>
  
  <body>
  <script>
  function validate(){
  if(Formname.uname.value==""){
  alert("用户名不能为空");
  return;
  }
  if(Formname.upwd.value==""){
  alert("密码不能为空");
  return;
  }
  Formname.submit();
  }
  </script>
   <form name="Formname" action="dodenglu.jsp" method="post">
   用户名:<input type="text" name="uname" ><br>
   密码:<input type="password" name="upwd"><br>
   <input type="submit" value="登录">
   <a href="login.jsp">注册</a>
   </form>
   </body>
</html>
<%@page import="com.gd.dao.msgDao" %>
<%@page import="com.gd.dao.usersDao" %>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
  <head>
  <base href="<%=basePath%>">
  </head>
  <body>
    <%
  request.setCharacterEncoding("utf-8");
  String uname=request.getParameter("uname");
  String upwd =request.getParameter("upwd");
  usersDao ud=new usersDao();
 //   msgDao md=new msgDao();
  if(ud.denglu(uname,upwd)){
 // session.attribute("uname",uname);
    request.getRequestDispatcher("ok.jsp").forward(request, response);
    }else
        response.setHeader("refresh","5;url=denglu.jsp");
 %>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
  <head>
  <base href="<%=basePath%>">
  </head>

  <body>
  <script type="text/javascript">
    function validate(){
  if(LoginForm.uname.value==""){
  alert("用户名不能为空");
  return;
  }
  if(LoginForm.upwd.value==""){
  alert("密码不能为空");
  return;
  }
  LoginForm.submit();
  }
  </script>
    <form action="jump.jsp" name="LoginForm" method="post"></form>
    用户名:<input type="text" name="uname"><br>
    密码:<input type="password" name="upwd"><br>
    <input type="submit" value="注册" ">
    <a href="denglu.jsp">登录</a>
     </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.gd.dao.usersDao" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   </head>
  <body>
  <% 
  request.setCharacterEncoding("utf-8");
  String uname=request.getParameter("uname");
  String upwd=request.getParameter("upwd ");
     usersDao ud=new usersDao();
  int i=ud.login(uname,upwd);
  if(i>0){
  out.print("注册成功!");
  request.getRequesDispatcher("ok.jsp").forward(request,response);
  }
  else{
  out.print("注册失败!");
   request.getRequesDispatcher("login.jsp").forward(request,response);
   }
  %>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.gd.dao.msgDao"%>
<%@page import="com.gd.dao.usersDao"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath%>">
<title></title>
</head>
<body>
    <%
        String uname = (String) session.getAttribute("uname");
        msgDao dao = new msgDao();
        List<msg> list = dao.getAll(uname);
    %>
    欢迎<%=uname%>
    <br>
    <br>
    <table border="1px" cellspacing="0" cellpadding="30px">
        <thead></thead>
        <tr>
            <th>发件人</th>
            <th>标题</th>
            <th>时间</th>
            <th>状态</th>
        </tr>
        <tbody></tbody>
        <%
            for (int i = 0; i < list.size(); i++) {
        %>
        <tr>
            <td><%out.print(list.get(i).getFjr().toString()); %></td>
            <td><%out.print(list.get(i).getTitle().toString()); %></td>
            <td><%out.print(list.get(i).getTime().toString()); %></td>
            <td><%if(list.get(i).getZt()==0){
            out.print("<img src='img/w.png'></img>");
            }else{
            out.print("<img src='img/y.png'></img>");
            }%></td>
        </tr>
        <%
            }
        %>

    </table>



</body>
</html>

 

 

 

 

 

posted @ 2022-05-15 14:05  好多鱼好多鱼好剩余  阅读(21)  评论(0编辑  收藏  举报