java - Confused with different methods of creating cookie in HttpClient -


there different methods create cookies in httpclient, confused 1 best. need create,retrieve , modify cookies.

for example , can use following code see list of cookies , modify them how create them ?

is proper method retrieving them? need them accessible in classes.

  • in addition methods have found require httpresponse, httprequest objects send cookie browser, how if not want use them?

code

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();       (int = 0; < 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);     } {       method.releaseconnection();     }   } } 

and i've tried create cookie using following code not

import org.apache.commons.httpclient.httpclient; import org.apache.commons.httpclient.methods.getmethod;  ....  public string execute() { try{       system.err.println("creating cookie");      httpclient httpclient = new httpclient();      httpclient.getparams().setparameter("http.useragent", "my browser");       getmethod method = new getmethod("http://localhost:8080/");      httpclient.executemethod(method);      org.apache.commons.httpclient.cookie cookie = new                                                  org.apache.commons.httpclient.cookie();      cookie.setpath("/");      cookie.setname("tim");      cookie.setvalue("tim");      cookie.setdomain("localhost");      httpclient.getstate().addcookie(cookie);      httpclient.executemethod(method);      system.err.println("cookie");    }catch(exception e){      e.printstacktrace();   } 

output following no cookie created.

severe: creating cookie severe: cookie 

scenario

1)user has access form search products (example.com/search/products) 2)user fills form , submit class search 3)form submitted search class  4)method products of search class returns , shows description of product           (example.com/search/products) 5)user clicks on "more" button more description product  6)request sent product class (example.com/product/description?id=4) 7)user clicks on "add cookie" button add product id cookie   product class subclasse of class. can not extend more class. 

in second example, creating new client-side cookie (i.e. impersonating browser , sending cookie to server).

this means need provide all relevant information, client can decide whether send cookie server or not.

in code correctly set path,name , value, domain information missing.

org.apache.commons.httpclient.cookie cookie    = new org.apache.commons.httpclient.cookie(); cookie.setdomain("localhost"); cookie.setpath("/"); cookie.setname("tim"); cookie.setvalue("tim"); 

this works if trying achieve send cookie http server.

your second example, though, spans execute method, since hinting @ struts2 in tag, maybe class containing meant struts2 action.

if case, trying achieve send new cookie browser.

the first approach hold of httpservletresponse in:

so action must like:

public class setcookieaction      implements servletresponseaware  // needed access                                       // httpservletresponse {      httpservletresponse servletresponse;      public string execute() {         // create cookie         cookie div = new cookie("tim", "tim");         div.setmaxage(3600); // lasts 1 hour          servletresponse.addcookie(div);         return "success";     }       public void setservletresponse(httpservletresponse servletresponse) {         this.servletresponse = servletresponse;     }  } 

another approach (without httpservletresponse) obtained using cookieproviderinterceptor.

enable in struts.xml

<action ... >   <interceptor-ref name="defaultstack"/>   <interceptor-ref name="cookieprovider"/>   ... </action> 

then implement cookieprovider as:

public class setcookieaction      implements cookieprovider  // needed provide coookies {      set<javax.servlet.http.cookie> cookies=             new hashset<javax.servlet.http.cookie>();      public set<javax.servlet.http.cookie> getcookies()      {             return cookies;     }      public string execute() {         // create cookie         javax.servlet.http.cookie div =                  new javax.servlet.http.cookie("tim", "tim");         div.setmaxage(3600); // lasts 1 hour          cookies.put(cookie)         return "success";     }  } 

(credits @romanc pointing out solution)

if subsequently need read have 2 options:

  • implement servletrequestaware in action , read cookies httpservletrequest
  • introduce cookieinterceptor , implement cookiesaware in action, method setcookiemap allows read cookies.

here can find relevant info:


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -