1. 程式人生 > >JSP簡單使用者管理(無javabean)

JSP簡單使用者管理(無javabean)

文章目錄

寫在前面

此文只是一個練習的過程,沒有用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>