最近发现一个问题,webwork在拦截器里用HttpServletRequest是获取不到cookie的,但是在action里却能够取到cookie。
情况是这样:
拦截器的代码:
public String intercept(ActionInvocation arg0){
String result = null;
HttpServletRequest request = ServletActionContext.getRequest();
Cookie cookies[] = request.getCookies();
Cookie sCookie = null;
String value = null;
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
sCookie = cookies[i];
if (sCookie.getName().equals("cookieName")) {
value = sCookie.getValue();
}
}
before(arg0);
result = arg0.invoke();
after(arg0, result);
return result;
}
这里的value的结果是null。
但是在action里,用同样的代码是可以获取到值的:
public String execute(){
javax.servlet.http.Cookie cookies[] = request.getCookies();
javax.servlet.http.Cookie sCookie = null;
String value = null;
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
sCookie = cookies[i];
if (sCookie.getName().equals("cookieName")) {
value = sCookie.getValue();
}
}
}
return SUCCESS;
}
此时的value是可以取到值的。
设置cookie的代码:
Cookie cookie_name =new Cookie("cookieName",value);
cookie_name.setPath("/");
cookie_name.setMaxAge(365*24*60*60);
response.addCookie(cookie_name);
难道webwork拦截器里的request处理问题?或者我这种拦截器里获取cookie的写法有问题?
------解决方案--------------------
不懂,帮你搜了一下,希望有帮助。。
http://www.opensymphony.com/webwork/wikidocs/Cookie%20Interceptor.html
------解决方案--------------------
可以的我这边测试是可以的,是不是你配置的问题
1、STRTUS有自定义的COOKIE拦截器,你可以参考下它的实现
org.apache.struts2.interceptor.CookieInterceptor
2、以下是我的测试代码
- Java code
struts.xml配置文件<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <constant name="struts.devMode " value="true"></constant> <constant name="struts.objectFactory" value="org.apache.struts2.impl.StrutsObjectFactory"></constant> <package name="zhangyz" extends="struts-default" namespace="/zhangyz"> <interceptors> <interceptor name="testInterceptor" class="com.zhangyz.tools.TestInterceptor" /> <interceptor-stack name="testStack"> <interceptor-ref name="defaultStack" /> <interceptor-ref name="testInterceptor" /> </interceptor-stack> </interceptors> <action name="userBeanTest" class="com.zhangyz.tools.UserBeanTest" method="method1"> <interceptor-ref name="testStack"></interceptor-ref> <result name="success">/index.jsp</result> </action> </package></struts><!-----JAVA类如下-----!>package com.zhangyz.tools;import java.util.Arrays;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletResponse;import org.apache.log4j.Logger;import org.apache.struts2.ServletActionContext;public class UserBeanTest { private static Logger logger = Logger.getLogger(UserBeanTest.class); private Integer id = null; public String method1() { Cookie[] cookies = ServletActionContext.getRequest().getCookies(); if (cookies != null) { for (int i = 0; i < cookies.length; i++) { logger.info("ACTION println cookies name " + cookies[i].getName() + " value " + cookies[i].getValue()); } } HttpServletResponse response = ServletActionContext.getResponse(); response.addCookie(new Cookie("test", "add" + System.currentTimeMillis())); return "success"; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; }}package com.zhangyz.tools;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import org.apache.log4j.Logger;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;public class TestInterceptor implements Interceptor { private static Logger logger = Logger.getLogger(TestInterceptor.class); @Override public void destroy() { } @Override public void init() { } @Override public String intercept(ActionInvocation invocation) throws Exception { String result = null; HttpServletRequest request = ServletActionContext.getRequest(); Cookie cookies[] = request.getCookies(); Cookie sCookie = null; String value = null; if (cookies != null) { for (int i = 0; i < cookies.length; i++) { logger.info("INTERCEPTOR println cookie name " + cookies[i].getName() + " value " + cookies[i].getValue()); } } return invocation.invoke(); }}