JavaWeb-通过表格显示数据库的信息(jsp+mysql)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41690324/article/details/84133912

login.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>登录页面</title>



</head>

<body>
	<h2>登录</h2>
	<br>
	<form action="login-action.jsp" method="post">
		用户名<input name="username">(只能由字母组成,3~12位)<br> 密码<input
			type="password" name="password">(6~12位)<br> <input
			type="checkbox" name="keep"> 两周免登陆<br> <input
			type="submit" value="登录">
	</form>
	<hr>
	<%
		//根据传回来的值显示错误信息
		String index = request.getParameter("index");
		if (index != null) {
			if (index.equals("1")) {
				out.print("<h1>用户名或密码为空</h1>");
			} else if (index.equals("2")) {
				out.print("<h1>用户名或密码不符合规则</h1>");
			} else {
				out.print("<h1>用户名或密码错误</h1>");
			}
		}
	%>
</body>
</html>

login-action.jsp

<%@ page language="java" import="java.util.*,java.sql.*"
	pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>逻辑判断</title>



</head>

<body>
	<%
		//接受用户名密码
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		if (!username.equals("") || !password.equals("")) {//如果用户名密码不为空
			if (username.matches("[a-zA-Z]{3,12}")
					&& password.matches("[a-zA-Z0-9]{6,12}")) {//如果符合规则
				try {
					//连接数据库,访问数据,查询用户名密码是否正确
					//1.加载驱动
					Class.forName("com.mysql.jdbc.Driver");
					//2.获得数据库连接---创建路
					String url = "jdbc:mysql://localhost:3306/mybase";
					Connection con = DriverManager.getConnection(url,
							"root", "root");
					//3.获得语句执行平台,通过数据库连接对象获取到SQL语句的执行者对象---创建人
					Statement stat = con.createStatement();
					//4.调用执行者对象,执行sql语句获取结果集---创建桶
					String sql = "select * from users where username='"
							+ username + "' and password='" + password
							+ "'";
					ResultSet rs = stat.executeQuery(sql);
					if (rs.next()) {//判断用户名密码是否正确
						if (request.getParameter("keep") != null) {//如果勾选复选框则创建Cookie,令用户两周内不在登录
							//Cookie
							Cookie name = new Cookie("cname", username);
							Cookie passwd = new Cookie("cpasswd", password);
							name.setMaxAge(60 * 60 * 24 * 7 * 2);
							passwd.setMaxAge(60 * 60 * 24 * 7 * 2);
							response.addCookie(name);
							response.addCookie(passwd);
						}
						//为了保证安全性,以session方式传递这两个值
						session.setAttribute("sname", username);
						session.setAttribute("spasswd", password);
						//跳转到欢迎页面
						response.sendRedirect("index.jsp");
					} else {
						//错误跳转,用户名密码有一项不正确就跳转到登录页面,并返回错误信息
						response.sendRedirect("login.jsp?index=3");
					}
				} catch (Exception e) {
					out.print(e.toString());
				}
			} else {
				//错误跳转,用户名密码有一项不符合业务逻辑就跳转到登录页面,并返回错误信息
				response.sendRedirect("login.jsp?index=2");
			}
		} else {
			//错误跳转,用户名密码有一项为空就跳转到登录页面,并返回错误信息
			response.sendRedirect("login.jsp?index=1");
		}
	%>

	<br>
</body>
</html>

index.jsp

<%@ page language="java" import="java.util.*,java.sql.*,com.entity.User"
	pageEncoding="UTF-8"%>

<!-- 需导入sql包,user实体包 -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>欢迎页面</title>

</head>

<body>
	<h2>欢迎页面</h2>
	<br>
	<%
		//获取session中信息
		String username = (String) session.getAttribute("sname");
		String password = (String) session.getAttribute("spasswd");
		if (username == null) {//判断直接访问欢迎页面的用户是否合法
			String cookiename = "";
			String cookiepasswd = "";
			Cookie[] cookies = null;
			cookies = request.getCookies();
			if (cookies.length > 1) {
				cookiename = getCookieByName(cookies, "cname").getValue();
				cookiepasswd = getCookieByName(cookies, "cpasswd")
						.getValue();
				if (!cookiename.equals("") && !cookiepasswd.equals("")) {//获取用户名密码,并在action页面验证	
					response.sendRedirect("login-action.jsp?username="
							+ cookiename + "&password=" + cookiepasswd);
					return;
				}
			}
			//跳转登录
			response.sendRedirect("login.jsp");
		}
		//显示欢迎
		out.print("<h1>Welcome!" + username + "!</h1>");
	%>


	<%!// 创建方法,用于查找指定名称的cookie
	public static Cookie getCookieByName(Cookie[] cs, String name) {
		if (cs == null || cs.length == 0) {
			return null;
		}
		for (Cookie c : cs) {
			if (name.equals(c.getName())) {
				return c;
			}
		}
		return null;
	}%>


	<%!//读取数据库,存到List<User> list中
	public List<User> readUser() {
		List<User> list = new ArrayList<User>();
		Connection con = null;
		ResultSet rs = null;
		try {//连接数据库的操作
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/mybase";
			con = DriverManager.getConnection(url, "root", "root");
			Statement stat = con.createStatement();
			String sql = "select * from users ";
			rs = stat.executeQuery(sql);
			while (rs.next()) {
				int id = rs.getInt("id");
				String myusername = rs.getString("username");
				String mypassword = rs.getString("password");
				User u = new User(id, myusername, mypassword);
				list.add(u);
			}
		} catch (Exception e) {
			e.toString();
		}

		try {//关闭连接
			if (rs != null) {
				rs.close();
			}
			if (con != null) {
				con.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return list;
	}%><hr>
	<!-- 创建表格 -->
	<table border="1">

		<tr>
			<th>id</th>
			<th>username</th>
			<th>password</th>
		</tr>
		<%
			List<User> list = readUser();

			for (User u : list) {
		%><tr>
			<td><%=u.getId()%></td>
			<td><%=u.getUname()%></td>
			<td><%=u.getUpasswd()%></td>
		</tr>
		<%
			}
		%>

	</table>

</body>
</html>

User.java

package com.entity;

//实体类
public class User {
	private int id;
	private String uname;
	private String upasswd;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public String getUpasswd() {
		return upasswd;
	}

	public void setUpasswd(String upasswd) {
		this.upasswd = upasswd;
	}

	public User(int id, String uname, String upasswd) {
		super();
		this.id = id;
		this.uname = uname;
		this.upasswd = upasswd;
	}

	public User() {
		super();
	}

	@Override
	public String toString() {
		return "user [id=" + id + ", uname=" + uname + ", upasswd=" + upasswd
				+ "]";
	}
}

效果

登录之后,如果正确则会展示表.

猜你喜欢

转载自blog.csdn.net/qq_41690324/article/details/84133912
今日推荐