为了账号安全,请及时绑定邮箱和手机立即绑定

好像每次使用request.getParameter都没效果,这次代码在网页中没保存cookies!

login.jsp:

  <body>

  <%

  String USERNAME = "";

  String PASSWORD = "";

  Cookie[] cookies = request.getCookies();

if(cookies != null && cookies.length > 0){

for(Cookie c : cookies){

if(c.getName().equals("userName")){

USERNAME = c.getValue();

}

if(c.getName().equals("passWord")){

PASSWORD = c.getValue();

}

}

}

  %>

    <div class="classA">用户登录界面</div>

    <form action="dologin.jsp" method="post">

    <div>用户名:</div><input type="text" name="username" value=<%=USERNAME %>><br/>

    <div>密码:</div><input type="password" name="password"><br/>

    <div><input type="checkbox" name="isUseCookie" checked="checked" value=<%=PASSWORD %>>十天内记住我的登录状态</div>

    <input type="submit" value="提交">

    <input type="reset" value="重置">    

  </body>

dologin.jsp:

  <body>

<div>登录成功欢迎</div>

<%

//判断用户是否选择了登录状态

String[] useCookies = request.getParameterValues("isUseCookie");

if(useCookies != null && useCookies.length > 0){

//把用户名和密码保存在Cookie对象里面

String userName = request.getParameter("username");

String passWord = request.getParameter("password");

Cookie userNameCookie = new Cookie("userName","userName");

Cookie passWordCookie = new Cookie("passWord","passWord");

userNameCookie.setMaxAge(60*60*24*10);//设置最大生存期限为10天

passWordCookie.setMaxAge(60*60*24*10);

response.addCookie(userNameCookie);

response.addCookie(passWordCookie);

} else{

Cookie[] cookies = request.getCookies();

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);//重新保存cookie

}

}

}

}

%>

<a href = "user.jsp" target="_blank">用户信息</a>

  </body>

user.jsp:

  <body>

  <%

  String USERNAME = "";

  String PASSWORD = "";

  Cookie[] cookies = request.getCookies();

if(cookies != null && cookies.length > 0){

for(Cookie c : cookies){

if(c.getName().equals("userName")){

USERNAME = c.getValue();

}

if(c.getName().equals("passWord")){

PASSWORD = c.getValue();

}

}

}

  %>

    <div>用户名:<%=USERNAME %></div><br/>

    <div>密码:<%=PASSWORD %></div><br/>

  </body>

哪里有问题啊?

http://img1.sycdn.imooc.com//591d084f000101e201810074.jpg

http://img1.sycdn.imooc.com//591d085000018a5602370164.jpg


正在回答

1 回答

Cookie userNameCookie = new Cookie("userName",userName);

Cookie passWordCookie = new Cookie("passWord",passWord);

第二个参数不加引号


1 回复 有任何疑惑可以回复我~
#1

慕粉4193678 提问者

厉害了,word哥,栽在键值对上,非常感谢!
2017-05-19 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

好像每次使用request.getParameter都没效果,这次代码在网页中没保存cookies!

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信