一只倔强的笨猪

【JAVAEE】实现账号注册的功能

1.编写javaBean的用户类和连接数据库类。
com/bean/User.java

package com.bean;

public class User {
	private int id;
	private String name;
	private String pwd;
	private int power;
	public User() {}
	public User(int id,String name,String pwd,int power) {
		this.id=id;
		this.name=name;
		this.pwd=pwd;
		this.power=power;
	}
	public User(String name,String pwd) {
		this.pwd=pwd;
		this.name=name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public int getPower() {
		return power;
	}
	public void setPower(int power) {
		this.power = power;
	}		
}

com/bean/Dbconnect.java

package com.bean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DbConnect {
	public static Connection getDBconnection() {
		String shujuku = "benzhu";
		String url1 ="jdbc:mysql://localhost:3306/"+shujuku;
		String uname = "root";
		String upwd="123456";
		Connection conn=null;
		try{			
			Class.forName("com.mysql.jdbc.Driver");
			conn= DriverManager.getConnection(url1, uname, upwd);
			System.out.println("哈哈哈哈 连接成功");
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return conn;
	}
	public static void Close(Connection conn,Statement stmt,ResultSet rs) {
		try{if(rs!=null) rs.close();}catch(Exception e){e.printStackTrace();}
		try{if(stmt!=null) stmt.close();}catch(Exception e){e.printStackTrace();}
		try{if(conn!=null) conn.close();}catch(Exception e){e.printStackTrace();}
	}
}

2.编写插入数据库数据和查询重名的dao方法。
com/dao/UserDao.java

package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import com.bean.DbConnect;
import com.bean.User;

public class UserDao {
public User findUserByname(String name) {
		User user1=null;
		try{
			Connection conn;
			conn = DbConnect.getDBconnection();
			Statement stmt = conn.createStatement();
			String sql="select * from benzhu_user where user_name='"+name+"'";
			ResultSet rs=stmt.executeQuery(sql);
			if(rs.next()) {
				int id = rs.getInt(1);
				String name1 = rs.getString(2);
				String pwd = rs.getString(3);
				int power = rs.getInt(4);
				user1 = new User(id,name1,pwd,power);			
			}
			else {
				int id= 0;
				String name1 = "name";
				String pwd = "pwd";
				int power = 0;
				user1 = new User(id,name1,pwd,power);
			}
			DbConnect.Close(conn, stmt, rs);			
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return user1;
	}
public int findUserByuserz(User user) {
		int user1=0;
		try{
			Connection conn;
			conn = DbConnect.getDBconnection();
			String sql="insert into benzhu_user(user_name,user_pwd,user_power) values(?,?,1)";
			PreparedStatement st1 = conn.prepareStatement(sql);
			st1.setString(1, user.getName());
			st1.setString(2, user.getPwd());
			user1 = st1.executeUpdate();			
			DbConnect.Close(conn, st1, null);
			
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return user1;	
	}
}

3.处理jsp提交页面的servlet。
com/servlet/userRegServlet.java

package com.servlet;

import java.io.IOException;
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 com.bean.User;
import com.dao.UserDao;


@WebServlet("/userRegServlet")
public class userRegServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("username");
		String pwd = request.getParameter("userpwd");
		User user = new User(name,pwd);
		UserDao dao1 = new UserDao();
		request.setAttribute("u", name);
		if(dao1.findUserByname(name).getId()!=0) {
			//存在同名用户
			request.getRequestDispatcher("byname/failure.jsp").forward(request, response);			
		}
		else {
			if(dao1.findUserByuserz(user)!=0) {
				request.getRequestDispatcher("byname/input.jsp").forward(request, response);
			}
			else {
				request.getRequestDispatcher("byname/register.jsp").forward(request, response);
			}
		}
	}	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

4.编写jsp页面。
byname/zhuce.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>注册页面</title>

</head>
<script type="text/javascript" language="javascript"> 
function validate(){
	var name=document.forms1.username.value;
	var pwd=document.forms1.userpwd.value;
	var pwd1=document.forms1.userpwd1.value;
	if(name.length<=0) alert("用户名不能为空");
	else if(pwd!=pwd1) alert("两次密码不一致!");
	else document.forms1.submit();
}
</script> 
<body>

<form action="../userRegServlet" method="post" name="forms1">
<h1 align="center">注册页面</h1>
	 用户名:<input type="text" name="username"/><br/>
	 密码:<input type="password" name="userpwd"/><br/>
	 确认密码:<input type="password" name="userpwd1"/><br/>
	<input type="Button"  value="注册" onClick="validate()"/>
    <input type="reset"/>
</form>
</body>
</html>

byname/input.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>注册成功</title>
</head>
<body>
${requestScope.u },注册成功!
<a href="byname/login.jsp">单击转跳登陆界面</a>
</body>
</html>

byname/failure.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
用户名${requestScope.u }已经被使用。
<a href="byname/zhuce.jsp">单击转跳重新注册</a>
</body>
</html>

byname/register.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>注册失败</title>
</head>
<body>
${requestScope.u },注册失败,服务器错误!
<a href="byname/zhuce.jsp">单击转跳注册页面</a>
</body>
</html>

数据库文件下载: 本地下载

效果图:
《【JAVAEE】实现账号注册的功能》

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注