JSP+Mysql实现登录注册
jsP+Mysql实现登录注册
最近学习了下jsp,感觉之前学过的Html帮上了一点作用,但是这点东西远远不够,在实现登录、注册界面的过程发现大多数使用的是JavaScript等一些知识,接下来小编来展示下自己的成果,希望能帮助初学者能更好的理解。
1.登录界面Login.jsp
2.登录判断界面CheckLogin.jsp
3.注册界面register.jsp
4.注册判断界面CheckRegister.jsp
5.登录成功界面Home.jsp
1.登录界面
2.登录判断界面( 这个页面首先取得L ogin.jsp 提交过来的数据,然后连接数据库,通过 sql 语句在数据库中进行查询比对,看有没有此用户,如果有,提示用户登陆成功,反之,登录失败)
<% String username = new String(request.getParameter("username").getBytes("ISO8859_1"),"utf-8"); String password = new String(request.getParameter("password").getBytes("ISO8859_1"),"utf-8");//取得从Login.jsp提交过来的数据//连接数据库的基本操作 Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://127.0.0.1:3306/student"; String usename = "root"; String psw= "1234"; Connection conn = DriverManager.getConnection(url, usename, psw); if(conn != null){ String sql = "select * from user where name='"+username+"' and password='"+ password + "'"; Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); if(rs.next()){ out.println(""); //response.sendRedirect("Home.jsp"); }else{ out.println(""); // response.sendRedirect("Login.jsp");//密码不对返回到登陆 } } %>
3.注册界面
4.注册判断界面
<% String user = new String(request.getParameter("username").getBytes("ISO8859_1"),"utf-8"); String pwd = new String(request.getParameter("password").getBytes("ISO8859_1"),"utf-8"); Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://127.0.0.1:3306/student"; String usename = "root"; String psw= "1234"; Connection conn = DriverManager.getConnection(url,usename,psw);//得到连接 PreparedStatement pStmt = conn.prepareStatement("select * from user where name = '" + user + "'"); ResultSet rs = pStmt.executeQuery(); if(rs.next()){ out.println(""); }else{ PreparedStatement tmt = conn.prepareStatement("Insert into user values('" + user + "','" + pwd + "')"); int rst = tmt.executeUpdate(); if (rst != 0){ out.println(""); }else{ out.println(""); } } %>5.登录成功界面
重新登陆
5.1登录界面添加显示个人信息的功能(从数据库获取全部人的信息)
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><%@ page language="java" import="java.sql.*,java.io.*,java.util.*"%>
<% out.print("姓名"); %> | <% out.print("密码"); %> |
---|---|
<% out.print(rs.getString(1)); %> | <% out.print(rs.getString(2)); %> |
<% out.print("数据查询成功,恭喜你"); %>