JSP实现用户登录、注册和退出成果(3)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"https://www.jb51.net/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="https://www.jb51.net/<%=basePath%>"> <title>注册页面</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link type="text/css" href="https://www.jb51.net/styles.css"> --> </head> <body> <form action="register_action.jsp" method="post"> <table> <tr> <td colspan="2">注册窗口</td> </tr> <tr> <td>用户名:</td> <td><input type="text" /></td> </tr> <tr> <td>暗码:</td> <td><input type="text" /></td> </tr> <tr> <td>确认暗码:</td> <td><input type="text" /></td> </tr> <tr> <td>email:</td> <td><input type="text" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="注册" /> <a href="https://www.jb51.net/login.jsp">返回</a></td> </tr> </table> </form> </body> </html>

当在登录页面点击“注册“时打开用户注册页面

结果

JSP实现用户登录、注册和退出成就

4.6注册逻辑处理惩罚页面

register_action.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="java.sql.*" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"https://www.jb51.net/"; %> <% String username = request.getParameter("username"); String password1 = request.getParameter("password1"); String password2 = request.getParameter("password2"); String email = request.getParameter("email"); if(username==null||"".equals(username.trim())||password1==null||"".equals(password1.trim())||password2==null||"".equals(password2.trim())||!password1.equals(password2)){ //out.write("用户名或暗码不能为空!"); System.out.println("用户名或暗码不能为空!"); response.sendRedirect("https://www.jb51.net/register.jsp"); return; //request.getRequestDispatcher("https://www.jb51.net/login.jsp").forward(request, response); } boolean isValid = false; Connection con = null;// 建设一个数据库毗连 PreparedStatement pre = null;// 建设预编译语句工具,一般都是用这个而不消Statement ResultSet result = null;// 建设一个功效集工具 try { Class.forName("oracle.jdbc.driver.OracleDriver");// 加载Oracle驱动措施 //System.out.println("开始实验毗连数据库!"); String url = "jdbc:oracle:" + "thin:@127.0.0.1:1521:orcl";// 127.0.0.1是本机地点,orcl是Oracle的默认数据库名 String user = "scott";// 用户名,系统默认的账户名 String pwd = "tiger";// 你安装时选配置的暗码 con = DriverManager.getConnection(url, user, pwd);// 获取毗连 //System.out.println("毗连乐成!"); String sql = "select * from p_user where username=?";// 预编译语句,“?”代表参数 pre = con.prepareStatement(sql);// 实例化预编译语句 pre.setString(1, username);// 配置参数,前面的1暗示参数的索引,而不是表中列名的索引 result = pre.executeQuery();// 执行查询,留意括号中不需要再加参数 if (!result.next()){ sql = "insert into p_user(id,username,password,email) values(?,?,?,?)";// 预编译语句,“?”代表参数 pre = con.prepareStatement(sql);// 实例化预编译语句 pre.setString(1, System.currentTimeMillis()+"");// 配置参数,前面的1暗示参数的索引,而不是表中列名的索引 pre.setString(2, username);// 配置参数,前面的1暗示参数的索引,而不是表中列名的索引 pre.setString(3, password1);// 配置参数,前面的1暗示参数的索引,而不是表中列名的索引 pre.setString(4, email);// 配置参数,前面的1暗示参数的索引,而不是表中列名的索引 pre.executeUpdate();// 执行 isValid = true; } } catch (Exception e) { e.printStackTrace(); } finally { try { // 逐一将上面的几个工具封锁,因为不封锁的话会影响机能、而且占用资源 // 留意封锁的顺序,最后利用的最先封锁 if (result != null) result.close(); if (pre != null) pre.close(); if (con != null) con.close(); //System.out.println("数据库毗连已封锁!"); } catch (Exception e) { e.printStackTrace(); } } if(isValid){ System.out.println("注册乐成,请登录!"); response.sendRedirect("https://www.jb51.net/login.jsp"); return; }else{ System.out.println("用户名已存在!"); response.sendRedirect("https://www.jb51.net/register.jsp"); return; } %>

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

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