Cookie是儲存在用戶端的一種資訊技術。
在上一頁中,我們學到了很多關於cookie的內容 如何建立cookie,如何刪除cookie,如何獲取cookie等
在這裡,我們將使用servlet cookie建立一個登入和登出範例。
在這個例子中,我們建立了3
個連結:登入,登出和使用者資料。使用者在未登入之前不能進入個人資料頁面。如果使用者已經登出,則需要再次登入才能存取個人資料。
在這個應用程式中,建立了以下檔案。
開啟Eclipse,建立一個動態Web專案:LoginWithCookies,其完整的專案結構如下圖所示 -
按以下順序,分別編寫每個檔案的原始碼。
檔案:index.html -
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>使用Cookie登入應用程式</title>
</head>
<body style="text-algin: center;">
<h2>歡迎使用Cookie登入應用程式</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="name"> 密碼:<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.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
request.getRequestDispatcher("link.html").include(request, response);
String name = request.getParameter("name");
String password = request.getParameter("password");
// 使用者名和密碼分別為:admin , 123456
if (name.equals("admin") && password.equals("123456")) {
out.print("您已成功登入系統!");
out.print("<br>歡迎您, " + name);
Cookie ck = new Cookie("name", name);
response.addCookie(ck);
} else {
out.print("<font style='color:red;'>使用者名或密碼錯誤!</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.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
request.getRequestDispatcher("link.html").include(request, response);
Cookie ck = new Cookie("name", "");
ck.setMaxAge(0);
response.addCookie(ck);
out.print("您已成功登出!");
}
}
檔案:ProfileServlet.java -
package com.yiibai;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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);
Cookie ck[] = request.getCookies();
if (ck != null) {
String name = ck[0].getValue();
if (!name.equals("") || name != null) {
out.print("<b>歡迎您來到個人資訊中心</b>");
out.print("<br>您好, " + name);
}
} else {
out.print("<font style='color:red;'>請先登入!</font>");
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>LoginWithCookies</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- 登入Servlet -->
<servlet>
<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>
<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>
<!-- 使用者資料Servlet -->
<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>
</web-app>
在編寫所有上面檔案程式碼之後,部署這個Web應用程式,開啟瀏覽器存取URL: http://localhost:8080/LoginWithCookies/ ,如果程式碼沒有錯誤,應該會看到以下結果 -
點選「登入」連結,輸入使用者名和密碼:admin 和 123456 ,應該會看到以下結果 -
提交登入成功後,看到以下結果 -
檢視個人資訊 -
其它如登出,登入密碼錯誤請自己嘗試一下。