jsp实现账户登录、注册!

2018-06-24 02:00:06来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

 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>

用户注册图片:

 

     

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:bootstrap-table 分页增删改查之一(分页)

下一篇:css制作三分圆形