Java 我能';t检索可用Cookie的列表

Java 我能';t检索可用Cookie的列表,java,jakarta-ee,cookies,struts2,Java,Jakarta Ee,Cookies,Struts2,一旦我调用下面类的方法,它就会返回cookie列表,但是当我尝试从另一个类访问同一方法时,它会返回NullPointerException 我想原因是servletRequest,但如何解决呢?有没有其他方法来实现它 public class ClientFind extends ActionSupport implements ServletResponseAware, Servl

一旦我调用下面类的方法,它就会返回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 "";
}

我不确定这是否是您想要的,但请尝试以下代码

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

public class GetCookiePrintAndSetValue {

  public static void main(String args[]) throws Exception {

    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "My Browser");

    GetMethod method = new GetMethod("http://localhost:8080/");
    try{
      client.executeMethod(method);
      Cookie[] cookies = client.getState().getCookies();
      for (int i = 0; i < cookies.length; i++) {
        Cookie cookie = cookies[i];
        System.err.println(
          "Cookie: " + cookie.getName() +
          ", Value: " + cookie.getValue() +
          ", IsPersistent?: " + cookie.isPersistent() +
          ", Expiry Date: " + cookie.getExpiryDate() +
          ", Comment: " + cookie.getComment());

        cookie.setValue("My own value");
      }
      client.executeMethod(method);
    } catch(Exception e) {
      System.err.println(e);
    } finally {
      method.releaseConnection();
    }
  }
}
import org.apache.commons.httpclient.Cookie;
导入org.apache.commons.httpclient.HttpState;
导入org.apache.commons.httpclient.httpclient;
导入org.apache.commons.httpclient.methods.GetMethod;
公共类GetCookiePrintAndSetValue{
公共静态void main(字符串args[])引发异常{
HttpClient=新的HttpClient();
client.getParams().setParameter(“http.useragent”、“我的浏览器”);
GetMethod=新的GetMethod(“http://localhost:8080/");
试一试{
客户端执行方法(方法);
Cookie[]cookies=client.getState().getCookies();
for(int i=0;i<代码> > ServLeTebug < /代码>,我知道的很少,但是如果是<代码> Null PoExtExeals< /代码>困扰了你,你应该考虑一个事实,即<代码> GoCookies()/Case>方法如果没有cookie发送,则返回<代码> null < /> >。
看

编辑:
第二个例外,您在我的第一个响应之后添加的,很容易捕获。您实际上是在第三行将值
null
赋给
cookieStore

我肯定存在两个cookie。我肯定存在,@TimNorman,但是由于getCookies()很可能返回null,您的代码应该检查它。
coockies.length
听起来很脏:DBtw,问题一点也不清楚:如果你从ClientFind操作中阅读Cookie,它会起作用;如果您从另一个操作读取Cookie,它是否不起作用?我们能看到另一个动作吗?
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

public class GetCookiePrintAndSetValue {

  public static void main(String args[]) throws Exception {

    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "My Browser");

    GetMethod method = new GetMethod("http://localhost:8080/");
    try{
      client.executeMethod(method);
      Cookie[] cookies = client.getState().getCookies();
      for (int i = 0; i < cookies.length; i++) {
        Cookie cookie = cookies[i];
        System.err.println(
          "Cookie: " + cookie.getName() +
          ", Value: " + cookie.getValue() +
          ", IsPersistent?: " + cookie.isPersistent() +
          ", Expiry Date: " + cookie.getExpiryDate() +
          ", Comment: " + cookie.getComment());

        cookie.setValue("My own value");
      }
      client.executeMethod(method);
    } catch(Exception e) {
      System.err.println(e);
    } finally {
      method.releaseConnection();
    }
  }
}