使用Jboss 5.1.0
在我的bean中,我想得到Response
个对象并使用它我想将一些值设置回cookie
。
下面是我在bean中的代码,
HttpServletResponse response = null;
response = (HttpServletResponse) PolicyContext.getContext("javax.servlet.http.HttpServletResponse");
//Line number 1230
HttpServletRequest req = null;
req = (HttpServletRequest) PolicyContext.getContext("javax.servlet.http.HttpServletRequest");
Cookie[] cookies = req.getCookies();
for(int i=0;i<cookies.length;i++)
{
if(cookies[i].getName().contains("myCookie"))
{
cookies[i].setValue("");
cookies[i].setPath("/");
cookies[i].setMaxAge(0);
response.addCookie(cookies[i]);
}
}
但是,我在第1230行遇到异常
java.lang.IllegalArgumentException: No PolicyContextHandler for key=javax.servlet.http.HttpServletResponse
13:24:16,457 ERROR [STDERR] at javax.security.jacc.PolicyContext.getContext(PolicyContext.java:107)
13:24:16,457 ERROR [STDERR] at myPackage.myBean(myBean.java:1230)
13:24:16,457 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
13:24:16,457 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
13:24:16,457 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
13:24:16,457 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:597)
13:24:16,457 ERROR [STDERR] at org.apache.el.parser.AstValue.invoke(AstValue.java:170)
13:24:16,457 ERROR [STDERR] at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
13:24:16,457 ERROR [STDERR] at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
13:24:16,457 ERROR [STDERR] at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
13:24:16,457 ERROR [STDERR] at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
13:24:16,457 ERROR [STDERR] at javax.faces.component.UICommand.broadcast(UICommand.java:387)
13:24:16,457 ERROR [STDERR] at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:329)
13:24:16,457 ERROR [STDERR] at org.ajax4jsf.component.AjaxViewRoot.broadcastEventsForPhase(AjaxViewRoot.java:304)
13:24:16,457 ERROR [STDERR] at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:261)
获得request
个对象但不是response
个对象。如何解决这个问题?
答案 0 :(得分:1)
在托管bean中不需要使用PolicyContext
。在托管bean中,您已经通过ExternalContext
直接访问HttpServletRequest
和HttpServletResponse
。
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
// ...
PolicyContext
仅在EJB等服务类中有用,在这些类中,您无法直接访问它们。导致您的具体问题是因为PolicyContext
根本不记得HTTP servlet响应,而只记得HTTP servlet请求,因为有关当前登录用户的信息存储在那里。实质上,HTTP servlet响应与策略上下文无关。
请注意,ExternalContext
还提供了方便的getRequestCookieMap()
方法,该方法返回所有Cookie的映射,因此您无需手动遍历Cookie[]
。
Cookie cookie = (Cookie) externalContext.getRequestCookieMap().get("myCookie");
// ...
我知道您正在使用JSF 1.2,但如果您使用的是JSF 2.0或更新版本,那么您也可以使用方便的addResponseCookie()
方法添加cookie而无需获取“原始” servlet响应。