jsp-login

jsp登录

学习了jsp的登录,下面直接给出示例代码

一、简单的登录注册代码

1、登录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</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 rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form name="loginForm" method="post" action="welcome.jsp">
<table>
<tr>
<td>用户名:<input type="text" name="userName" id="userName"></td>
</tr>
<tr>
<td>密码:<input type="password" name="password" id="password"></td>
</tr>
<tr>
<td><input type="submit" value="登录" style="background-color:pink">
<input type="reset" value="重置" style="background-color:red">
</td>
</tr>
</table>
</form>
</body>
</html>

2、注册

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<script language="javascript">
function on_submit()
{ // 验证数据的合法性
if(form1.username.value=="")
{
alert("用户名不能为空,请输入用户名!");
form1.username.focus();
return false;
}
if(form1.userpassword.value=="")
{
alert("用户密码不能为空,请输入密码!");
form1.username.focus();
return false;
}
if(form1.reuserpassword.value=="")
{
alert("用户确认密码不能为空,请输入密码!");
form1.reuserpassword.focus();
return false;
}
if(form1.userpassword.value!=form1.reuserpassword.value)
{
alert("密码与确认密码不同");
form1.userpassword.focus();
return false;
}
}
</script>
<script>
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<base href="<%=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 rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form method="post" action="register.jsp" name="form1" onsubmit="return on_submit()">
新用户注册<br>
用户名(*):<input type="text" name="username" size="20"><br>
密 码(*):<input type="password" name="password" size="20"><br>
再输一次密码(*):<input type="password" name="reuserpassword" size="20"><br>
性别:<input type="radio" value="男" checked name="sex">男
<input type="radio" name="sex" value="女">女<br>
出生年月:<input name="year" size="4" maxlength=4>年
<select name="month">
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>月
<input name="day" size="3" maxlength=4>日
<br>
所在地: <select name="所在地">
<option>宁夏</option>
<option>北京</option>
<option>上海</option>
<option>贵州</option>
</select>
<br>
<!--电子邮件(*):<input name="E-mail" max-length="28"><br> -->
兴趣:<input type="checkbox" name="habit" value="">
看书
<input type="checkbox" name="habit" value="">
足球
<input type="checkbox" name="habit" value="">
旅游
<input type="checkbox" name="habit" value="">
唱歌
<input type="checkbox" name="habit" value="">
看电视
<input type="checkbox" name="habit" value="">
跳舞 <br>
<!-- 家庭住址:<input type="text" name="address" size="20"> -->
<textarea rows="10" cols="50"></textarea>
<br>
<input type="submit" value="提交" name="B1"><input type="reset" value="全部重写" name="B2"><br>
</form>
</body>
</html>

3、通过验证,登录成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'welcome.jsp' starting page</title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<style>
body{
margin:0 auto;
padding:0;
font-family:"微软雅黑" ;
}
.container{
min-width: 960px;
}
.header{
height: 50px;
line-height: 50px;
text-align: center;
font-size: 20px;
color: black;
}
#nav{
position:relative; z-index:9999;
text-align: center;
height:50px;
line-height: 50px;
background-color: gainsboro;
}
#nav ul{
width:800px;
list-style: none;
}
#nav ul .li{
background: #eee;
height: 37px;
width: 154px;
line-height: 37px;
border-bottom: 1px solid #ccc;
text-align: center;
float: left;
margin-right: 10px;
display: inline-block;
}
a{
text-decoration: none;
}
#nav ul .li a{
color: #000;
display: block;
width: 145px;
height: 37px;
background: greenyellow;
}
#nav ul .li a:hover{
background-color: #000000;
background: orange;
}
#nav ul .li ul .li1{
float: none;
width: 154px;
margin: 0;
}
#nav ul .li ul .li1 a{
background: none;
width: 154px;
}
#nav ul .li ul .li1 a:hover{
background: orange;
color: #FFFFFF;
}
#nav ul .li ul{
display: none;
width:154px;
}
#nav ul li ul .li1 a{
}
#nav ul .li:hover ul{
position:absolute; z-index:9999;
display: block;
background: greenyellow;
}
#nav ul .li:hover{
background: orchid;
}
//正文
。content img{
width:20px;
margin:10px;
float:left;
}
</style>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");//对客户端请求进行重新编码的编码
%>
<h3>登录成功!</h3> <br>
<h4>欢迎您, <%=request.getParameter("username") %></h4>
<hr>
<div class="container">
<div class="header"><a href="index.html">购物商城欢迎您!</a></div>
<div id="nav">
<ul>
<li class="li"><a href="index.html">首页</a>
</li>
<li class="li">闪购
<ul>
<li class="li1"><a href="">今日上新</a></li>
<li class="li1"><a href="">潮流服饰</a></li>
<li class="li1"><a href="">运动户外</a></li>
</ul>
</li>
<li class="li">潮流
<ul>
<li class="li1"><a href="">今日上新</a></li>
<li class="li1"><a href="">潮流服饰</a></li>
<li class="li1"><a href="">运动户外</a></li>
</ul>
</li>
<li class="li">运动户外
<ul>
<li class="li1"><a href="">今日上新</a></li>
<li class="li1"><a href="">潮流服饰</a></li>
<li class="li1"><a href="">运动户外</a></li>
</ul>
</li>
</ul>
</div>
<!--content-->
<div class="content">
<img alt="" src="/upload/content.png">
<img alt="" src="/upload/tt.png">
</div>
</div>
<hr>
<div>
</div>
</body>
</html>

二、当用户名,密码与规定的代码的密码和用户名一致,就登录成功

1、验证代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'check.jsp' starting page</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 rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
request.setCharacterEncoding("utf-8");//对客户端请求进行重新编码的编码
String username=request.getParameter("username");
String password=request.getParameter("password");
// System.out.println(username);
if(username.equals("123")&&password.equals("123")){
%>
<jsp:forward page="welcome.jsp"/>
<%
}else{
%>
<jsp:forward page="login.jsp"/>
<%
}
%>
</body>
</html>

三、记录用户名和密码,并在下一个页面中显示出来

1、登录页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<%@ page language="java" contentType="text/html; charset=utf-8" import="java.util.*"
pageEncoding="utf-8"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>用户登录表</title>
</head>
<body>
<%! // 定义若干个数据库的连接常量
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
public static final String DBURL = "jdbc:mysql://localhost:3306/mydb" ;//注意和你建的数据库名一致
public static final String DBUSER = "root" ;//mysql用户为root
public static final String DBPASS = "" ;//密码为空
%>
<%!int count=0;%>
<div style="width:200px;height:250px;margin:15px auto 50px;padding:40px 80px;color:green;">
<form action="record.jsp" method="post" name="form1">
<h3>姓名:<input type="text" name="user"></h3>
<h3>密码:<input type="password" name="pwd"></h3>
<h3><input type="submit" name="sub" value="登录">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" name="rest" value="重置密码"></h3>
</form>
</div>
<%
if(application.getAttribute("number")==null)
application.setAttribute("number","0");
else{count=Integer.parseInt((String)(application.getAttribute("number")));
count++;
application.setAttribute("number",Integer.toString(count));
}
out.print("登录成功!"+"<br>我的用户名:"+application.getAttribute("user")+"<br>我的密码:"+application.getAttribute("pwd"));
%>
你是第<%=(String)application.getAttribute("number")%>次用户登录
<br>
</body>
</html>

2、显示用户名和密码(已保存的)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.util.*"%>
<!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 >
<%!Vector v=new Vector();Vector v_p=new Vector();ServletContext application;
synchronized void sendMessage(String s,String s1){
application=getServletContext();
v.add(s);
v_p.add(s1);
application.setAttribute("pwd", v_p);
application.setAttribute("user", v);
}
%>
<%
request.setCharacterEncoding("utf-8");
String usert=request.getParameter("user");
String pwdt=request.getParameter("pwd");
sendMessage(request.getParameter("user"),request.getParameter("pwd"));
if(usert.equals("")&&pwdt.equals(""))
{
out.print("此选项不能为空,3s后返回主页!!!");
response.setHeader("Refresh", "3;index.jsp");
}
if(usert.equals(application.getAttribute("user"))&&pwdt.equals(application.getAttribute("pwd")))
out.print("<br>我的用户名:"+application.getAttribute("user")+"<br>我的密码:"+application.getAttribute("pwd"));
%>
<a href="index.jsp"><br>去首页inedx.jsp页面</a>
<%
%>
</body>
</html>

四、当密码和用户名与数据库中的一致,就登录成功了

1、登录页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%>
<html>
<head>
<title>
用户登录
</title>
</head>
<body bgcolor="#e3e3e3">
<center>
<form action="check_login.jsp" method="post">
<table>
<caption>用户登录</caption>
<tr><td>用户名:</td><td><input type="text" name="username" size="20"/></td></tr>
<tr><td>密码:</td><td><input type="text" name="password" size="20"/></td></tr>
<tr><td><input type="submit" value="登录"/><td><input type="reset" value="重置"/>
</table>
</form>
</body>
</center>
</html>

2、验证代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<%@ page language="java" import="java.sql.*" contentType="text/html;charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'check.jsp' starting page</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 rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String username=request.getParameter("username");
String password=request.getParameter("password");
try {
// 加载数据库驱动,注册到驱动管理器
Class.forName("com.mysql.jdbc.Driver");
// 数据库连接字符串
String url = "jdbc:mysql://localhost:3306/checkmysql";
// 数据库用户名
String usename = "root";
// 数据库密码
String psw = "123123";
// 创建Connection连接
Connection conn = DriverManager.getConnection(url,usename,psw);
// 判断 数据库连接是否为空
if(conn != null){
String sql="select * from emp where empno='"+username+"' and password='"+ password + "'";
Statement stmt = conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()){
request.getRequestDispatcher("success.jsp").forward(request,response);
}else{
response.sendRedirect("wrong.jsp");
%>
<%
}
// 输出连接信息
//out.println("数据库连接成功!");
// 关闭数据库连接
conn.close();
}else{
// 输出连接信息
out.println("数据库连接失败!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
%>
</body>
</html>

3、登录成功页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success.jsp' starting page</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 rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
登陆成功,欢迎您! <%=request.getParameter("username")%> <br>
</body>
</html>

4、失败页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<%@ page language="java" import="java.sql.*" contentType="text/html;charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=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 rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<% out.println("登录失败");%>
<% response.setHeader("refresh","5;url=index.jsp");%>
<a href="javascript:history.back()">返回</a>
</body>
</html>