cookie应用案例

之前说到cooki保存在本地,那具体在哪呢?
在这里插入图片描述其中文件为二进制编码保存。
也可以通过浏览器设置中的cookie来看
在这里插入图片描述

3,通过cookie 自动记住用户名 功能

在这里插入图片描述

新建cookieDemo文件夹,然后新建login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%! 
         String uname;
    %>
    <%
        Cookie [] cookies = request.getCookies();
	    for(Cookie cookie :cookies){
	        if(cookie.getName().equals("uname")){
	        	uname = cookie.getValue();
	        }
	    }
    %>

   	<form action="check.jsp" method="post">
	用户名:<input type="text" name="uname" value="<%=(uname== null ?"":uname)%>"><br/>
	密码:<input type="password" name="upwd"><br/>
	<input type="submit" value="登录"><br/>
</form>
</body>
</html>

check.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
   <%
	  request.setCharacterEncoding("utf-8");
	  String name =request.getParameter("uname");
	  String pwd = request.getParameter("upwd");
	  //检查是否保留用户名等信息
	  //将用户名加入到cookie里
	  Cookie cookie = new Cookie("uname",name);
	  //不要中文,建议只放数字和英文,否则需要进行编码解码处理
	  response.addCookie(cookie);
	  response.sendRedirect("result.jsp");
	%>
</body>
</html>

result.jsp

原始生成,只是为了看跳转没有

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
自己实验发现删掉result.jsp也不影响功能,那send函数有啥用??

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值