开发者社区 问答 正文

为什么我的读取出来的cookies是这个样子?:报错

而不是"A12345AD",,,,,,,,,,"xiaoMing"?

展开
收起
kun坤 2020-06-07 14:43:27 499 分享 版权
1 条回答
写回答
取消 提交回答
  • 把你的代码贴出来瞧瞧######你好红薯大哥,代码我贴出来了######

    package com.hxy.service;

    import java.io.IOException;

    import javax.mail.Session; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;

    import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.dao.EmptyResultDataAccessException;

    import com.hxy.dao.User_CookieDao; import com.hxy.entity.Cookie_UserDemoEntity;

    /** * * @author HuangXinyu * * @version 1.0 2012-11-20 下午03:54:24 */ public class LoginService extends HttpServlet {

    ApplicationContext act = new ClassPathXmlApplicationContext("bean.xml");
    
    User_CookieDao user_CookieDao = (User_CookieDao) act
             .getBean("user_CookieDao");
    
    private static final long serialVersionUID = 8956174527054306717L;
    
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
    		throws ServletException, IOException {
    	response.setContentType("text/html;charset=utf-8");
    	request.setCharacterEncoding("utf-8");
    	//HttpSession session = request.getSession();
    	String username = request.getParameter("username");
    	String pwd = request.getParameter("pwd");
    	String autologin = request.getParameter("autologin");
    	
    	if (username != null && !username.equals("") && pwd != null && !pwd.equals("")) {
    		Cookie_UserDemoEntity userDemoEntity = user_CookieDao.findUser(
    				username, pwd);
    		if (userDemoEntity != null) {
    			if(autologin!=null && !autologin.equals("")){
    				Cookie ckUsername = new Cookie("A12345AD", username);
    				ckUsername.setMaxAge(60 * 60 * 24 * 14);   //设置Cookie有效期为14天 
    

    // String ss = session.getId(); // 取得当前的session id // System.out.println(ss); Cookie[] coolies = request.getCookies(); for(Cookie cookie : coolies){ cookie.getName(); cookie.getValue(); System.out.println(cookie.getName()+",,,,,,,,,,"+cookie.getValue()); } } response.sendRedirect("index.jsp"); // } else if (cs != null) { // // for (int i = 0; i < cs.length; i++) { // Cookie c = cs[i]; // // String cname = c.getName();// 获取的是cookie的名字 // String cvalue = c.getValue();// 获取的是cookie的value值 // // System.out.println(cname + "\t" + cvalue); // }

    		} else {
    			request.setAttribute("loginErro", "用户名密码错误!");
    			request.setAttribute("login", "login/");
    			request.getRequestDispatcher("/login/login.jsp").forward(request,
    					response);
    			return;
    		}
    	}else{
    		request.setAttribute("loginErro", "用户名密码错误!");
    		request.getRequestDispatcher("/login/login.jsp").forward(request,
    				response);
    		return;
    	}
    }
    

    }

    ######你的代码得到的就应该是显示出来的结果啊,没错哦######回复 @红薯 : 红薯大哥,我加了个response.addCookie(co);就好了居然,好奇怪######回复 @红薯 : 嗯,好的,谢谢了######回复 @xinyu_huang : 那要看 xiaoming 这个值存在哪里,有没有在 cookie 里,你要先确定这个问题######回复 @红薯 : 哦,我怎么样才可以显示xiaoming这个值?######回复 @xinyu_huang : 你这段代码是取所有 cookie 并显示,是没错的

    2020-06-07 14:43:33
    赞同 展开评论
问答地址: