DWR无法取得cookie问题

JollyRoger 发布于 2012/03/10 13:51
阅读 402
收藏 0

我因为用了拦截器来拦截所有DWR的操作来判断session为空验证是否登录超时,所以在登入时用 的JQuery来post到servlet进行登录。这样保存的cookie

Cookie cookieUser = new Cookie("cookieUser", document.selectSingleNode("//root").asXML());
					response.addCookie(cookieUser); 

然后再用DWR的时候用以下方式却无法取得保存的cookie,而session却可以取得,请问是什么原因呢?

session保存:

 

HttpSession session = request.getSession();
					session.setAttribute("user_id", d.selectSingleNode("//user_id").getText());
					session.setAttribute("account", account);

 session取得:

HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
		HttpSession session = req.getSession();
		return session.getAttribute(name).toString();

cookie保存:

Document document = DocumentHelper.createDocument();
					Element rootElement = document.addElement("root");
					Element accountElement = rootElement.addElement("account");
					accountElement.setText(account);
					Element pwdElement = rootElement.addElement("pwd");
					pwdElement.setText(pwd);	
					Element useridElement = rootElement.addElement("user_id");
					useridElement.setText(d.selectSingleNode("//user_id").getText());						
					//寫Cookie
					Cookie cookieUser = new Cookie("cookieUser", document.selectSingleNode("//root").asXML());
					response.addCookie(cookieUser); 

cookie取得:

HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
		Cookie[] cookies = req.getCookies();
		String mycookie = "";
		try {
			if (cookies != null) {
				for (int i = 0; i < cookies.length; i++) {
					Cookie c = cookies[i];
					if (c.getName().equalsIgnoreCase("cookieUser")) {
						Document d = DocumentHelper.parseText(c.getValue().toString());
						mycookie = d.selectSingleNode("//"+name).getText();
						break;
					}
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}

这样取的话cookie的name就不是我保存的那个了。请问大家是怎么解决的?

加载中
0
郝竹林
郝竹林
我也遇到同样问题 我在dwr的java代码中给cookie赋值了  但是在Servlet中去取这个cookie 竟然没有值 是空
返回顶部
顶部