JSP简单用户管理(无javabean)
程序员文章站
2022-07-14 19:58:43
...
写在前面
此文只是一个练习的过程,没有用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>
推荐阅读
-
jsp+servlet+mysql简单实现用户登陆注册
-
JSP实现简单的用户登录并显示出用户信息的方法
-
使用Eclipse开发Web项目(JSP)——简单登录、无sql
-
Java——实现简单的ATM银行管理系统(很简单,无GUI)
-
jsp+ajax实现无刷新,鼠标离开文本框即验证用户名
-
使用JSP实现简单的用户登录注册页面示例代码解析
-
JSP简单用户管理(无javabean)
-
jwt,spring security ,feign,zuul,eureka 前后端分离 整合 实现 简单 权限管理系统 与 用户认证的实现
-
Python实现简单的用户管理系统的步骤教程
-
Android布局管理器-使用TableLayout表格布局管理器实现简单的用户登录页面