(1)
(2)Session----Coolie
(3)
(4)
cookie保存
index.jsp
<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘index.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<h1>用户登录</h1>
<hr>
<%
request.setCharacterEncoding("utf-8");
String username = "";
String password = "";
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length > 0) {
for (Cookie c : cookies) {
if (c.getName().equals("username")) {//(名称)
username=URLDecoder.decode(c.getValue(),"utf-8");
}
if( c.getName().equals("password")){
password=URLDecoder.decode(c.getValue(),"utf-8");
}
}
}
%>
<form action="dologin.jsp" method="post" name="loginForm">
<table>
<tr><td>用户名:</td><td><input type="text" name="username" value="<%=username %>" /> </td></tr>
<tr><td>密码:</td><td><input type="password" name="password" value="<%=password %>" /></td></tr>
<tr><td colspan="2" ><input type="checkbox" name="isUseCookie" checked="checkd" />十天内记住登陆状态</td></tr>
<tr><td colspan="2" aligin="center"><input type="submit" value="登录"></td></tr>
</table>
</form>
</body>
</html>
dologin.jsp
<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘dologin.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<h1>登陆成功</h1> <br><br><br><br>
<%
request.setCharacterEncoding("utf-8");//防止中文乱码
//首先判断用户是否选择了记录登陆状态
String[] isUseCookie=request.getParameterValues("isUseCookie");//返回包含参数name的所有值的数组
if(isUseCookie!=null&&isUseCookie.length>0){
//吧用户名和密码保存在Cookie对象里面
String username= URLEncoder.encode(request.getParameter("username"),"utf-8");//返回name指定函数的参数值
//URLEncoder.encode(s, enc)防止中文乱码。。短码
String password= URLEncoder.encode(request.getParameter("password"),"utf-8");
Cookie usernameCookie=new Cookie("username",username);//创建cookie对象
Cookie passwordCookie=new Cookie("password",password);
usernameCookie.setMaxAge(8640000);//设置最大生存期限为10天(有效期)
passwordCookie.setMaxAge(8640000);
response.addCookie(usernameCookie);//加入cookie对象
response.addCookie(passwordCookie);
}else{
Cookie[] cookies=request.getCookies();//读取cookie对象
if(cookies!=null&&cookies.length>0){
for(Cookie c:cookies){
if(c.getName().equals("username")||c.getName().equals("password")){//(名称)
c.setMaxAge(0);//设置cookie失效
response.addCookie(c);//重新保存
}
}
}
}
%>
<a href="users.jsp" target="_blank">查看用户信息</a>
</body>
</html>
users.jsp
<%@ page language="java" import="java.util.*,java.net.*"
contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘users.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<h1>用户信息</h1>
<br>
<br>
<br>
<br>
<%
request.setCharacterEncoding("utf-8");
String username = "";
String password = "";
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length > 0) {
for (Cookie c : cookies) {
if (c.getName().equals("username")) {//(名称)
//解码用URLDecoder.decode(s, enc)
username=URLDecoder.decode(c.getValue(),"utf-8");
}
if( c.getName().equals("password")){
password=URLDecoder.decode(c.getValue(),"utf-8");
}
}
}
%>
用户名:<%=username %>
<br> 密码:<%=password %>
<br>
</body>
</html>
(5)Session-Cookie
时间: 2024-11-05 14:38:16