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 ,应该会看到以下结果 -
提交登录成功后,看到以下结果 -
查看个人信息 -
其它如注销,登录密码错误请自己尝试一下。