Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Can one write/set a cookie from inside a bowser java applet?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Francois Belfort  
View profile  
 More options Aug 11 2004, 7:24 am
Newsgroups: comp.lang.java.help
From: francoisbelf...@yahoo.fr (Francois Belfort)
Date: 11 Aug 2004 04:24:10 -0700
Local: Wed, Aug 11 2004 7:24 am
Subject: Can one write/set a cookie from inside a bowser java applet?
A friend wants to set a permanent cookie
from inside a Java applet that runs
inside the browser VM. Is that possible?

Can one write a Java Applet that does this?

fb


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
William Brogden  
View profile  
 More options Aug 11 2004, 8:16 am
Newsgroups: comp.lang.java.help
From: "William Brogden" <wbrog...@bga.com>
Date: Wed, 11 Aug 2004 07:16:47 -0500
Local: Wed, Aug 11 2004 8:16 am
Subject: Re: Can one write/set a cookie from inside a bowser java applet?
On 11 Aug 2004 04:24:10 -0700, Francois Belfort <francoisbelf...@yahoo.fr>  
wrote:

> A friend wants to set a permanent cookie
> from inside a Java applet that runs
> inside the browser VM. Is that possible?

> Can one write a Java Applet that does this?

> fb

You will have to use JavaScript - it can read public applet
variables and set a cookie.

Bill
--
www.wbrogden.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Francois Belfort  
View profile  
 More options Aug 17 2004, 9:11 am
Newsgroups: comp.lang.java.help
From: francoisbelf...@yahoo.fr (Francois Belfort)
Date: 17 Aug 2004 06:11:57 -0700
Local: Tues, Aug 17 2004 9:11 am
Subject: Re: Can one write/set a cookie from inside a bowser java applet?

"William Brogden" <wbrog...@bga.com> wrote in message <news:opsckhp9b6k0yerx@ruby>...
> > A friend wants to set a permanent cookie
> > from inside a Java applet that runs
> > inside the browser VM. Is that possible?

> > Can one write a Java Applet that does this?

> You will have to use JavaScript - it can read public applet
> variables and set a cookie.

My friend wants to do this from inside the java applet,
without javascript, but in Java itself. Is that possible?
(He also stresses that the cookie should be a session cookie,
not a permanent one)

Thanks!

fb


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
zoopy  
View profile  
 More options Aug 17 2004, 12:56 pm
Newsgroups: comp.lang.java.help
From: zoopy <zo...@hates.spam>
Date: Tue, 17 Aug 2004 18:56:31 +0200
Local: Tues, Aug 17 2004 12:56 pm
Subject: Re: Can one write/set a cookie from inside a bowser java applet?
On 17-8-2004 15:11, Francois Belfort wrote:

I thought at first that it would not be possible (java.applet.AppletContext allows you to inteface
with the browser, but it doesn't provide a set/get cookie methods)

But 'liveconnect' comes to the rescue. It allows you to interact with the browser's DOM from within
the applet. It's a sort of using JavaScript from within Java.

The class below shows an applet setting and reading a cookie. Also included a HTML doc demonstrating
the applet.

Some points for using and compiling:
1) To be able to use a 'liveconnect' applet, the attribute "MAYSCRIPT" is required in the <applet> tag.
2) To compile you need to include 'plugin.jar' in the classpath; this contains the
netscape.javascript.JSObject class. 'plugin.jar' can be found in the lib directory of your JRE, e.g.
C:\Program Files\Java\j2re1.4.2\lib\plugin.jar
3) It's likely not to work with Microsoft's VM, but only with browsers with Sun's Java plugin.
(tried it successfully with Mozilla and IE6 with the jre1.5b2 plugin)
4) To see what the applet is printing, open the Java console.
5) For a session cookie: if you don't specify an expires=<gmtDate> for the cookie, the browser will
remove the cookie at end of session. If you want to set expiration, you'll need to enhance the
applet's setCookie method

Regards,
Z.

---------File: SGCookie.java --------------
import java.applet.Applet;
import netscape.javascript.JSException;
import netscape.javascript.JSObject;
/**
  * Demonstration of setting cookies from within an applet.
  * <p>
  * To be able to use this applet, the attribute "MAYSCRIPT" is required
  * in the applet tag, e.g.
  * <pre>
  * &lt;applet
  *     code="SGCookie.class"
  *     codebase="."
  *     name="cookieApp"
  *     MAYSCRIPT
  * &gt;
  * &lt;/applet&gt;
  * </pre>
  * @author zoopy
  * @created Aug 17, 2004
  */
public class SGCookie extends Applet
{
   private JSObject window;
   private JSObject document;

   public void init()
   {
     // Get a reference to the DOM window and document objects
     try
     {
       window = JSObject.getWindow(this);

       System.out.println("init()\twindow=" + window);

       document = (JSObject) window.getMember("document");

       System.out.println("init()\tdocument=" + document);
     }
     catch (JSException ex)
     {
       // We couldn't get reference to either object
       ex.printStackTrace();
     }
   }

   public void setCookie(String name, String value)
   {
     if (document == null)
     {
       // init() failed to obtain a reference to document object.
       System.out.println("Can't set cookie: no document reference");
       return;
     }

     try
     {
       /*
        * should do here checks to ensure name and value don't contain
        * invalid characters and name is not an empty string
        * ...
        */

       String cookieText = name + "=" + value;

       System.out.println("Setting document.cookie = \"" + cookieText + '"');

       // Set document member "cookie" to cookieText
       // Similar to Javascript    document.cookie = "name=value";
       document.setMember("cookie", cookieText);

       // Read document cookies; This might return multiple
       // name=value pairs separated by ';' (a document can
       // have more than one cookie)
       // Similar to  Javascript    cookies = document.cookie;
       Object cookies = document.getMember("cookie");

       System.out.println("Got document.cookie = \"" + cookies + '"');
     }
     catch (JSException ex)
     {
       // Something went wrong
       ex.printStackTrace();
     }
   }

}

---------------------------------------

---------File: demo.html --------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
                "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Set Cookie With Java Applet</title>
</head>
<body>
<h1>Set Cookie With Java Applet</h1>
<form name="myForm">
Cookie
name:<input Type="text" value="foo" name="cookieName">
=
value:<input Type="text" value="bar" name="cookieValue">
<input Type="button" value="Set Cookie"
onClick="document.cookieApp.setCookie(document.myForm.cookieName.value,
document.myForm.cookieValue.value)">
</form>

<!-- The MAYSCRIPT attribute is required in order for the Java methods to be available to the
JavaScript functions. -->
<applet code="SGCookie.class" codebase="." name="cookieApp" NOTMAYSCRIPT>
</applet>

</body>
</html>
---------------------------------------


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
zoopy  
View profile  
 More options Aug 17 2004, 1:29 pm
Newsgroups: comp.lang.java.help
From: zoopy <zo...@hates.spam>
Date: Tue, 17 Aug 2004 19:29:31 +0200
Local: Tues, Aug 17 2004 1:29 pm
Subject: Re: Can one write/set a cookie from inside a bowser java applet?
On 17-8-2004 18:56, zoopy wrote:

> ---------File: demo.html --------------
> <applet code="SGCookie.class" codebase="." name="cookieApp" NOTMAYSCRIPT>

Change that to
   <applet code="SGCookie.class" codebase="." name="cookieApp" MAYSCRIPT>

--
Regards,
Z.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Francois Belfort  
View profile  
 More options Aug 18 2004, 7:59 am
Newsgroups: comp.lang.java.help, comp.lang.java.programmer
From: francoisbelf...@yahoo.fr (Francois Belfort)
Date: 18 Aug 2004 04:59:33 -0700
Local: Wed, Aug 18 2004 7:59 am
Subject: Re: Can one write/set a cookie from inside a bowser java applet?

zoopy <zo...@hates.spam> wrote in message <news:4122406f$0$21106$e4fe514c@news.xs4all.nl>...
> On 17-8-2004 18:56, zoopy wrote:
> > ---------File: demo.html --------------
> > <applet code="SGCookie.class" codebase="." name="cookieApp" NOTMAYSCRIPT>

> Change that to
>    <applet code="SGCookie.class" codebase="." name="cookieApp" MAYSCRIPT>

That is great! Thank you very much for the code!
The code seems to need  enabled javascript in the browser.
is there also a way to do this without javascript?
(or at least, with a browser that is not necessarily set to
javascript enabled, so that it works for any setting?)

francois


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
zoopy  
View profile  
 More options Aug 18 2004, 8:39 am
Newsgroups: comp.lang.java.help, comp.lang.java.programmer
From: zoopy <zo...@hates.spam>
Date: Wed, 18 Aug 2004 14:39:13 +0200
Local: Wed, Aug 18 2004 8:39 am
Subject: Re: Can one write/set a cookie from inside a bowser java applet?
On 18-8-2004 13:59, Francois Belfort wrote:

Don't think it works with Javascript disabled (the code eventually uses the Javascript engine of the
browser), but just give it a try (it's easy to test).

--
Regards,
Z.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »