为啥我创建的Cookie老是有问题

来源:6-4 案例:Cookie在登录中的应用

qq_吊儿郎当_6

2017-03-29 16:51

<%@page import="java.net.CookieHandler"%>
<%@page import="org.omg.CORBA.RepositoryIdHelper"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page  import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>登录成功</h1>
<hr>
<br>
<br>
<br>
<br>
<%
           //判断是否选择了记住登录状态
            String[]  isUserCookie=request.getParamerterValues("isUserCookie");
            if( isUserCookie!=null&&isUserCookie.length>0)
            {
             String username=request.getParmarterValue("username");
             String passward=request.getParmarterValue("passward");
             Cookie usernamecookie=new Cookie("username",username);
             Cookie passwardcookie=new  Cookie("passward",passward);
             usernamecookie.setMaxage(864000);
             passwardcookie.setMaxage(864000);
             response.addCookie(usernamecookie);
             response.addCookie(passwardcookie);
            }
            else{
             
             Cookie[]  cookie=request.getCookie();
             if(cookie!=null&&cookie.length>0)
             {
               for(Cookie  c:cookie)
               {
                if(c.getName().equals("username")||c.getName()/equals("passward"))
                {
                 c.setMaxage(0);
                 response.addCookie(c);
                
                } 
               }
             }
          
            }
  
 

%>


<a href="users.jsp" target="_Blank">查看已经登录的用户信息</a>
</body>
</html>_


写回答 关注

1回答

  • 慕前端2053235
    2017-03-29 17:49:54

      if(c.getName().equals("username")||c.getName()/equals("passward"))   这一行是不小心输错了吧 

    c.getName()/equals("passward"))  方法调用改为“.”c.getName().equals("passward"))

JAVA遇见HTML——JSP篇

Java Web入门级教程JSP,带你轻松的学习JSP基础知识

248277 学习 · 3071 问题

查看课程

相似问题