JavaBean初应用

版权声明:多多交流。 https://blog.csdn.net/qq_42776455/article/details/83447113

写在前面

将用户管理这个小项目:https://blog.csdn.net/qq_42776455/article/details/83446340 改写成结合JavaBean的。

ValueBean

com.um.ValueBean
MyEclipse自动生成getter,setter函数。右键source->getter,setter。

package com.um.ValueBean;

public class User {
	private String name;
	private String tel;
	private String sex;
	private String password;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

ToolsBean

com.um.ToolsBean,此处我的类命名有点不太合适。

package com.um.ToolsBean;

import com.um.ValueBean.User;
import java.util.*;


public class UserList {
	
	
	public static List<User> getUsers() {
		List<User> users = new ArrayList();
		for(int i=0;i<20;i++)
		{
			User user = new User();
			user.setName("zhang"+i);
			user.setTel("133333333"+i);
			user.setSex("男");
			user.setPassword("123456");
			users.add(user);
		}
		return  users;
	}
	
	public boolean checkLogin(List<User> users,String username,String password) {
		boolean login_flag = false;
		for (User u : users) {
			if (u.getPassword().equals(password)
					&& u.getName().equals(username))
				login_flag = true;
		}
		return login_flag;
	}
}

改写doLogin.jsp

仅改写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 + "/";
%>
<%@page import="com.um.ToolsBean.UserList" %>
<%@page import="com.um.ValueBean.*" %>

<%
	// 用户创建 
	List<User> users = UserList.getUsers();
%>	
<%
	// 登录验证
	request.setCharacterEncoding("utf-8");
	String username = request.getParameter("username");
	String password = request.getParameter("password");
	UserList userF = new UserList();
	boolean check_login = userF.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.getName()%></td>
			<td><%=u.getTel()%></td>
			<td><%=u.getSex()%></td>
			<td><%=u.getPassword()%></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>

猜你喜欢

转载自blog.csdn.net/qq_42776455/article/details/83447113