在前面一節教學文章中,已經介紹了什麼是HttpSession
,如何儲存和從對談物件獲取資料。
我們可以繫結HttpSession
範例上的物件,並使用setAttribute
和getAttribute
方法獲取物件的資料資訊。
在這裡,將演示一個在不使用資料庫連線的情況下,建立一個真實的登入和登出應用程式。假設有一個使用者名是:admin
,它密碼是admin123
。
註: 存取這裡檢視:使用cookies登入和登出的應用程式範例 ,有助於學習本節文章的內容。
在這個例子中,我們建立了3
個連結頁面:登入,登出和個人資料。實現功能:使用者在登入之前不能進入個人資料頁面。如果使用者已經登出,則需要再次登入才能存取個人資料頁面。
在這個應用程式中,建立了以下幾個檔案。
開啟Eclipse,建立一個動態Web專案:SessionLoginout,其完整的目錄結構如下所示 -
以下是這個專案中的幾個主要的程式碼檔案。
檔案:index.html -
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Session登入登出範例</title>
</head>
<body style="text-algin: center;">
<h2>歡迎Session登入登出應用程式</h2>
<a href="login.html">登入</a>|
<a href="logout">登出</a>|
<a href="profile">個人資訊</a>
</body>
</html>
檔案:link.html -
<a href="login.html">登入</a>|
<a href="logout">登出</a>|
<a href="profile">個人資訊</a>
<hr>
檔案:login.html -
<div style="text-algin: center; padding-top:12px;">
<form action="login" method="post">
使用者名:<input type="text" name="username"> 密碼:<input type="password"
name="password"><input type="submit" value="登入">
</form>
</div>
</body>
</html>
檔案:LoginServlet.java -
package com.yiibai;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("UTF-8");
request.getRequestDispatcher("link.html").include(request, response);
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username == null) {
username = "";
}
if (password == null) {
password = "";
}
if (username.equals("admin") && password.equals("admin123")) {
out.print("Welcome, " + username);
HttpSession session = request.getSession();
session.setAttribute("username", username);
session.setAttribute("nickname", "蘇小牛");
session.setAttribute("age", "21");
} else {
out.print("<font style='color:rec;'>對不起,使用者名或密碼錯誤!</font>");
request.getRequestDispatcher("login.html").include(request, response);
}
out.close();
}
}
檔案:logoutServlet.java -
package com.yiibai;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LogoutServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("UTF-8");
request.getRequestDispatcher("link.html").include(request, response);
HttpSession session = request.getSession();
// 清除資料
session.invalidate();
out.print("您已成功登出退出系統!");
out.close();
}
}
檔案:ProfileServlet.java -
package com.yiibai;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ProfileServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
request.getRequestDispatcher("link.html").include(request, response);
HttpSession session = request.getSession(false);
if (session != null) {
String name = (String) session.getAttribute("username");
String nickname = (String) session.getAttribute("nickname");
String age = (String) session.getAttribute("age");
out.print("您好, " + name + " 歡迎您來到個人資訊中心!");
out.print("<hr/>個人資訊如下, 暱稱:" + nickname + " ,年齡:"+age);
} else {
out.print("請登入系統!");
request.getRequestDispatcher("login.html").include(request, response);
}
out.close();
}
}
檔案:web.xml -
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>SessionLoginout</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.yiibai.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ProfileServlet</servlet-name>
<servlet-class>com.yiibai.ProfileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProfileServlet</servlet-name>
<url-pattern>/profile</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>com.yiibai.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
</web-app>
在編寫上面程式碼後,部署此Web應用程式(在專案名稱上點選右鍵->」Run On Server…」),開啟瀏覽器存取URL: http://localhost:8080/SessionLoginout/ ,如果沒有錯誤,應該會看到以下結果 -
點選「登入」,如下介面 -
使用使用者名:admin
,密碼:admin123
,點選登入成功後,如下介面 -
使用者名或密碼錯誤時,顯示如下 -
點選「個人資訊」連結,顯示如下 -
點選「登出」連結,顯示如下 -