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);}
    		}
    	}
    %>
    
    
  • 相关阅读:
    HTTP状态码
    HTTP详解教程 / HTTP 响应头信息 HTTP 响应头信息
    HTTP请求方法
    HTTP 消息结构
    HTTP 简介
    Session 工作原理
    CSS 布局
    css float 浮动
    CSS 布局
    css position定位
  • 原文地址:https://www.cnblogs.com/du1991/p/7112072.html
Copyright © 2011-2022 走看看