用JSP实现用户登录验证的功能。
一.第一个程序
1.第一个页面 登录注册框
<%@ 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>Insert title here</title>
</head>
<body>
<form id = "form1" name = "form1" method = "post" action = "5-5-2.jsp">
<ul style="list-style:none">
<li>账号:<input name = "account" type = "text"/></li>
<li>密码:<input name = "password" type = "password"/></li>
<div style="text-align:left">
<li><input name = "Submit" type = "submit" value = "登录"/></li>
</div>
</ul>
</form>
</body>
</html>
2.第二个页面,检测账号密码是否有效。若符合条件则弹出获取到的账号,密码;反之跳出“您输入的账号密码有误,请重新输入! ”的提示语句。
这里设置账号为“yyy”,密码为“111”。
<%@ 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>Insert title here</title>
</head>
<body>
<%
String account = request.getParameter("account");
String password = request.getParameter("password");
if(account.equals("yyy") || password.equals("111"))
{
out.print("您输入的账号密码有误,请重新输入!");
%>
<form id = "form1" name = "form1" method = "post" action = "5-5-3.jsp">
<ul style="list-style:none">
<li>账号:<input name = "account" type = "text"/></li>
<li>密码:<input name = "password" type = "password"/></li>
<div style="text-align:left">
<li><input name = "Submit" type = "submit" value = "登录"/></li>
</div>
</ul>
</form>
<%
}
else
{
out.print("您好," + account + "!");
%>
<form>
<ul style = "list-style:none">
<li>账号:<%= account %></li>
<li>密码:<%= password %></li>
</ul>
</form>
<%
}
%>
</body>
</html>
效果图:
二.第二个页面
第一个页面,登录输入密码界面。
<%@ 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>Insert title here</title>
</head>
<body>
<form id = "form1" name = "form1" method = "post" action = "5-5-4.jsp">
<ul style="list-style:none">
<li>账号:<input name = "account" type = "text"/></li>
<li>密码:<input name = "password" type = "password"/></li>
<div style="text-align:left">
<li><input name = "Submit" type = "submit" value = "登录"/></li>
</div>
</ul>
</form>
</body>
</html>
第二个页面,判断是否符合登录条件。若账号和密码输入为空,则点击登录按钮,出现“非法访问”;若符合条件,则跳出“欢迎您”。
<%@ 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>Insert title here</title>
</head>
<body>
<%
String account = request.getParameter("account");
String password = request.getParameter("password");
if(account.equals("") && password.equals(""))
{
out.print("欢迎你, null" + "<br>");
out.print("非正常访问");
}
else
{
out.print("您好," + account + "!" + "<br>");
out.print("账号:" + account + "<br>");
out.print("密码:" + password);
}
%>
</body>
</html>
效果图: