write cookie fro applet

Posted on 2004-10-29
Last Modified: 2013-11-24
hi all,

I am using applet-servlet communication to write data onto client machine when user logs in for the first time.  I want to save my sessionID in a cookie . how do i do it from my applet??
PS:  I do not want to use netscape.javascript package since i dont have a control over browser version/ type  and OS of client machine.

Question by:sujata7576
    LVL 92

    Expert Comment

    the session id is probably already being written to a cookie.

    Author Comment


    << the session id is probably already being written to a cookie. >>

    No, Actually the session we are  maintaining is not Tomcat Session ,but some customized Session which is created when user logs in and expires on log out . so this sessionid is not written in cookie
    LVL 92

    Expert Comment


    o/wise using jsobject:

    JSObject document = (JSObject) JSObject.getWindow(this).getMember("document");      
    LVL 4

    Accepted Solution

    If you do not want to use JSObject, which is supported in most browsers and JREs, and I can understand this, then my recommendation would be to turn the issue around, and use JavaScript to write the cookie.

    Depending on when your applet determines what the session ID will be, you can have your JavaScript poll the applet. SOmeting along the lines of:

    <body onLoad="poll()">

    <applet name=MyApplet ....>

    <script language="javascript">
    function poll()
       var sessionID = MyApplet.getSessionID();
       if (sessionID == null)
          setTimeout("poll();", 1000);
          document.cookie = cookieName + "=" + escape(sessionID);


    This is the simplest form, and once the cookie has been set, you can stop the polling.

    You can also add expiration and domain settings as well if you wish.

    As for using this, one reason I can see is that I have encountered browser/JRE combinations where, either JSObject does not exist, or the Java to JavaScript simply does not work. Also, the latest Sun JREs (1.4.2_05 and 1.5) have acknowledged bugs in this communication that have the potential to lock up the browser. I do not believe those woudl come into play in your case, but they are another reason to avoid the mechanism.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now