jsp使用cookie实现记住密码的功能,代码怎么写的。这个问题困扰我很长时间了,请高手帮帮我。在登陆界面,如果用户点击记住密码复选框,在下次打开登录界面时候用户名和密码不用输入,已经在文本框中!请高手一定要帮帮我,我真的很急。先谢谢大家了!
解决方案 »
- 这道题求教啊,谢谢啦
- Exception in thread "main" java.lang.NoSuchMethodError ?
- java基础教程
- 请问 数据库表id 的high/low算法是什么 hibernate中也提到这样的概念
- Java如何用同步块来保护静态变量?
- 那位兄弟能提供几本免费下载的关于GUI设计的书啊?感谢!!!
- 请问java中API中有二叉树的类么?
- mysql的驱动如何安装?我把com考到java的文件夹下,在CLASSPATH里添加了路径
- 请问:怎么样把sun的doc集成到Jbuilder里面去呀。。Jbuilder里面的帮助一点也好呀。。连个样列都没有?
- String 字符串
- java 向下转型疑问!
- 代码中红色标定部分有什么区别?
换一种思路,按你所说的点击记住密码复选框,可以把数据提交到数据库中
这样也安全,可能会烦琐点。
但这样可行。
取数据。其中 request 是 HttpServletRequest 对象Cookie[] cookies = request.getCookies();
for (int i = 0; cookies != null && i < cookies.length; i++) {
System.out.println(cookies[i].getName() + "=" + cookies[i].getValue());
}
存数据。其中 response 是 HttpServletResponse 对象Cookie cookie = new Cookie("testkey","testvalue");
cookie.setMaxAge(3600);// 有效时间,单位是秒
cookie.setPath("abc");// 路径,一定要设置,不然可能产生多个COOKIE文件
response.addCookie(cookie);
// 然后返回一个 JSP 页面。一定要返回,不然不会保存
[/codet]删除
[code=Java]
Cookie cookie = new Cookie("testkey", null);
cookie.setMaxAge(0);
cookie.setPath("...");
response.addCookie(cookie);
javascript//写cookies函数
//两个参数,一个是cookie的名称,一个是值
function SetCookie(name,value)
{
var Days = 30; //此 cookie 将被保存 30 天
var exp = new Date(); //new Date("December 31, 9998");
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}//读取cookies函数
function getCookie(name)
{
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;
}//删除cookie
function delCookie(name)
{
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
Cookie user = new Cookie("user",name+"-"+passward);
加到cookie中,然后在访问此登录页面时,就先调用
Cookie[] cookies=request.getCookies(); 看cookie中是否有user这个值,如果有就取得用户名、密码。
代码示例:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String name="nihao";
String passward="nihao";
try{
Cookie user = new Cookie("user",name+"-"+passward);
user.setMaxAge(60);
response.addCookie(user);
Cookie[] cookies=request.getCookies();
if(cookies!=null){
for(int i=0;i<cookies.length;i++){
if(cookies[i].getName().equals("user")){
name=cookies[i].getValue().split("-")[0];
passward=cookies[i].getValue().split("-")[1];
request.setAttribute("name",name);
request.setAttribute("pass",passward);
}
}
}
}catch(Exception e){
e.printStackTrace();
}%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--></head><body>
<form action="test10.jsp" method="post">
<input type="text" value="${name}" name=name /></br>
<input type="text" value="${pass}" name=pass />
<input type="submit" value="提交">
</form>
</body>
</html>
注本人也不是13楼
每天回帖即可获得10分可用分!小技巧:教您如何更快获得可用分
注本人也不是13楼