首页 > 开发 > Java > 正文

jsp与Javabean实现登录注册

2024-07-21 02:52:52
字体:
来源:转载
供稿:网友

jsp与javabean实现登录注册

jsp与Javabean实现登录注册jsp技术MySQL数据库编程结合javabean组件实现最简单基础的一个注册登录demoJavaBean开发表示用户信息的JavaBeanUserInfojava在Mysql数据库中创建数据库及相应的表格注册用户时执行数据库操作的JavaBeanUserRegistjava用户登录时连接数据库处理的JavaBeanUserLoginjavaJSP页面的开发填写用户注册信息的JSP页面registerjsp用户登录页面loginjsp运行结果

jsp技术+Mysql数据库编程,结合javabean组件,实现最简单基础的一个注册登录demo

思路:首先需要登录页面以及相应的登录验证,对于未注册用户需要点击注册按钮进行用户信息注册,注册同样需要一个注册表单以及各个字段的验证处理。为了简化JavaBean中的处理和重用,我们把用户的信息封装成一个值对象的JavaBean,这个JavaBean只包含 用户信息相关的属性和这些属性相关的getter和setter方法,我们将其命名为UserInfo.java,其次,随着JSP技术的发展和开发技术的成熟,在JSP页面中连接数据库的编程方式已经很少见,虽然在JSP页面中使用Scriptlets也可以连接数据库,进行数据库操作,但这样会造成页面难以维护等缺点,在大型项目中,这些缺点会更加突出,所以我们建议在JavaBean中连接数据库,UserRegiste.java用来处理注册时连接数据库查询事务,UserLogin.java用来处理登录时连接数据库插入事务。

JavaBean开发

表示用户信息的JavaBean(UserInfo.java)

public class UserInfo { //属性 PRivate String name; private String passWord; private String phone; //方法 public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; }}

在Mysql数据库中创建数据库及相应的表格

create database jspdev;use jspdev;create table user_Info( name varchar(15), password varchar(20), phone varchar(15));

注册用户时执行数据库操作的JavaBean(UserRegist.java)

import java.io.UnsupportedEncodingException;import java.sql.DriverManager;import com.mysql.jdbc.Connection;import com.mysql.jdbc.PreparedStatement;public class UserRegist { private UserInfo userInfo; private Connection con; // 获得数据库连接 public UserRegist() { String Classforname = "com.mysql.jdbc.Driver"; String Servanddb = "jdbc:mysql://127.0.0.1/jspdev"; String user = "root"; String pwd = "123456"; try { Class.forName(Classforname); con = (Connection) DriverManager.getConnection(Servanddb, user, pwd); } catch (Exception e) { e.printStackTrace(); } } // 设置待注册的用户信息 public void setUserInfo(UserInfo userInfo) { this.userInfo = userInfo; } // 进行注册 public void regist() throws Exception { String reg = "insert into user_Info values(?,?,?)"; try { PreparedStatement pstmt = (PreparedStatement) con.prepareStatement(reg); pstmt.setString(1, transFormat(userInfo.getName())); pstmt.setString(2, userInfo.getPassword()); pstmt.setString(3, userInfo.getPhone()); pstmt.execute(); } catch (Exception e) { e.printStackTrace(); throw e; } } String transFormat(String s) { try { byte[] b = s.getBytes("iso-8859-1"); s = new String(b); } catch (UnsupportedEncodingException e) { System.out.println(e.toString()); } return s; }}

用户登录时连接数据库处理的JavaBean(UserLogin.java)

import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;import com.mysql.jdbc.Connection;public class UserLogin { private UserInfo userInfo; private Connection con; // 获得数据库连接 public UserLogin() { String Classforname = "com.mysql.jdbc.Driver"; String Servanddb = "jdbc:mysql://127.0.0.1/jspdev"; String user = "root"; String pwd = "123456"; try { Class.forName(Classforname); con = (Connection) DriverManager.getConnection(Servanddb, user, pwd); } catch (Exception e) { e.printStackTrace(); } } // 获取已经注册的用户信息 public void setUserInfo(UserInfo userInfo) { this.userInfo = userInfo; } // 进行登录查询验证 public boolean login(String name, String password) throws Exception { try { Statement stmt = con.createStatement(); ResultSet rst = stmt.executeQuery( "select name, password from user_Info where name= '" + name + "' and password='" + password + "'"); if (rst.next()) { rst.close(); stmt.close(); con.close(); return false; } else { rst.close(); stmt.close(); con.close(); return true; } } catch (Exception e) { e.printStackTrace(); } return false; }}

JSP页面的开发

填写用户注册信息的JSP页面(register.jsp)

<%@page import="org.apache.jasper.tagplugins.jstl.core.Out"%><%@ page language="java" contentType="text/html; charset=gb2312"%><%@ page import="java.sql.*,java.io.*"%><jsp:useBean id="info" class="webUser.UserInfo" scope="request"> <jsp:setProperty property="*" name="info" /></jsp:useBean><jsp:useBean id="regist" class="webUser.UserRegist" scope="request"> <jsp:setProperty property="*" name="regist" /></jsp:useBean><%! String transFormat(String s){ try{ byte []b=s.getBytes("iso-8859-1"); s=new String(b); } catch(UnsupportedEncodingException e){ System.out.println(e.toString()); } return s;}%><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>登录首页</title><script type="text/Javascript"> function register() { if (document.registerform.name.values == "") { window.alert("请输入用户名称"); document.registerform.name.focus(); return false; } if (document.registerform.password.value == "") { window.alert("请输入密码"); document.registerform.password.focus(); return false; } if (document.registerform.repassword.value == "") { window.alert("请确认密码"); document.registerform.repassword.focus(); return false; } if (document.registerform.password.value != document.registerform.repassword.value) { window.alert("两次密码不同,请重新输入"); document.registerform.repassword.focus(); return false; } if (document.registerform.phone.value == "") { window.alert("请输入电话号码"); document.registerform.phone.focus(); return false; } return true; }</script></head><body bgcolor=cyan> <font size=4> <% /* request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=utf-8"); */ String name = request.getParameter("name"); String password = request.getParameter("password"); String repassword = request.getParameter("repassword"); String phone = request.getParameter("phone"); if (name == null) { name = ""; if (password == null) { password = ""; if (repassword == null) { repassword = ""; if (phone == null) { phone = ""; } } } } boolean isSuccessRegist = false; if (password.equals(repassword) && !name.equals("") && !password.equals("") && !repassword.equals("") && !phone.equals("")) { name=transFormat(name); regist.setUserInfo(info); regist.regist(); isSuccessRegist = true; } %> <form action="" method=post name=registerform> <table width="100%" height="500" align="center"> <tr> <td> <div heigh="100%" align="center"> <table border="0" align="center"> <tr> <td>用户名:</td> <td><input type="text" name="name"></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password"></td> </tr> <tr> <td>确认密码:</td> <td><input type="password" name="repassword"></td> </tr> <tr> <td>手机号码:</td> <td><input type="text" name="phone"></td> </tr> </table> <p align="center"> <a href="login.jsp"><input type="button" value="登录" name="button"></a> <input type="submit" value="注册" name="regist" onclick="register()"> </p> <font size=4 color=red> <% if(isSuccessRegist){ out.println(name); out.println("注册成功"); } %> </font> </div> </td> </tr> </table> </form> </font></body></html>

用户登录页面(login.jsp)

<%@ page language="java" contentType="text/html; charset=gb2312"%><jsp:useBean id="info" class="webUser.UserInfo" scope="request"> <jsp:setProperty property="*" name="info" /></jsp:useBean><jsp:useBean id="login" class="webUser.UserLogin" scope="request"> <jsp:setProperty property="*" name="login" /></jsp:useBean><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>登录首页</title><script type="text/javascript"> function login() { if (document.registerform.name.values == "") { window.alert("请输入用户名称"); document.registerform.name.focus(); return false; } if (document.registerform.password.value == "") { window.alert("请输入密码"); document.registerform.password.focus(); return false; } return true; }</script></head><body bgcolor=cyan> <font size=4> <% String name = request.getParameter("name"); String password = request.getParameter("password"); if (name == null) { name = ""; if (password == null) { password = ""; } } boolean isSuccessLogin = false; if (!name.equals("") && !password.equals("")) { login.setUserInfo(info); isSuccessLogin = login.login(name,password); if (isSuccessLogin) { response.sendRedirect("welcome.jsp"); } isSuccessLogin = false; } %> <form action="" method=post name=form> <table width="100%" height="500" align="center"> <tr> <td> <div heigh="100%" align="center"> <table border="0" align="center"> <tr> <td>用户名:</td> <td><input type="text" name="name"></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password"></td> </tr> </table> <p align="center"> <input type="submit" value="登录" name="login" onclick="login()"> <a href="register.jsp"><input type="button" value="注册" name="button"></a> </p> </div> </td> </tr> </table> </form> </font></body></html>### 登录成功跳转页面(Main.jsp) ###<%@ page language="java" contentType="text/html; charset=gb2312"%><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>主页面</title></head><body bgcolor=cyan><font size=6 color=red><div height="100%" align="center" valign="center"> <br><br>欢迎来到本页面!!!</div></font></body></html>

运行结果

登录页面

注册页面

注册验证

注册成功

查询注册结果


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表