一次javaweb登陆实验
Posted 暗影蔽日
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一次javaweb登陆实验相关的知识,希望对你有一定的参考价值。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>My JSP ‘login.jsp‘ starting page</title> </head> <body> <form action = "first.jsp" method = "post"> <p>账 号:<input type = "text" name = "ID"></p> <p>密 码:<input type = "password" name = "ipwd"><br><br> <input type = "button" value = "注册" onclick ="window.location.href = ‘sign.jsp‘"> <input type = "submit" value = "登陆"> </p></form> </body> </html>
注册:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <script> function validate(){ var ID = document.form1.newID.value; var pwd = document.form1.newpwd.value; var pwd1 = document.form1.newpwd1.value; if(ID.length>10) alert("账号长度过长,不得超过10位"); else if(pwd!=pwd1) alert("两次密码不一致!"); else document.form1.submit(); } </script> <title>注册</title> </head> <body> <form action = "insert.jsp" name = "form1"> 账 号:<input type = "text" name = "newID"><br> 密 码:<input type = "password" name = "newpwd"><br> 确 认:<input type = "password" name = "newpwd1"><br><br> <input type = "button" value = "返回" onclick ="window.location.href = ‘login.jsp‘"> <input type = "button" value = "提交" onclick ="validate()"/> </form> </body> </html>
登陆成功:
<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%> <html> <head> </head> <body><% out.print("登陆成功!"); %> <a href = "login.jsp">退出</a> </body> </html>
登陆处理:
<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%> <html> <head><title>everything</title> </head> <body> <% String driverName = "com.mysql.jdbc.Driver"; String dbName = "students"; String url1 = "jdbc:mysql://localhost:3306/"+dbName; String url3 = "&useUnicode = true & characterEncoding = UTF-8"; try{ Class.forName(driverName); Connection conn = DriverManager.getConnection(url1,"root","123456"); String sql = "select pwd from user where ID = ?"; PreparedStatement pstmt = conn.prepareStatement(sql); String ID = request.getParameter("ID"); pstmt.setString(1,ID); ResultSet rs = pstmt.executeQuery(); String pwd = ""; while(rs.next()) pwd = rs.getString("pwd"); if(pwd.equals("")) {out.print("用户不存在");} else { String Dpwd = request.getParameter("ipwd"); rs.close(); pstmt.close(); conn.close(); if(pwd.equals(Dpwd)){ %> <jsp:forward page = "access.jsp"></jsp:forward> <% }else {out.print("密码错误");%> <%}}%> <a href = "login.jsp">返回登陆</a> <%}catch(SQLException e) { e.printStackTrace(); }%> </body> </html>
注册处理:
<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%> <html> <head> </head> <body> <% String driverName = "com.mysql.jdbc.Driver"; String dbName = "students"; String url1 = "jdbc:mysql://localhost:3306/"+dbName; try{ Class.forName(driverName); Connection conn = DriverManager.getConnection(url1,"root","123456"); String newID = request.getParameter("newID"); String newpwd = request.getParameter("newpwd"); String sql = "insert into user(ID,pwd) values(?,?)"; String bbl = "select ID from user where ID = ?"; PreparedStatement bbls = conn.prepareStatement(bbl); bbls.setString(1,newID); ResultSet rs = bbls.executeQuery(); String ID = ""; if(!rs.next()) { PreparedStatement psmt = conn.prepareStatement(sql); psmt.setString(1, newID); psmt.setString(2,newpwd); int n = psmt.executeUpdate(); psmt.close(); conn.close();%> <%if(n!=0){ out.print("注册成功!");%> <a href ="login.jsp">返回登陆</a> <% }else { out.print("注册失败!"); } } else{ out.print("用户已存在!");%> <a href = "sign.jsp">返回注册</a> <%} rs.close(); bbls.close(); }catch (SQLException e) {e.printStackTrace();} %> </body> </html>
以上是关于一次javaweb登陆实验的主要内容,如果未能解决你的问题,请参考以下文章
《java精品毕设》基于javaweb宠物领养平台管理系统(源码+毕设论文+sql):主要实现:个人中心,信息修改,填写领养信息,交流论坛,新闻,寄养信息,公告,宠物领养信息,我的寄养信息等(代码片段