JSP的饼干在登录中的使用
JSP的饼干在登录中的使用
一功能需求
实现记忆用户名和密码功能。
二代码
1、login.jsp
“% @”页面语言=“Java”导入=“Java”。乌提尔。*,Java。净。*“ContentType=”文本/html;charset=utf-8 " % % String path=request。GetContextPath();字符串基本路径=请求。getscheme()' ://'请求。getservername()“:”请求。getserverport()“路径”/;% !DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN ' HTML head base href=' %=base path % ' rel=' external no follow ' rel=' external no follow ' rel=' external no follow ' title my JSP ' index。“JSP”起始页/title meta http-equiv=' pragma ' content=' no-cache ' meta http-equiv=' cache-control ' content=' no-cache ' meta http-equiv=' expires ' content=' 0 ' meta-link rel='样式表type=' text/CSS ' href=' style。CSS ' rel='外部nofollow ' rel='外部nofollow ' rel='外部nofollow '-/头体h1用户登录/h1 HR %请求。setcharacter encoding(' utf-8 ');字符串用户名=' ';字符串密码=' ';cookie[]cookie=请求。getCooKies();如果(饼干!=nullcookies。长度0){对于(Cookie c : Cookies){ if(c . GetName().equals(' username '){ username=urldector。decode(c . getvalue(),' utf-8 ');} if(c.getName().等于(' password '){ password=urldector。decode(c . getvalue(),' utf-8 ');} } } % form name=' loginFOrm ' action=' dolo gin。JSP ' method=' post '表tr TD用户名:/TD tdinput type=' text ' name=' username ' value=' %=username % '//TD/tr TD密码:/TD TD TD输入类型=' password ' name=' password ' value=' %=password % '//TD/tr TD col span=' 2 '输入类型=' checkbox ' name=' is cookie ' checked=' checked '/十天内记住我的登录状态/TD/tr/tr TD栏跨度=' 2 ' align=' center '输入类型=' submit '值='登录/input type='reset' value='取消//td /tr /table /form /body/html2、dologin.jsp
“% @”页面语言=“Java”导入=“Java”。乌提尔。*,Java。净。*“ContentType=”文本/html;charset=utf-8 " % % String path=request。GetContextPath();字符串基本路径=请求。getscheme()' ://'请求。getservername()“:”请求。getserverport()“路径”/;% !DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN ' HTML head base href=' %=base path % ' rel=' external no follow ' rel=' external no follow ' rel=' external no follow ' title my JSP ' dolo gin。“JSP”起始页/title meta http-equiv=' pragma ' content=' no-cache ' meta http-equiv=' cache-control ' content=' no-cache ' meta http-equiv=' expires ' content=' 0 '-link rel='样式表type=' text/CSS ' href=' style。CSS ' rel='外部nofollow ' rel='外部nofollow ' rel='外部nofollow '-/头体h1登录成功/h1 HR br br %请求。setcharacter encoding(' utf-8 ');//首先判断用户是否选择了记住登录状态string[]is cookies=request。getparameter值(' isse cookie ');if(is cookies!=null susecookies。长度0){//把用户名和密码保存在饼干对象里面字符串用户名=urlencoder。编码(请求。GetParameter(' username ',' utf-8 ');//使用URLEncoder解决无法在饼干当中保存中文字符串问题字符串密码=urlencoder。编码(请求。GetParameter(' password ',' utf-8 ');Cookie usernameCookie=新Cookie('用户名,用户名);Cookie密码库克=新Cookie('密码,密码);usernamecookie。setmaxage(864000);passwordcook。setmaxage(864000);//设置最大生存期限为10天回应。添加cookie(username cookie);回应。addCookie(passwordCookie);} else { Cookie[]Cookies=请求。getCooKies();如果(饼干!=nullcookies。长度0){对于(Cookie c : Cookies){ if(c . GetName().equals('username')||c.getName().equals(' password '){ c . setmaxage(0);//设置饼干失效回应。addCooKie(c);//重新保存} } } % a href=' users。JSP ' rel='外部无跟随' target=' _ blank '查看用户信息/a /body /html3、users.jsp
“% @”页面语言=“Java”导入=“Java”。乌提尔。*,Java。净。*“ContentType=”文本/html;charset=utf-8 " % % String path=request。GetContextPath();字符串基本路径=请求。getscheme()' ://'请求。getservername()“:”请求。getserverport()“路径”/;% !DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN ' HTML head base href=' %=base path % ' rel=' external no follow ' rel=' external no follow ' rel=' external no follow ' title my JSP '用户。“JSP”起始页/title meta http-equiv=' pragma ' content=' no-cache ' meta http-equiv=' cache-control ' content=' no-cache ' meta http-equiv=' expires ' content=' 0 ' meta-link rel='样式表type=' text/CSS ' href=' style。CSS ' rel='外部nofollow ' rel='外部nofollow ' rel='外部nofollow '-/头体h1用户信息/h1 HR %请求。setcharacter encoding(' utf-8 ');字符串用户名=' ';字符串密码=' ';cookie[]cookie=请求。getCooKies();如果(饼干!=nullcookies。长度0){对于(Cookie c : Cookies){ if(c . GetName().equals(' username '){ username=urldector。decode(c . getvalue(),' utf-8 ');} if(c.getName().等于(' password '){ password=urldector。decode(c . getvalue(),' utf-8 ');} } } % BR BR BR用户名:%=用户名%br密码:%=密码% br/正文/html三测试
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
版权声明:JSP的饼干在登录中的使用是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。

















