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
+ "]";
}
}
效果
登錄之後,如果正確則會展示表.