zoukankan      html  css  js  c++  java
  • 17_7_3 Cookie 简述

    Cookie
    十天免登陆

    思路:
    1.login.jsp中request 携带的Cookie 找出 我们需要的 value;
    2.在 form 中把步骤一的value 赋给对应的 name
    3.创建新 Cookie
    4.response覆盖添加

    注意:
    1.login界面的赋值
    2.新增Cookie之后不要忘记 在response中添加

    login.jsp代码:

    <body>
    <%
    	String username_login="";
    	String password_login="";
    	Cookie[] co=request.getCookies();
    	if(co!=null){
    		for(Cookie c:co){
    			if(c.getName().equals("username")){
    				username_login=c.getValue();
    			}
    			if(c.getName().equals("password")){
    				password_login=c.getValue();
    			}
    		}
    	}
    %>
    	<form action="doLogin.jsp">
    		<table>
    			<tr>
    				<td>用户名:</td>
    				<td><input type="text" name="username" value="<%= username_login%>"></td>
    			</tr>
    			<tr>
    				<td>密码:</td>
    				<td><input type="password" name="password" value="<%= password_login%>"></td>
    			</tr>
    			<tr>
    				<td colspan="2"> <input type="checkbox" checked="checked" name="X" value="Y">十天内免登陆</td>
    			</tr>
    			<tr>
    				<td colspan="2"><input type="submit" value="提交"></td>
    			</tr>
    		</table>
    	</form>
    </body>
    
    

    doLogin.jsp 代码:

    <%
    	Cookie[] c=request.getCookies();
    	if(request.getParameter("X")!=null){
    		Cookie a=new Cookie("username",request.getParameter("username"));
    		Cookie b=new Cookie("password",request.getParameter("password"));
    		a.setMaxAge(600000);
    		b.setMaxAge(600000);
    		response.addCookie(a);
    		response.addCookie(b);
    	}
    	else{
    		for(Cookie co:c){
    			if(co.getName().equals("username")){
    				co.setMaxAge(0);
    				response.addCookie(co);}
    			if(co.getName().equals("password")){
    				co.setMaxAge(0);
    				response.addCookie(co);}
    		}
    	}
    %>
    
    
  • 相关阅读:
    BZOJ2095 [Poi2010]Bridges
    BZOJ3307 雨天的尾巴
    【CSP2020】 T3 动物园
    【CSP2020】 T1儒略日
    洛谷P3455 [POI2007]ZAP-Queries
    【黑科技学习】光速幂
    XJTUOJ #1023 JM的祖传零钱箱
    XJTUOJ #1168 zxh的后宫管理系统
    XJYUOJ #1053 nocriz与队列计算机
    XJTUOJ #1017 JM的完美集合
  • 原文地址:https://www.cnblogs.com/du1991/p/7112072.html
Copyright © 2011-2022 走看看