之前说到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函数有啥用??