1.建立web工程
2.創建存放servlet的包
3右鍵包,新建servlet,路徑將前面的servlet去掉,只需要doPost和doGet方法
編寫servlet
CookieServlet.java代碼如下:
package test1029; 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 CookieServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); String uname=request.getParameter("uname"); String password=request.getParameter("password"); String ck=request.getParameter("ck"); //被選中的狀態是on 沒有被選中的狀態下是null if("on".equals(ck)){ //構造Cookie對象 //添加到Cookie中 Cookie c=new Cookie("users", uname+"-"+password); //設置過期時間 c.setMaxAge(600); //存儲 response.addCookie(c); } } }
配置文件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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>test1029</display-name> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>CookieServlet</servlet-name> <servlet-class>test1029.CookieServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CookieServlet</servlet-name> <url-pattern>/CookieServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
index.jsp代碼:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <% //el表達式 String names=""; String pwd=""; //取出Cookie Cookie [] c=request.getCookies(); for(int i=0;i<c.length;i++){ if(c[i].getName().equals("users")){ //存着數據(用戶名+密碼) names=c[i].getValue().split("-")[0]; pwd=c[i].getValue().split("-")[1]; //再一次的存起來(備用) request.setAttribute("xingming",names); request.setAttribute("mima", pwd); } } %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="CookieServlet" method="post"> 用戶名:<input type="text" name="uname" id="uname" value="${xingming}"/><br> 密碼:<input type="password" name="password" id="password" value="${mima }"/><br> <input type="checkbox" name="ck">記住用戶名和密碼<br> <input type="submit" value="登錄"> </form> </body> </html>
效果圖:
1、項目運行之后,先進入登錄界面,填寫用戶名和密碼,效果如下圖
2.登錄后:
3.重新訪問登錄頁面,效果如下: