码迷,mamicode.com
首页 > 其他好文 > 详细

家庭记账本的登录和注册功能的实现

时间:2020-01-25 22:03:23      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:next   users   ref   relative   date()   sql   roo   mamicode   count   

下面是工程目录

技术图片

 

 login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
    <style type="text/css">
        *{margin: 0;padding: 0}
        html,body{height: 100%}     /*这里很关键*/
 
        .outer-wrap{
            /*只有同时为html和body设置height: 100%时,这里的height才生效,
            并且随浏览器窗口变化始终保持和浏览器视窗等高*/
            height: 100%;    
            position: relative;
            background-image: url(‘images/01.jpg‘);
        }
        .login-panel{
            width: 400px;
            height: 200px;
            background-image: url(‘images/02.jpg‘);
            position: absolute;
            top: 50%;
            left: 50%;
            margin-top: -150px;
            margin-left: -200px;
        }
    </style>
<body>
<%
         request.setCharacterEncoding("utf-8");
         String message=(String)request.getAttribute("message");
         if(message!=null){
         if(message.equals("error")){
             %>
             <script type="text/javascript">
              alert("用户名或密码错误");
         </script>
         <%
      }else if(message.equals("noerror")){
          %>
          <script type="text/javascript">
              alert("登录成功");
         </script>
         <%
      }else{
      }
      }
      %>
    <div class="outer-wrap">
    
    <div style="font-size:160px;text-align:center">家庭记账本</div>
    <div class="login-panel">
    <div style="font-size:40px;text-align:center">登陆界面</div>
        <form action="LoginServlet" method="post" >
         用户名<input type="text" style="width:300px;height:30px;text-align:center" name="uname"><br/>&nbsp&nbsp&nbsp码<input style="width:300px;height:30px;text-align:center"type="password" name="upwd"><br/>
      <div style="text-align:center">   <input type="submit"  value="登录"><br/></div>
      <div style="text-align:center">  <a href="register.jsp" >没有账号,立即去注册</a>  </div>
        </form>
    </div>
    </div>
</body>
</html>

register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<%
         request.setCharacterEncoding("utf-8");
         String message=(String)request.getAttribute("message");
         if(message!=null){
         if(message.equals("error")){
             %>
             <script type="text/javascript">
              alert("该用户名已被使用,请重试");
         </script>
         <%
      }else if(message.equals("noerror")){
          %>
          <script type="text/javascript">
              alert("注册成功");
         </script>
         <%
      }else{
      }
      }
      %>
<style type="text/css">
        *{margin: 0;padding: 0}
        html,body{height: 100%}     /*这里很关键*/
 
        .outer-wrap{
            /*只有同时为html和body设置height: 100%时,这里的height才生效,
            并且随浏览器窗口变化始终保持和浏览器视窗等高*/
            height: 100%;    
            position: relative;
            background-image: url(‘images/01.jpg‘);
        }
        .register-panel{
            width: 400px;
            height: 250px;
            background-image: url(‘images/03.jpg‘);
            position: absolute;
            top: 50%;
            left: 50%;
            margin-top: -150px;
            margin-left: -200px;
        }
    </style>
<body>
 <div class="outer-wrap">
    
    <div style="font-size:160px;text-align:center">家庭记账本</div>
    <div class="register-panel">
    <div style="font-size:40px;text-align:center">注册界面</div>
    <form action="RegisterServlet" method="post">
    <div style="text-align:center">
    用户名<input type="text" style="width:300px;height:30px;text-align:center" name="uname"><br/>
    </div>
    <div style="text-align:center">&nbsp&nbsp&nbsp码<input type="text" style="width:300px;height:30px;text-align:center" name="upwd"><br/>
    </div>
    <div style="text-align:center">
    <input type="radio" name="usex" checked="checked"><input type="radio" name="usex"></div>
    <div style="text-align:center">   <input type="submit"  value="注册"><br/></div>
    <div style="text-align:center">  <a href="login.jsp" >已有账号,立即去登录</a>  </div>
    </form>
    </div>
    </div>
</body>
</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
         request.setCharacterEncoding("utf-8");
         String message=(String)request.getAttribute("message");
         if(message!=null){
         if(message.equals("error")){
             %>
             <script type="text/javascript">
              alert("用户名或密码错误");
         </script>
         <%
      }else if(message.equals("noerror")){
          %>
          <script type="text/javascript">
              alert("登录成功");
         </script>
         <%
      }else{
      }
      }
      %>
登录成功
</body>
</html>

DBUtil.java

 

package com.zzw.utils;

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


public class DBUtil {
    //数据库URL和账号密码
            private static final String URL="jdbc:mysql://localhost:3306/bookkeeping?serverTimezone=UTC&characterEncoding=utf-8";
            private static final String UNAME="root";
            private static final String UPWD="vayne";
            public static PreparedStatement  pstmt=null;
            public static ResultSet  rs = null;
            public static Connection  connection=null;
            
            
     //增删改
     public static boolean executeUpdate(String sql,Object [] params) {
         boolean flag = false;
         try {
             
         //a.导入驱动,加载具体的驱动类
         Class.forName("com.mysql.cj.jdbc.Driver");
         //b.与数据库建立连接
         connection = DriverManager.getConnection(URL,UNAME,UPWD);
         
         pstmt = connection.prepareStatement(sql);
         for(int i=0;i<params.length;i++) {
             pstmt.setObject(i+1, params[i]);
         }
         int count=pstmt.executeUpdate();//返回值表示,增删改几条数据
         //处理结果
         if(count>0)
         {
             System.out.println("操作成功!!!");
         }
         
         }catch(ClassNotFoundException e) {
             e.printStackTrace();
         }catch(SQLException e) {
             e.printStackTrace();
         }catch(Exception e){
             e.printStackTrace();
         }finally {
             try {
                 //先开的后关,后开的先关
             if(pstmt!=null)pstmt.close();
             if(connection !=null)connection.close();
             }catch(SQLException e) {
                 e.printStackTrace();
             }finally {
                 
             }
         }
        return flag;
     }
    //
     public static ResultSet executeQuery(String sql,Object [] params) {
         
                 try {
                     
                 //a.导入驱动,加载具体的驱动类
                 Class.forName("com.mysql.cj.jdbc.Driver");
                 //b.与数据库建立连接
                 connection = DriverManager.getConnection(URL,UNAME,UPWD);
                 
                 pstmt = connection.prepareStatement(sql);
                 if(params!=null) {
                 for(int i=0;i<params.length;i++) {
                     pstmt.setObject(i+1, params[i]);
                 }
                 }
                  rs = pstmt.executeQuery();
                  return rs;
                 }catch(ClassNotFoundException e) {
                     e.printStackTrace();
                     return null;
                 }catch(SQLException e) {
                     e.printStackTrace();
                     return null;
                 }catch(Exception e){
                     e.printStackTrace();
                     return null;
                 }
             
        }
}

LoginServlet.java

package com.zzw.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zzw.entity.User;
import com.zzw.service.IUserService;
import com.zzw.service.Impl.UserServiceImpl;


public class LoginServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        String name= request.getParameter("uname");
        String pwd= request.getParameter("upwd");
        User user = new User(name,pwd);
        //接口 x=new 实现类()
        IUserService userservice = new UserServiceImpl();
        boolean result=userservice.Login(user);
    
        if(!result) {
            request.setAttribute("message","error");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }else {
            request.setAttribute("message","noerror");
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

Register.java

package com.zzw.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zzw.entity.User;
import com.zzw.service.IUserService;
import com.zzw.service.Impl.UserServiceImpl;




public class RegisterServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        String name= request.getParameter("uname");
        String pwd= request.getParameter("upwd");
        String sex= request.getParameter("usex");
        User user = new User(name,pwd,sex);
        //接口 x=new 实现类()
        IUserService userservice = new UserServiceImpl();
        boolean result=userservice.Register(user);
    
        if(!result) {
            request.setAttribute("message","error");
            request.getRequestDispatcher("register.jsp").forward(request, response);
        }else {
            request.setAttribute("message","noerror");
            request.getRequestDispatcher("register.jsp").forward(request, response);
        }
    }

    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

UserServiceImpl.java

package com.zzw.service.Impl;




import com.zzw.dao.IUserDao;
import com.zzw.dao.Impl.UserDaoImpl;
import com.zzw.entity.User;
import com.zzw.service.IUserService;

public class UserServiceImpl implements IUserService{
    IUserDao userdao= new UserDaoImpl();
    //登录
     public boolean Login(User user) {
            boolean flag=false;
          if(userdao.Login(user.getUname(),user.getUpwd())) {
                flag=true;
          }
          return flag;
     }
    //注册
    public boolean Register(User user) {
        boolean flag=false;
        if(!userdao.isExist(user.getUname())) {
            userdao.Register(user);
            flag=true;
        }else {
            System.out.println("此人已存在");
        }
       return flag; 
    }
  //根据账号查询用户
    public User Query(String uname) {
        return userdao.Query(uname);
    }
}

IUserService.java

package com.zzw.service;

import com.zzw.entity.User;

public interface IUserService {
    //登录
         public boolean Login(User user);
    //注册
         public boolean Register(User user) ;
    //根据账号查询用户
         public User Query(String uname) ;
}

User.java

package com.zzw.entity;

public class User {
      private int uid;
      private String uname;
      private String upwd;
      private String usex;
      
    @Override
    public String toString() {
        return "User [uid=" + uid + ", uname=" + uname + ", upwd=" + upwd + ", usex=" + usex + "]";
    }
    
    public User() {
        
    }
    public User( String uname, String upwd) {
        this.uname = uname;
        this.upwd = upwd;
    }
    public User( String uname, String upwd, String usex) {
        this.uname = uname;
        this.upwd = upwd;
        this.usex = usex;
    }
    public User(int uid, String uname, String upwd, String usex) {
        this.uid = uid;
        this.uname = uname;
        this.upwd = upwd;
        this.usex = usex;
    }

    public int getUid() {
        return uid;
    }
    public void setUid(int uid) {
        this.uid = uid;
    }
    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;
    }
    public String getUsex() {
        return usex;
    }
    public void setUsex(String usex) {
        this.usex = usex;
    }
      
}

UserDaoImpl.java

package com.zzw.dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.zzw.dao.IUserDao;
import com.zzw.entity.User;
import com.zzw.utils.DBUtil;

public class UserDaoImpl implements IUserDao{
    //注册
            public  boolean Register(User user) {
                 String sql="insert into user(uname,upwd,usex) values(?,?,?)" ;
                 Object [] params= {user.getUname(),user.getUpwd(),user.getUsex()};
                 return  DBUtil.executeUpdate(sql, params);
            }
            //查询账户是否存在
            public  boolean isExist(String uname) {
                return Query(uname)==null? false:true;
            }
    //登录
           public boolean Login(String uname,String upwd) {
               return Query(uname,upwd)==null? false:true;
           }
    //根据账号查询用户全部信息
    public   User Query(String uname) {
         User user= null;
         ResultSet rs = null; 
         try {
             String sql="select * from user where uname =?" ;
             Object [] params= {uname};
             rs=DBUtil.executeQuery(sql, params);
             if(rs.next()) {
                 String name=rs.getString("uname");
                 String pwd=rs.getString("upwd");
                 String sex=rs.getString("usex");
                 user= new User(name,pwd,sex);
             }
         }catch(SQLException e) {
             e.printStackTrace();
         }catch(Exception e) {
             e.printStackTrace();
         }finally {
             try {
                    //先开的后关,后开的先关
                if(rs!=null)rs.close();
                if(DBUtil.pstmt!=null)DBUtil.pstmt.close();
                if(DBUtil.connection !=null)DBUtil.connection.close();
                }catch(SQLException e) {
                    e.printStackTrace();
                }finally {
                    
                }
         }
         return user;
    }
    //根据账户密码确定是否存在
    public   User Query(String uname,String upwd) {
         User user= null;
         ResultSet rs = null; 
         try {
             String sql="select * from user where uname =? and upwd=?" ;
             Object [] params= {uname,upwd};
             rs=DBUtil.executeQuery(sql, params);
             if(rs.next()) {
                 String name=rs.getString("uname");
                 String pwd=rs.getString("upwd");
                 String sex=rs.getString("usex");
                 user= new User(name,pwd,sex);
             }
         }catch(SQLException e) {
             e.printStackTrace();
         }catch(Exception e) {
             e.printStackTrace();
         }finally {
             try {
                   //先开的后关,后开的先关
               if(rs!=null)rs.close();
               if(DBUtil.pstmt!=null)DBUtil.pstmt.close();
               if(DBUtil.connection !=null)DBUtil.connection.close();
               }catch(SQLException e) {
                   e.printStackTrace();
               }finally {
                   
               }
         }
         return user;
    }
}

IUserDao.java

package com.zzw.dao;

import com.zzw.entity.User;

public interface IUserDao {
    //注册
    public  boolean Register(User user) ;
    //查询账户是否存在
    public  boolean isExist(String uname) ;
    //登录
    public  boolean Login(String uname,String upwd) ;
    //根据帐号查询用户全部信息
    public   User Query(String uname) ;
}

 

 

 

 

 

 

技术图片

 

 

 技术图片

 

 

 此时无用户,登录失败

技术图片

 

 

 去注册

技术图片

 

 

 技术图片

 

 

 

 

 

 

 技术图片

 

 

 技术图片

 

 

 技术图片

 

 

 技术图片

 

 

 输入错误的密码

技术图片

 

 

 技术图片

 

家庭记账本的登录和注册功能的实现

标签:next   users   ref   relative   date()   sql   roo   mamicode   count   

原文地址:https://www.cnblogs.com/yeyueweiliang/p/12233476.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!