转自:http://www.cnblogs.com/thrilling/p/4924077.html
1.建立web工程
2.创建存放servlet的包
3右键包,新建servlet,路径将前面的servlet去掉,只需要doPost和doGet方法
编写servlet
CookieServlet.java代码如下:
1 package test1029; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.Cookie; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 public class CookieServlet extends HttpServlet { 13 14 public void doGet(HttpServletRequest request, HttpServletResponse response) 15 throws ServletException, IOException { 16 this.doPost(request, response); 17 } 18 19 public void doPost(HttpServletRequest request, HttpServletResponse response) 20 throws ServletException, IOException { 21 response.setContentType("text/html"); 22 23 String uname=request.getParameter("uname"); 24 String password=request.getParameter("password"); 25 String ck=request.getParameter("ck"); 26 27 //被选中的状态是on 没有被选中的状态下是null 28 if("on".equals(ck)){ 29 //构造Cookie对象 30 //添加到Cookie中 31 Cookie c=new Cookie("users", uname+"-"+password); 32 33 //设置过期时间 34 c.setMaxAge(600); 35 36 //存储 37 response.addCookie(c); 38 } 39 } 40 }
配置文件web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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"> 3 <display-name>test1029</display-name> 4 <servlet> 5 <description>This is the description of my J2EE component</description> 6 <display-name>This is the display name of my J2EE component</display-name> 7 <servlet-name>CookieServlet</servlet-name> 8 <servlet-class>test1029.CookieServlet</servlet-class> 9 </servlet> 10 11 <servlet-mapping> 12 <servlet-name>CookieServlet</servlet-name> 13 <url-pattern>/CookieServlet</url-pattern> 14 </servlet-mapping> 15 <welcome-file-list> 16 <welcome-file>index.html</welcome-file> 17 <welcome-file>index.htm</welcome-file> 18 <welcome-file>index.jsp</welcome-file> 19 <welcome-file>default.html</welcome-file> 20 <welcome-file>default.htm</welcome-file> 21 <welcome-file>default.jsp</welcome-file> 22 </welcome-file-list> 23 </web-app>
index.jsp代码:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <% 8 9 //el表达式 10 String names=""; 11 String pwd=""; 12 //取出Cookie 13 Cookie [] c=request.getCookies(); 14 for(int i=0;i<c.length;i++){ 15 if(c[i].getName().equals("users")){ 16 //存着数据(用户名+密码) 17 names=c[i].getValue().split("-")[0]; 18 pwd=c[i].getValue().split("-")[1]; 19 20 //再一次的存起来(备用) 21 request.setAttribute("xingming",names); 22 request.setAttribute("mima", pwd); 23 } 24 } 25 26 %> 27 28 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 29 <html> 30 <head> 31 <base href="<%=basePath%>"> 32 33 <title>My JSP 'index.jsp' starting page</title> 34 <meta http-equiv="pragma" content="no-cache"> 35 <meta http-equiv="cache-control" content="no-cache"> 36 <meta http-equiv="expires" content="0"> 37 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 38 <meta http-equiv="description" content="This is my page"> 39 <!-- 40 <link rel="stylesheet" type="text/css" href="styles.css"> 41 --> 42 </head> 43 44 <body> 45 <form action="CookieServlet" method="post"> 46 用户名:<input type="text" name="uname" id="uname" value="${xingming}"/><br> 47 密码:<input type="password" name="password" id="password" value="${mima }"/><br> 48 <input type="checkbox" name="ck">记住用户名和密码<br> 49 <input type="submit" value="登录"> 50 </form> 51 </body> 52 </html>
效果图:
1、项目运行之后,先进入登录界面,填写用户名和密码,效果如下图
2.登录后:
3.重新访问登录页面,效果如下:
<?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>