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

login.jsp


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







登录页面






	

登录


用户名(只能由字母组成,3~12位)
密码(6~12位)
两周免登陆

<% //根据传回来的值显示错误信息 String index = request.getParameter("index"); if (index != null) { if (index.equals("1")) { out.print("

用户名或密码为空

"); } else if (index.equals("2")) { out.print("

用户名或密码不符合规则

"); } else { out.print("

用户名或密码错误

"); } } %>

login-action.jsp

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






逻辑判断






	<%
		//接受用户名密码
		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");
		}
	%>

	

index.jsp

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







欢迎页面




	

欢迎页面


<% //获取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("

Welcome!" + username + "!

"); %> <%!// 创建方法,用于查找指定名称的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 list中 public List readUser() { List list = new ArrayList(); 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; }%>
<% List list = readUser(); for (User u : list) { %> <% } %>
id username password
<%=u.getId()%> <%=u.getUname()%> <%=u.getUpasswd()%>

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
				+ "]";
	}
}

效果

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

JavaWeb-通过表格显示数据库的信息(jsp+mysql)_第1张图片

你可能感兴趣的:(JavaWeb)