【JAVAEE】实现账号注册的功能
本文最后更新于 341 天前,其中的信息可能已经有所发展或是发生改变。

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

[sourcecode language=”java” title=”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;
}
}[/sourcecode]

com/bean/Dbconnect.java

[sourcecode language=”java” title=”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();}
}
}[/sourcecode]

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

[sourcecode language=”java” title=”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;
}
}
[/sourcecode]

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

[sourcecode language=”java” title=”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);
}

}[/sourcecode]

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

[sourcecode language=”html” title=”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>[/sourcecode]

byname/input.jsp

[sourcecode language=”html” title=”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>[/sourcecode]

byname/failure.jsp

[sourcecode language=”html” title=”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>[/sourcecode]

byname/register.jsp

[sourcecode language=”html” title=”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>[/sourcecode]

数据库文件下载:[bdbtn]https://ge.benzhu.xyz/?/%E4%BB%A3%E7%A0%81/%E6%BA%90%E4%BB%A3%E7%A0%81/%E6%95%B0%E6%8D%AE%E5%BA%93/benzhu.sql[/bdbtn]

效果图:

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇