1

Having some problem with this code. When I send applet request to the JSP and get cookies on JSP then send request to the server then cookie is not enable on server.

public class Helloworld extends Applet {

String ip;

public void init(){

 resize(350,350);
 int Port = 0;
 try
    {
        if(getDocumentBase().getPort()!=-1){
        Port=getDocumentBase().getPort();
        }
    else
    {
        Port=80;
    }
        Socket socket = new Socket(getDocumentBase().getHost(), Port);
        ip = socket.getLocalAddress().getHostAddress();
       }
       catch(IOException io)
       {
       System.out.println(io.getMessage());
    }
}
  public void paint(Graphics g){
       StringBuffer sb = new StringBuffer()
       .append(" IP address : ").append(ip);
       g.drawString(sb.toString(), 50,100);
}
  public void setCookieUsingCookieHandler() {
      try {
            CookieManager manager = new CookieManager();
            manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
            CookieHandler.setDefault(manager);
            URL url = new URL("http://LocalHost8081");
           // URL url = applet.getCodeBase();


            URLConnection connection = url.openConnection();
            connection.getContent();

            // get cookies from underlying CookieStore
            CookieStore cookieJar = manager.getCookieStore();
            java.util.List<HttpCookie>  cookies = cookieJar.getCookies();

            for (HttpCookie cookie: cookies) {
                System.out.println("CookieHandler retrieved cookie: " + cookie);
            }
        } catch(Exception e) {
            System.out.println("Unable to get cookie using CookieHandler");
            e.printStackTrace();
        }
        try {
            // instantiate CookieManager
            CookieManager manager = new CookieManager();
            CookieHandler.setDefault(manager);
            CookieStore cookieJar = manager.getCookieStore();

            //create cookies
            HttpCookie cookie = new HttpCookie("IpAddress", "ip");
            //URL connection
            URL url = new URL("http://LocalHost8081");
            //URL url = applet.getCodeBase();
            //cookie jar
            cookieJar.add(url.toURI(), cookie);
            System.out.println("Added cookie using cookie handler");
        } catch(Exception e) {
 System.out.println("Unable to set cookieusingCookieHandler");
            e.printStackTrace();
        }
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Mar 22 '15 at 02:25
  • *"then cookie is not enable on server."* What does that mean? And why are you sending the cookie to the server? – Andrew Thompson Mar 22 '15 at 02:27

0 Answers0