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

下面是工程目录

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

此时无用户,登录失败

去注册

输入错误的密码

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

时间: 2024-10-06 00:45:17

家庭记账本的登录和注册功能的实现的相关文章

基于struts2和hibernate的登录和注册功能——完整实例

1.该项目使用MySQL数据库,数据库名为test,表名info,如图所示: 2.配置web.xml(Struts2使用) <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/X

用java数组模拟登录和注册功能

package com.linkage.login; import java.util.Scanner; public class user { // 存储用户名和密码 public static String arr1[] = new String[3]; public static String arr2[] = new String[3]; static int index = 0; @SuppressWarnings("resource") public static void

BBS登录与注册功能

登录功能 视图函数 def my_login(request): if request.method == 'GET': return render(request, 'login.html') else: name = request.POST.get('name') password = request.POST.get('password') code_text = request.POST.get('text') text = request.session.get('code_text

Unity uGUI 登录及注册功能

上次我们已经完成了登录界面的模拟功能,今天咱们把上次没做完的继续完善下!那么废话少说直接开始吧! PS:本次完善的功能有: 1,增加对数据库的操作. 2,使用了MD5Key值加密 3,完善登录和组测功能 4,环境Win8.1+Unity4.6+mysql 1,咱们就接着上次的工程继续吧,所以就不再新建工程了. 2,引入对数据库的操作,因此我们封装了一个SqlAccess类. SqlAccess.cs /****************************** * 对数据库操作的封装 * @A

Django框架操作之实现登录、注册功能(数据库的增、删、改、查)

一.Django的工作流 1:用户通过浏览器输入相应的 URL 发起 HTTP 请求(一般是 GET/POST) 2:Django 接受到请求,检测 urls.py 文件,找到和用户输入的 URL 相匹配的项,并调用该 URL 对应的视图函数(view),例如,通常来说 urls.py 文件里的代码是这样的: url(r'^homepage/$', views.home_page) 则当用户输入的 URL 为 www.某个网址.com/homepage 时,django 检测到该 URL 与上面

利用PHP实现登录与注册功能以及使用PHP读取mysql数据库——以表格形式显示数据

登录界面 <body><form action="login1.php" method="post"><div>用户名:<input type="text" name="uid" /></div><br /><div>密码:<input type="password" name="pwd" />

android和php开发的用户登录以及注册功能

完成android的记住密码.自动登录以及发送短信.验证码倒计时功能.用户名和手机唯一性验证.以android为客户端.php为服务端

使用函数完成用户登录和注册功能

#先让用户选择,是登陆还是注册 #选择序号完毕之后,运行相应的程序,#验证成功之后,可以让其继续选择,登陆还是注册,还可以选择退出. import time,os #文件名 file_name = 'user_list.txt' def file_exists(*args,**kwargs): ''' # 判断用户列表文件是否存在 :return: True 存在 False 不存在 ''' # 判断文件是否存在 if os.path.exists(file_name): return True

使用PHP实现用户登录和注册的功能

登陆界面 login.PHP [html] view plain copy <form action="logincheck.php" method="post"> 用户名:<input type="text" name="username" /> <br /> 密码:<input type="password" name="password"