写在前面
此文只是一个练习的过程,没有用javabean,想看javabean的同学可异步到这个小项目的改写:https://blog.csdn.net/qq_42776455/article/details/83447113 。
登录界面的form表单
login.jsp
<form action="doLogin.jsp" method="post">
<label>用户名:</label>
<input type="text" name="username" value="">
<label>密码:</label>
<input type="password" name="password" value=""><br>
<input type="submit" value="提交">
</form>
创建用户
doLogin.jsp
<%
// 用户创建
class User {
String name;
String tel;
String sex;
String password;
}
List<User> users = new ArrayList();
for (int i = 0; i < 20; i++) {
String sex;
if (i % 2 == 0)
sex = "女";
else
sex = "男";
User user = new User();
user.name = "zhang"+i;
user.sex = sex;
user.tel = "133333333" + i;
user.password = "123456";
// 别忘了把创建的用户添加到users列表里。
users.add(user);
}
登录验证
doLogin.jsp中创建类DoLogin来判断账号密码是否正确。返回boolean类型,true登录成功,false登录失败。
// 登录验证
class DoLogin {
public boolean checkLogin(List<User> users, String username,
String password) {
boolean login_flag = false;
for (User u : users) {
if (u.password.equals(password)
&& u.name.equals(username))
login_flag = true;
}
return login_flag;
}
}
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
DoLogin doLogin = new DoLogin();
boolean check_login = doLogin.checkLogin(users, username, password);
登录成功js管理用户
button标签里的onclick属性值为一个js函数来删除当前节点的。
<td><button onclick="delrow(this)">delete</button>
当前节点只能由父节点的removeChild()方法来删除,要删_tr,就要找到他的父节点_table来删除。
<script type="text/javascript">
function delrow(_obj) {
_td = _obj.parentNode;
_tr = _td.parentNode;
_table = _tr.parentNode;
_table.removeChild(_tr);
}
</script>
登录失败重定向
else {
// 登录失败重定向。
response.sendRedirect("loginFailed.jsp");
}
登录失败页面
loginFailed.jsp
<p style="text-indent: 2em; margin: 30px">
系统将在<span id="time">5</span>秒钟后自动跳转到登录界面,如果未能跳转,
<a href="login.jsp" title="点击访问">请点击</a>
</p>
<% response.setHeader("refresh", "5;URL=login.jsp"); %>
完整代码
login.jsp(系统入口)
<%@ 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 action="doLogin.jsp" method="post">
<label>用户名:</label>
<input type="text" name="username" value="">
<label>密码:</label>
<input type="password" name="password" value=""><br>
<input type="submit" value="提交">
</form>
</body>
</html>
doLogin.jsp
<%@page import="java.awt.Checkbox"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%
// 用户创建
class User {
String name;
String tel;
String sex;
String password;
}
List<User> users = new ArrayList();
for (int i = 0; i < 20; i++) {
String sex;
if (i % 2 == 0)
sex = "女";
else
sex = "男";
User user = new User();
user.name = "zhang"+i;
user.sex = sex;
user.tel = "133333333" + i;
user.password = "123456";
// 别忘了把创建的用户添加到users列表里。
users.add(user);
}
%>
<%
// 登录验证
class DoLogin {
public boolean checkLogin(List<User> users, String username,
String password) {
boolean login_flag = false;
for (User u : users) {
if (u.password.equals(password)
&& u.name.equals(username))
login_flag = true;
}
return login_flag;
}
}
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
DoLogin doLogin = new DoLogin();
boolean check_login = doLogin.checkLogin(users, username, password);
if (check_login) {
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'doLogin.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>
<p>
当前用户<b><%=username%></b>已登录
</p>
<table border="1" cellspacing="0">
<tr>
<th>用户名</th>
<th>电话</th>
<th>性别</th>
<th>密码</th>
<th>操作</th>
</tr>
<%
for (User u : users) {
%>
<tr>
<td><%=u.name%></td>
<td><%=u.tel%></td>
<td><%=u.sex%></td>
<td><%=u.password%></td>
<td><button onclick="delrow(this)">delete</button>
</td>
</tr>
<%
}
%>
</table>
<%
} else {
// 登录失败重定向。
response.sendRedirect("loginFailed.jsp");
}
%>
<script type="text/javascript">
function delrow(_obj) {
_td = _obj.parentNode;
_tr = _td.parentNode;
_table = _tr.parentNode;
_table.removeChild(_tr);
}
</script>
</body>
</html>
loginFailed.jsp
<%@ 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 'loginFailed.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>
<p style="text-indent: 2em; margin: 30px">
系统将在<span id="time">5</span>秒钟后自动跳转到登录界面,如果未能跳转,
<a href="login.jsp" title="点击访问">请点击</a>
</p>
<% response.setHeader("refresh", "5;URL=login.jsp"); %>
</body>
</html>