Solved

Applets Cookies Password

Posted on 2000-03-21
12
405 Views
Last Modified: 2013-11-23
I have a site which is to be viewed locally with a number of applets on.  I want some of the applets to check if the user has logged in, and if not, ask them their password.  This would then be stored in the browser for that session only, so if they went to pages with other applets on, those applets would know the user was logged in and not need to ask.

I guess the best way to do this is cookies - but I don't know how to write these with Java, or even if you can.  I would prefer a Java-only solution, but some Java/HTML/JavaScript-based solution would be OK too.

Cheers,

Tom
0
Comment
Question by:yatest5
12 Comments
 
LVL 2

Expert Comment

by:muraliram
ID: 2639774
Dear yatest5,

    If you are using Java Servlets then you can do this easily using Servlet Session API. Otherwise you can do this from JavaScript. I prefer Java Servlets

Also visit

http://www.cookiecentral.com/faq/index.shtml

Bye

All the best

Murali
0
 
LVL 1

Author Comment

by:yatest5
ID: 2639784
Cheers Murali, but unfortunately I'm not using servlets.  I'll have a look at your link though.

Ta,

Tom
0
 
LVL 1

Expert Comment

by:dmaguillo
ID: 2639790
Hi yatest5. Here i have an useful page with cookies examples that can help you...

http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/index.html

(It`s based on servlets)

Bye... :)
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 2

Expert Comment

by:muraliram
ID: 2639856
Dear yatest5,

      Teh link I have given is not related with Servlet. Go and have a llok at it. It may help you. Try it in
your own.

Bye

Murali
0
 
LVL 1

Author Comment

by:yatest5
ID: 2639879
Adjusted points from 100 to 150
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2640319
as far as I remember, applets can't access cookies (after all, cookies are kind of client side 'resources') - you'll need some JavaScript + Java soluton.
0
 
LVL 1

Author Comment

by:yatest5
ID: 2640353
So the first person to show me how to call javascript cookie-writing code from an applet gets the cookies, erm, points.
0
 
LVL 1

Author Comment

by:yatest5
ID: 2640356
mmmmmmmmmmmmmm, I like cookies, yummeee
0
 
LVL 2

Expert Comment

by:muraliram
ID: 2640397
Dear yatest5,

Try the following. Its a link which I have given you earlly. I think you haven't had a llok into it. Try the following


<html>
<head>
<title>Cookie Functions</title>
</head>
<body>
<script language="javascript">
<!-- begin script
//
//  Cookie Functions -- "Night of the Living Cookie" Version (25-Jul-96)
//
//  Written by:  Bill Dortch, hIdaho Design <bdortch@hidaho.com>
//  The following functions are released to the public domain.
//
//  This version takes a more aggressive approach to deleting
//  cookies.  Previous versions set the expiration date to one
//  millisecond prior to the current time; however, this method
//  did not work in Netscape 2.02 (though it does in earlier and
//  later versions), resulting in "zombie" cookies that would not
//  die.  DeleteCookie now sets the expiration date to the earliest
//  usable date (one second into 1970), and sets the cookie's value
//  to null for good measure.
//
//  Also, this version adds optional path and domain parameters to
//  the DeleteCookie function.  If you specify a path and/or domain
//  when creating (setting) a cookie**, you must specify the same
//  path/domain when deleting it, or deletion will not occur.
//
//  The FixCookieDate function must now be called explicitly to
//  correct for the 2.x Mac date bug.  This function should be
//  called *once* after a Date object is created and before it
//  is passed (as an expiration date) to SetCookie.  Because the
//  Mac date bug affects all dates, not just those passed to
//  SetCookie, you might want to make it a habit to call
//  FixCookieDate any time you create a new Date object:
//
//    var theDate = new Date();
//    FixCookieDate (theDate);
//
//  Calling FixCookieDate has no effect on platforms other than
//  the Mac, so there is no need to determine the user's platform
//  prior to calling it.
//
//  This version also incorporates several minor coding improvements.
//
//  **Note that it is possible to set multiple cookies with the same
//  name but different (nested) paths.  For example:
//
//    SetCookie ("color","red",null,"/outer");
//    SetCookie ("color","blue",null,"/outer/inner");
//
//  However, GetCookie cannot distinguish between these and will return
//  the first cookie that matches a given name.  It is therefore
//  recommended that you *not* use the same name for cookies with
//  different paths.  (Bear in mind that there is *always* a path
//  associated with a cookie; if you don't explicitly specify one,
//  the path of the setting document is used.)
//  
//  Revision History:
//
//    "Toss Your Cookies" Version (22-Mar-96)
//      - Added FixCookieDate() function to correct for Mac date bug
//
//    "Second Helping" Version (21-Jan-96)
//      - Added path, domain and secure parameters to SetCookie
//      - Replaced home-rolled encode/decode functions with Netscape's
//        new (then) escape and unescape functions
//
//    "Free Cookies" Version (December 95)
//
//
//  For information on the significance of cookie parameters, and
//  and on cookies in general, please refer to the official cookie
//  spec, at:
//
//      http://www.netscape.com/newsref/std/cookie_spec.html    
//
//******************************************************************
//
// "Internal" function to return the decoded value of a cookie
//
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
//
//  Function to correct for 2.x Mac date bug.  Call this function to
//  fix a date object prior to passing it to SetCookie.
//  IMPORTANT:  This function should only be called *once* for
//  any given date object!  See example at the end of this document.
//
function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}
//
//  Function to return the value of the cookie specified by "name".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
//
function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}
//
//  Function to create or update a cookie.
//    name - String object containing the cookie name.
//    value - String object containing the cookie value.  May contain
//      any valid string characters.
//    [expires] - Date object containing the expiration data of the cookie.  If
//      omitted or null, expires the cookie at the end of the current session.
//    [path] - String object indicating the path for which the cookie is valid.
//      If omitted or null, uses the path of the calling document.
//    [domain] - String object indicating the domain for which the cookie is
//      valid.  If omitted or null, uses the domain of the calling document.
//    [secure] - Boolean (true/false) value indicating whether cookie transmission
//      requires a secure channel (HTTPS).  
//
//  The first two parameters are required.  The others, if supplied, must
//  be passed in the order listed above.  To omit an unused optional field,
//  use null as a place holder.  For example, to call SetCookie using name,
//  value and path, you would code:
//
//      SetCookie ("myCookieName", "myCookieValue", null, "/");
//
//  Note that trailing omitted parameters do not require a placeholder.
//
//  To set a secure cookie for path "/myPath", that expires after the
//  current session, you might code:
//
//      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

//  Function to delete a cookie. (Sets expiration date to start of epoch)
//    name -   String object containing the cookie name
//    path -   String object containing the path of the cookie to delete.  This MUST
//             be the same as the path used to create the cookie, or null/omitted if
//             no path was specified when creating the cookie.
//    domain - String object containing the domain of the cookie to delete.  This MUST
//             be the same as the domain used to create the cookie, or null/omitted if
//             no domain was specified when creating the cookie.
//
function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

//
//  Examples
//
var expdate = new Date ();
FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); // 24 hrs from now
SetCookie ("ccpath", "http://www.hidaho.com/colorcenter/", expdate);
SetCookie ("ccname", "hIdaho Design ColorCenter", expdate);
SetCookie ("tempvar", "This is a temporary cookie.");
SetCookie ("ubiquitous", "This cookie will work anywhere in this domain",null,"/");
SetCookie ("paranoid", "This cookie requires secure communications",expdate,"/",null,true);
SetCookie ("goner", "This cookie must die!");
document.write (document.cookie + "<br>");
DeleteCookie ("goner");
document.write (document.cookie + "<br>");
document.write ("ccpath = " + GetCookie("ccpath") + "<br>");
document.write ("ccname = " + GetCookie("ccname") + "<br>");
document.write ("tempvar = " + GetCookie("tempvar") + "<br>");
// end script -->
</script>
</body>
</html>


This is lengthy. But teh code contains some utility functions whch will be useful.

Bye

Murali
0
 
LVL 1

Author Comment

by:yatest5
ID: 2640410
Cheers Murali,

This is all good stuff but I still don't know how to call jscript functions and access variables from a java applet.

ta
0
 
LVL 10

Accepted Solution

by:
kollegov earned 150 total points
ID: 2640750
You would need to download LiveConnect package from
http://developer.netscape.com MIE4+ and NN3 both have this package
in browsers' standard so you don't need to bundle it with applet.
But you would need it to compile applet in JDK.

Note also that MIE cookie is only for http:// content.
You can't store cookie when loaded from file://  

Here is solution I used:


import java.awt.*;
import java.applet.*;
import java.net.*;
import netscape.javascript.*;

public class test extends Applet {

  public void init(){

// actually you can evaluate any // command returning String    System.out.println(evaluatejs("document.cookie+''"));
  }

  String evaluatejs(String cmd)
  {try
     {JSObject w=JSObject.getWindow(this);
      String args[] = new String[0];
      w.eval("function vmax_effects_handler(){"+cmd+";};");
      Object o=w.call("vmax_effects_handler",args);
      if(o instanceof String) return (String)o;
     }
    catch(NoClassDefFoundError nf){}  
    catch(UnsatisfiedLinkError le){}
    catch(Exception ex){System.out.println(ex);}
    return null;
  }

}
0
 
LVL 2

Expert Comment

by:muraliram
ID: 2643005
Dear yatesty5,

I will tell you how to call applet methods and variables from JavaScript

<html>
<head>
<applet code="your.class" name="myapplet">
</applet>
<script>
var name="myname";
function t()
{
 document.myapplet.somepublicmethod(name);
}
</script>
</head>
</html>

You cann call any public method of the applet and access all the public variables from Javascript and pass any variables.

Bye

Murali
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 86
difference of if loops 23 49
Why my table column Id is not passed to java object? 4 39
Unhandled exception type Exception 18 31
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

770 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