一旦我调用以下类的方法,它就会返回 cookie 列表,但是当我尝试从另一个类访问相同的方法时,它会返回NullPointerException
.
我想原因是servletRequest
但是如何解决呢?还有其他方法可以实现吗?
public class ClientFind extends ActionSupport implements ServletResponseAware,
ServletRequestAware
{
.....
Cookie coockies[] = servletRequest.getCookies();
for (int i = 0; i < coockies.length; i++) {
if (coockies[i].getName().equalsIgnoreCase("ID")) {
return coockies[i].getValue();
}
}
return "";
}
我也使用了以下代码,但它java.lang.NullPointerException
在第 4 行返回
1 BasicClientCookie cookie = new BasicClientCookie("Namez", "Tim");
2 cookie.setPath("/");
3 org.apache.http.client.CookieStore cookieStore = null;
4 cookieStore.addCookie(cookie);
5 DefaultHttpClient httpclient = new DefaultHttpClient();