java 用户注册登陆Demo

一个用户注册登陆的系统,用到了MD5加密处理密码,实现了一个简单的数据库连接池connectionPool,

实现了注册,登陆,登陆之后修改用户信息等功能,非常适合初学者

一.准备工作

数据库:MySQL  5.5.62-log MySQL Community Server (GPL)

创建user表,数据库为first_db;

CREATE TABLE IF NOT EXISTS `user`(
`user_id` INT UNSIGNED AUTO_INCREMENT,
 `username` VARCHAR(100) NOT NULL,
 `password` VARCHAR(100) NOT NULL,
 `phone` VARCHAR(100) NOT NULL, PRIMARY KEY ( `user_id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

java 用户注册登陆Demo

  二.项目结构图:

java 用户注册登陆Demo

三.部分核心代码

web.xml:  

实现servlet指向CheckUser类和UpdateUser类,指定login.jsp为首页

<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee "> <display-name></display-name> <servlet> <servlet-name>CheckUser</servlet-name> <servlet-class>com.Account.CheckUser</servlet-class> </servlet> <servlet> <servlet-name>UpdateUser</servlet-name> <servlet-class>com.Account.UpdateUser</servlet-class> </servlet> <servlet-mapping> <servlet-name>CheckUser</servlet-name> <url-pattern>/CheckUser</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>UpdateUser</servlet-name> <url-pattern>/UpdateUser</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>

login.jsp:

login.jsp是登陆页面,from表单中加了一个<input type="hidden" value="login" />的隐藏按钮,用于在CheckUser类中判断是登陆还是注册,通过actiontype的值。点击登陆,根据servlet分发到CheckUser类处理请求

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <title>登陆</title> </head> <body> <h2>欢迎登陆</h2> <form action="CheckUser" method="post"> 账号:<input type="text"><br/> 密码:<input type="password"><br/> <input type="hidden" value="login" /> <!-- 用隐藏域来判断登陆还是注册 --> <input type="submit" value="登陆"/><br/><br /> </form> <button>注册</button> ${sessionScope.errorMsg} <!-- EL语法--获取session的errorMsg并显示 --> </body> </html>

CheckUser.java:

实现处理登陆注册的请求 向数据库请求并返回结果

package com.Account; import java.io.IOException; import java.sql.Connection; //import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @SuppressWarnings("serial") public class CheckUser extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); String userName = request.getParameter("userName"); String password = request.getParameter("password"); //md5对密码加密 Md5 md5=new Md5(); //判断类型是登陆还是注册 String Sql=""; String action_type = request.getParameter("actiontype"); if(action_type.equals("login")){ Sql = "SELECT user_id,username,password,phone FROM user " + "where username=\'" + userName+ "\' AND password=\'" + md5.MD5(password) + "\' "; }else if(action_type.equals("register")){ Sql = "INSERT into user (username,password,phone) values (\'"+ userName+"\' ,\'"+md5.MD5(password) +"\',\' \')"; } Connection connection = null; PreparedStatement preSta = null; ResultSet rs = null; // 建立连接,DriverManager是JDBC的管理层 try { //使用连接池的方式连接数据库 ConnectionPool.setUrl(ConnectionPool.getUrl()); ConnectionPool.setUser(ConnectionPool.getUser());//DBTools.getDatabaseUserName() ConnectionPool.setPassword(ConnectionPool.getPassword()); connection = ConnectionPool.getConnection(); preSta = connection.prepareStatement(Sql); UserInfo info = null; if(action_type.equals("login")) { rs = preSta.executeQuery(); while(rs.next()){ info = new UserInfo(); info.setuser_id(rs.getInt("user_id")); info.setUserName(rs.getString("username")); info.setPassword(rs.getString("password")); info.setPhone(rs.getString("phone")); } if(info != null){ session.setAttribute("password", password); session.setAttribute("info", info); String login_suc = "success.jsp"; response.sendRedirect(login_suc); } else{ String login_fail = "login.jsp"; session.setAttribute("errorMsg", "用户名或密码错误,登陆失败!"); response.sendRedirect(login_fail); } } else if(action_type.equals("register")){ int count= preSta.executeUpdate(); if(count>0){ session.setAttribute("registerMsg", "注册成功!"); } else{ session.setAttribute("registerMsg", "注册失败,请重试!"); } response.sendRedirect("register.jsp"); } } catch (Exception e) { e.printStackTrace(); } // 关闭连接 finally { if(connection!=null){ try{ connection.close(); }catch(Exception e){} } if(preSta!=null){ try{ preSta.close(); }catch(Exception e){} } if(rs!=null){ try{ rs.close(); }catch(Exception e){} } } } }

MD5.Java:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwzpsw.html