Servlet登錄註銷Cookies實例
Cookie是存儲在客戶端的一種信息技術。
在上一頁中,我們學到了很多關於cookie的內容 如何創建cookie,如何刪除cookie,如何獲取cookie等
在這裏,我們將使用servlet cookie創建一個登錄和註銷示例。
在這個例子中,我們創建了3
個鏈接:登錄,註銷和用戶資料。用戶在未登錄之前不能進入個人資料頁面。如果用戶已經註銷,則需要再次登錄才能訪問個人資料。
在這個應用程序中,創建了以下文件。
- index.html - Web應用程序首頁。
- link.html - 鏈接頁面。
- login.html - 登錄頁面。
- LoginServlet.java - 登錄Servlet處理。
- LogoutServlet.java - 註銷Servlet處理。
- ProfileServlet.java - 用戶個人資料Servlet。
- web.xml - Servlet配置文件。
打開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 ,應該會看到以下結果 -
提交登錄成功後,看到以下結果 -
查看個人信息 -
其它如註銷,登錄密碼錯誤請自己嘗試一下。