Cookie是一种存储在客户端信息的技术。在上一页中,我们已经了解了很多有关Cookie的技术,例如:如何创建cookie,如何删除cookie,如何获取cookie等。
在这个示例中,我们将使用servlet cookie创建一个登录和注销应用的示例。在此示例中将创建3个链接:登录,注销和配置文件。 用户必须先登录才能进入个人资料页面。如果用户已注销,则需要再次登录才能访问个人资料页面。
启动Eclipse,创建一个动态Web应用项目:LoginServletApp,完整的目录结构如下:
在此应用程序中,需要创建以下几个文件:
- index.html
- link.html
- login.html
- LoginServlet.java
- LogoutServlet.java
- ProfileServlet.java
- web.xml
文件:index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Servlet登录示例</title>
</head>
<body>
<h1>欢迎使用Cookie登录应用</h1>
<a href="login.html">登录</a>|
<a href="LogoutServlet">注销</a>|
<a href="ProfileServlet">个人信息</a>
</body>
</html>
文件:link.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="login.html">登录</a>|
<a href="LogoutServlet">注销</a>|
<a href="ProfileServlet">个人信息</a>
<hr>
</body>
</html>
文件:login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="LoginServlet" method="post">
用户名:<input type="text" name="name"><br> 密码:<input
type="password" name="password"><br> <input
type="submit" value="登录">
</form>
</body>
</html>
文件:LoginServlet.java
package com.xntutor.servlet;
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.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
request.getRequestDispatcher("link.html").include(request, response);
String name = request.getParameter("name");
String password = request.getParameter("password");
if (password.equals("admin123")) {
out.print("登录系统成功!");
out.print("<br>您好, " + name);
Cookie ck = new Cookie("name", name);
response.addCookie(ck);
} else {
out.print("抱歉,用户名或密码错误!");
request.getRequestDispatcher("login.html").include(request, response);
}
out.close();
}
}
文件:LogoutServlet.java
package com.xntutor.servlet;
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.setContentType("text/html;charset=utf-8");
response.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.xntutor.servlet;
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.setContentType("text/html;charset=utf-8");
response.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("请先登录~");
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://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>LoginServletApp</display-name>
<servlet>
<description></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.xntutor.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>ProfileServlet</display-name>
<servlet-name>ProfileServlet</servlet-name>
<servlet-class>com.xntutor.servlet.ProfileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProfileServlet</servlet-name>
<url-pattern>/ProfileServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>LogoutServlet</display-name>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>com.xntutor.servlet.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/LogoutServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
启动Tomcat服务器,打开浏览器访问下面URL: