1. 程式人生 > >jsp實現賬戶登錄、註冊!

jsp實現賬戶登錄、註冊!

type com mage 連接失敗 ext etc rac gets dir

jsp連接mysql數據庫進行賬戶登錄驗證和賬戶註冊

~jsp: Login.jsp 、LoginCl.jsp、Welcome.jsp、Register.jsp、login.css

login.css:

#c1{
border-style:solid;
border-width:5px;
background-color:#ffe4c4;
text-align:center;
hight:300px;
width:500px;
padding:5px;

}
#c2{
background-image:url("taobao.PNG");
background-attachment: fixed;
}
#c3{
font-weight:700;
text-align:left;
}
#c4{

font-size:10px;

}

思路:

  1.學習淘寶登錄界面,構建登錄界面Login.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>
<link rel=stylesheet type=text/css href="Login.css" >
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>JSP Login</title>
</head>
<body id="c2">
<center>
<div id="c1">
<form action="LoginCl.jsp" method="post">
<h3 id="c3"><b> 密碼登錄</b></h3>
<h3>賬號:<input type="text" name="user" size=6></h3>
<h3>密碼:<input type="password" name="passwd" size=6></h3>
<hr>
<input type="submit" value="登錄">
</form>
<a id="c4" href="">忘記密碼</a>
<a id="c4" href="">忘記賬號</a>
<a id="c4" href="Rigester.jsp"> 免費註冊 </a>

</div>
</center>
</body>
</html>

技術分享

  2.由Login.jsp 提交表單->LoginCl.jsp 接收 ,並在LonginCl.jsp中連接數據庫

LoginCl.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.sql.*"%>
<!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>
<%try{

//以下是連接數據庫 ,調試過程請刪除這些註釋!!

Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf-8&useSSL=false";
Connection con=DriverManager.getConnection(url,"root","guoxiaotong");
System.out.println("連接成功1");
Statement cmd = con.createStatement();

String Rigname=request.getParameter("Rigname"); //註冊用戶名
String passwd=request.getParameter("passwd");//註冊用戶密碼


String n1=request.getParameter("user");//登錄用戶名
String n2=request.getParameter("passwd");//登錄密碼

if(n1!=null&&n2!=null){ //已有用戶登錄 數據庫查詢

String sql="select * from username1 where uname=\""+n1+"\"";
ResultSet rs=cmd.executeQuery(sql);

System.out.println("連接成功5");
while(rs.next()){
System.out.println("連接成功6");
String a1=rs.getString(1);
String a2=rs.getString(2);
String a3=rs.getString(3);
System.out.println(a1+"-"+a2+"-"+a3);
if((rs.getString(3)).equals(n2)){
session.setAttribute("name",n1); //jsp自帶session保存消息 再跳轉後通過session.getAttribute("name") 獲得保存消息
session.setAttribute("ID", a1);
response.sendRedirect("Welcome.jsp");
}else {
response.sendRedirect("Login.jsp");
}

}
}
if(Rigname!=null&&passwd!=null){ //註冊用戶登錄

//判斷表總記錄條數
String sqlcount="select * from username1";
ResultSet m=cmd.executeQuery(sqlcount);
int sum=0;
while(m.next()){

sum++;
}
System.out.println("連接成功2,總數sum是:"+sum);
sum++; //sum表示記錄條數


String sql="insert into username1 values(\‘"+sum+"\‘,"+"\‘"+Rigname+"\‘,"+"\‘"+passwd+"\‘)";
System.out.println("連接成功3:"+sql);
cmd.executeUpdate(sql);
System.out.println("更新成功!");
String sqla="select * from username1 where uname=\""+Rigname+"\"";
ResultSet rs=cmd.executeQuery(sqla);

System.out.println("連接成功5");
while(rs.next()){

if((rs.getString(3)).equals(passwd)){
session.setAttribute("name",Rigname); //jsp自帶session保存信息
session.setAttribute("ID", sum);
response.sendRedirect("Welcome.jsp");
}else {
response.sendRedirect("Rigester.jsp");
}

}
}

}catch(Exception e){
System.out.println("連接失敗");
}

%>
</body>
</html>

  3.由LoginCl.jsp跳轉到Welcome.jsp,並接收session和request信息

<%@ 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">
<link >
<title>歡迎界面</title>
</head>
<body>

<center>
<h1>歡迎,<%=session.getAttribute("name") %>你好!</h1>
<br>您的ID:<%=session.getAttribute("ID") %>
</center>
</body>
</html>

用戶登錄成功圖片:

技術分享

  4.在Login.jsp中點擊賬戶註冊超鏈接跳轉到Register.jsp,填寫完表單點擊申請註冊跳轉到Login.jsp的第二個if判斷語句進行數據庫insert操作

  

<%@ 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><center>
<h1>填寫賬戶信息</h1>
<hr>
<form action="LoginCl.jsp">
賬戶名:<input type="text" name="Rigname" size=10><br>
密碼:&nbsp;<input type="password" name="passwd" size=10><br>
<p>
<p>
<input type="submit" value="申請註冊">

</form>

</center>

</body>
</html>

用戶註冊圖片:

技術分享

jsp實現賬戶登錄、註冊!