Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Easy Learn Java: Programming Articles, Examples and Tips - Page 148


Previous 1060 Stories (530 Pages, 2 Per Page) Next

Java Newsletters: Java Newsletters Archive: 18

Go to all tips in Java Newsletters
***********************************************
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
* > The Java FAQ Daily Tips, weekly publication < *
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
* *
* Issue No: 18 3 January 2001 *
* http://www.javafaq.nu/java *
* *
* *
* Please recommend our FREE "100 Java Tips" book and us to your *
* friends and colleagues! *
* http://javafaq.nu/java/advert/our_book.shtml *
***********************************************

Table of Contents

1. I am writing an application, which requires me to set and read
cookies. But it fails...
2. I would like to burn a CD from within my code, that way I won't ..
3. I try to copy an object of my own using the clone() method...
4. I was just wondering about the usefulness of Interfaces...
5. I'm interested in writing a little mp3 player in Java...
6. How can I slow down my applet?
7. On MsSQLSERVER7 I am able to load the driver but it says unable
to get socket connection.
***********************************************

Hello dear friends!

***********************************************

Tip 1 Q: I am writing an application, using Java Servlets, which
requires me to set and read cookies.
Using the servlet API, and the javax.servlet.http.Cookie class.
I created a new cookie, and added it to the http response, using
its addCookie() method. It works okay, but fails if I use the
setDomain method, on the newly created cookie, to set the domain
of the cookie to something other than the current server.

Answer: I suspect that is not a legal operation for any browser to
accept a cookie that has a domain inconsistent with the source
of the cookie.
by William Brogden

***********************************************

Tip 2 Q: I would like to burn a CD from within my code, that way
I won't have to waste so much time making illegal copies of music.
Is there a convenient way to do this in Java?

Answer: Unfortunately Java doesn't provide any API for this Smile

***********************************************

Tip 3 Q: I try to copy an object of my own using the clone() method
from java.lang.Object, but this is a protected method so I can't
use it. Is there some other way to get my objective of duplicating
an arbitrary object?

Answer:If you want to clone your object, you need to make it
cloneable. To achieve this, you need to do two things:

1. implement the interface Cloneable

2. override the method clone(), so that it
a. becomes public
b. calls super.clone()
c. if necessary, clones any members, or
d. if a member can't be cloned, creates a new instance.

Simple example:
public MyClass implements Cloneable {
int someNumber;
String someString;

public Object clone() {
// primitives and Strings are no
// problem
return super.clone();
}
}

In this case the method clone() of the class MyClass returns a new
instance of MyClass, where all members have exactly the same value.
That means, the object reference 'someString' points to the same
object. This is called a shallow copy. In many cases this is no
problem. Strings are immutable and you do not
need a new copy. But if you need new copies of members, you have
to do it in the clone() method. Here is another simple example:

public class SomeMember implements Cloneable {
long someLong;

public Object clone() {
return super.clone();
}
}

public AnotherClass extends MyClass {
SomeMember someMember;

public Object clone() {
AnotherClass ac = (AnotherClass)(super.clone());
if (someMember != null) {
ac.someMember = (SomeMember)(someMember.clone());
}
return ac;
}
}

Note that the class AnotherClass, that extends MyClass,
automatically becomes Cloneable, because MyClass is Cloneable.

Also note, that super.clone() always returns an Object of the
type of the actual object, although the superclass doesn't know
anything about that sub class. The reason is, that Object.clone()
is a native method, which just allocates new memory for the new
object and copies the bytes to that memory. Native code has it's
own ways of finding out which type to return Wink

***********************************************

Tip 4 Q: I was just wondering about the usefulness of Interfaces...
Q: I was just wondering about the usefulness of Interfaces. I was
under the impression that interfaces could be used to perform
multiple inheritance. But an interface only declares a method -
in a very abstract way.

A class that implements an interface needs to define its own
implementation of a certain method. What is the use of having an
interface when nothing is being gained...?

A: If two classes implements the same interface, you can get a
reference to the interface instead of the effective class without
bother what class are you managing.

This is very useful in RMI (for example) or in any condition when
you have to take an object without knowing exactly his class, but
only the interface that it implement.

For example:
public void recurseList( List l )

the generic List ensure that you can use every List for this method
(ArrayList, AbstractList, Vector...), so your calling method can be:

ArrayList l = new ArrayList(); or
Vector l = new Vector();

recurseList( l );

***********************************************

Tip 5 Q: I'm interested in writing a little mp3 player in Java...
I'm interested in writing a little mp3 player in java. I have an
entirely different app right now that plays sound (wav files),
and I substituted an mp3 file for one of the waves but it didn't
work. Can anyone tell me if java even supports mp3 files?

A: Go to the "Products & APIs" section of java.sun.com and look
for JMF (Java Media Framework). It's a library that also supports
reading MP3 files.

***********************************************

Tip 6 Q: How can I slow down my applet?
I have a game applet that is running too fast on newer systems
that have high-end video cards. Its easy enough to slow down the
game by having it sleep between thread cycles, but I need to be
able to determine how fast a users machine is before I determine
how long to sleep for.

I have been muddling through the documentation but cannot find
any calls that will tell my applet what the users configuration
is as regards to CPU speed and other components they may have on
their system.

A: Simple create a new Date (), then perform a standard lengthy
operation on the order of something that takes about one second
on your machine, like a long loop, then create another new Date()
and compare it to the first. If it takes 1/2 of the time compared
to your machine, then the CPU is probably about 2 times faster.
If it takes 3 times the duration compared to your machine, the
CPU is probably 1/3 as fast as yours.

Do this dynamically, and it might help with speed changes when
there's lots of action happening as well - unless this issue is
already being dealt with using threads, that is.


***********************************************

Tip 7 Q: I am working on weblogic server 5.1 with MsSQLSERVER7 I
am able to load the driver but it says unable to get socket
connection. It says connect to the MSSQLSERVER's host and port no.
How do I get these name and value?

The MS Sql Server's host is usually the name or ip of the server
that run SQL Server, if you know the IP (ping
in console or command line, like: ping javafaq.nu),
put the IP in it, it will be faster, for the Port number,
sincerely I don't remember the standard port, but look into the
SQL Server documentation and you will find it.


**********************************************************

The Java FAQ Daily Tips is a newsletter that is only sent to those
who have specifically subscribed to it.



Copyright (c) 2000 John Andersson
***********************************************
* *
* You can find our tips on site also! *
* Please recommend us and our FREE "100 Java Tips" book to your *
* friends and colleagues! *
* http://javafaq.nu/java/advert/our_book.shtml *
* *
***********************************************

21 comments | Printer Friendly Page  Send to a Friend | Java Newsletters | Score: 0
Posted by jalex on Monday, February 02, 2004 (15:16:17) (2778 reads)

Java Newsletters: Java Newsletters Archive: 17

Go to all tips in Java Newsletters
***********************************************
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
* > The Java FAQ Daily Tips, weekly publication < *
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
*
* Issue No: 17 27 December 2000
* http://www.javafaq.nu/java
*
*
* Please recommend our FREE "100 Java Tips" book and us to your
* friends and colleagues!
* http://javafaq.nu/java/advert/our_book.shtml
***********************************************

Table of Contents

1. I don't know anything about security... Part 2.
2. I guess what I'm asking is "Is java.util.Hashtable
thread safe?"
3. Is there any way to run code after the VM has been
instructed to exit?
4. Why would anyone use AWT and not Swing today?
5. I'm working on a java project and looking for a better
API that can generate PDF, work with Excel, Word documents...
6. Where can I find Java --> Native code compilers?
7. I have a directory having class files arranged in package
hierarcy. How can I make the executable of this whole directory?
***********************************************

Hello dear friends!

Happy New Year!
If you have your good tips please send us and we will add it to our
free book! One sunny day when your friends will load the new version of
our book they will find your name there also!

I hope you will have enough time to check Zapper - that make life much
easier and offers a new, more efficient way to get the information
you need from the Web.
It takes you from wherever you are on your desktop to exactly
what you're looking for on the Web - in just one click of your mouse!


Tip 1 Q: I need to read C: est.doc for example and store it
somewhere for my future use. I don't know anything about security.
Part 2, Part 1 was published last week.

Answer :
I think they want you to go out and buy a cert from Verisign or
Thawte, etc. You can also generate your own signing certificate
through openSSL, or some other package, but, if you do, you'll
need to have them import it into their database as a trusted signer.
This can be done with a simple Perl script which sends the
certificate with a header of:
"Content-Type: application/x-x509-ca-cert"
You must also add some code to your applet to use the Netscape
way of signing. You can download the file 'capsapi_classes.zip'
from Netscape. This file contains the Netscape Capabilities API.
Basically, all you need to do is import the package and enable
privileges as you need them. It is important to enable privileges
in the same method (ie. same frame stack)
in which you are going to use them. You cannot just enable all
the privileges you need in the init method of your applet. The
code looks something like this:

import netscape.security.*;
public void MyMethod() {

try {
PrivilegeManager.enablePrivilege("UniversalFileRead");
FileInputStream fis = new FileInputStream("myfile");
} catch (Exception e) {
System.out.println(e);
}
} // end MyMethod

You'll need to add 'capsapi_classes.zip' to the archive attribute
of your applet tag. I also believe it is possible to grant
privileges to a codebase in Netscape without signing. This is
done by editing the 'prefs.js' file on the client. Obviously,
this is not possible on the internet, but, for an intranet
environment, this could be a solution. Anyway, when an applet
tries to enable a privilege, the Netscape browser will pop up a
window asking the user if they wish to grant the privilege. This
decision can be persistent.
The user can manage the privileges they have granted by clicking
on the padlock icon in the bottom left corner of their browser
window, and selecting the "Java/JavaScript" section.

If your users may be using Microsoft IE as well as Netscape,
you'll have to learn the Microsoft security API. I haven't done
this yet, but I believe it is possible to code an applet so that
it will work in either browser.
Another alternative is to code different versions of the applet,
and build the page containing the applet dynamically, based on
the "User-Agent". I have found that it is possible in IE to grant
universal privileges to an applet, even if it is not signed, but
I sure wouldn't recommend doing that.



***********************************************

Tip 2 Q: I guess what I'm asking is "Is java.util.Hashtable
thread safe?"

It's been a while since I've used hashtables for anything
significant, but I seem to recall the get() and put()
methods being synchronized.
The JavaDocs don't reflect this. They simply say that the class
Hashtable is synchronized. What can I assume? If several threads
access the hashtable at the same time (assuming they are not
modifying the same entry), the operations will succeed, right?
I guess what I'm asking is "Is java.util.Hashtable thread safe?"

A: That is right! It is recommendable, if you have questions like
these, always look at source for the api, it's freely available.

***********************************************

Tip 3 Q: Is there any way to run code after the VM has been
instructed to exit?

A: In 1.3, you can use Runtime.addShutdownHook(Thread hook)


***********************************************

Tip 4 Q: Why would anyone use AWT and not Swing today?

A: There are several handheld devices that do not have a lot of
memory or CPU power available, AWT is more suitable for them
(Smaller footprint).


Tip 5 Q: I'm working on a java project and looking for a better
API that can generate PDF, work with Excel, Word documents...
Where can I find it?

Answer: We have a list here:
http://www.javafaq.nu/java/office/index.shtml


***********************************************

Tip 6 Q: Where can I find Java --> Native code compilers?

Answer: We just published a list of Java products that lets you do
Java --> Native code compilation:
http://javafaq.nu/java/staff/staff.shtml


***********************************************

Tip 7 Q: I have a directory having class files arranged in package
hierarcy. How can I make the executable of this whole directory?

Any application available for that in Windows NT environment.

Answer: Make a JAR file out of it and add a manifest file that
indicates which main() method of which class must be called.
Double-clicking this JAR file will run your application.


The Java FAQ Daily Tips is a newsletter that is only sent to those
who have specifically subscribed to it.

Copyright (c) 2000 John Andersson
***********************************************
* *
* You can find our tips on site also! *
* Please recommend us and our FREE "100 Java Tips" book to your *
* friends and colleagues! *
* http://javafaq.nu/java/advert/our_book.shtml *
* *
***********************************************

18 comments | Printer Friendly Page  Send to a Friend | Java Newsletters | Score: 0
Posted by jalex on Monday, February 02, 2004 (15:11:24) (3269 reads)

Previous 1060 Stories (530 Pages, 2 Per Page) Next

530| 529| 528| 527| 526| 525| 524| 523| 522| 521| 520| 519| 518| 517| 516| 515| 514| 513| 512| 511| 510| 509| 508| 507| 506| 505| 504| 503| 502| 501| 500| 499| 498| 497| 496| 495| 494| 493| 492| 491| 490| 489| 488| 487| 486| 485| 484| 483| 482| 481| 480| 479| 478| 477| 476| 475| 474| 473| 472| 471| 470| 469| 468| 467| 466| 465| 464| 463| 462| 461| 460| 459| 458| 457| 456| 455| 454| 453| 452| 451| 450| 449| 448| 447| 446| 445| 444| 443| 442| 441| 440| 439| 438| 437| 436| 435| 434| 433| 432| 431| 430| 429| 428| 427| 426| 425| 424| 423| 422| 421| 420| 419| 418| 417| 416| 415| 414| 413| 412| 411| 410| 409| 408| 407| 406| 405| 404| 403| 402| 401| 400| 399| 398| 397| 396| 395| 394| 393| 392| 391| 390| 389| 388| 387| 386| 385| 384| 383| 382| 381| 380| 379| 378| 377| 376| 375| 374| 373| 372| 371| 370| 369| 368| 367| 366| 365| 364| 363| 362| 361| 360| 359| 358| 357| 356| 355| 354| 353| 352| 351| 350| 349| 348| 347| 346| 345| 344| 343| 342| 341| 340| 339| 338| 337| 336| 335| 334| 333| 332| 331| 330| 329| 328| 327| 326| 325| 324| 323| 322| 321| 320| 319| 318| 317| 316| 315| 314| 313| 312| 311| 310| 309| 308| 307| 306| 305| 304| 303| 302| 301| 300| 299| 298| 297| 296| 295| 294| 293| 292| 291| 290| 289| 288| 287| 286| 285| 284| 283| 282| 281| 280| 279| 278| 277| 276| 275| 274| 273| 272| 271| 270| 269| 268| 267| 266| 265| 264| 263| 262| 261| 260| 259| 258| 257| 256| 255| 254| 253| 252| 251| 250| 249| 248| 247| 246| 245| 244| 243| 242| 241| 240| 239| 238| 237| 236| 235| 234| 233| 232| 231| 230| 229| 228| 227| 226| 225| 224| 223| 222| 221| 220| 219| 218| 217| 216| 215| 214| 213| 212| 211| 210| 209| 208| 207| 206| 205| 204| 203| 202| 201| 200| 199| 198| 197| 196| 195| 194| 193| 192| 191| 190| 189| 188| 187| 186| 185| 184| 183| 182| 181| 180| 179| 178| 177| 176| 175| 174| 173| 172| 171| 170| 169| 168| 167| 166| 165| 164| 163| 162| 161| 160| 159| 158| 157| 156| 155| 154| 153| 152| 151| 150| 149|
148
| 147| 146| 145| 144| 143| 142| 141| 140| 139| 138| 137| 136| 135| 134| 133| 132| 131| 130| 129| 128| 127| 126| 125| 124| 123| 122| 121| 120| 119| 118| 117| 116| 115| 114| 113| 112| 111| 110| 109| 108| 107| 106| 105| 104| 103| 102| 101| 100| 99| 98| 97| 96| 95| 94| 93| 92| 91| 90| 89| 88| 87| 86| 85| 84| 83| 82| 81| 80| 79| 78| 77| 76| 75| 74| 73| 72| 71| 70| 69| 68| 67| 66| 65| 64| 63| 62| 61| 60| 59| 58| 57| 56| 55| 54| 53| 52| 51| 50| 49| 48| 47| 46| 45| 44| 43| 42| 41| 40| 39| 38| 37| 36| 35| 34| 33| 32| 31| 30| 29| 28| 27| 26| 25| 24| 23| 22| 21| 20| 19| 18| 17| 16| 15| 14| 13| 12| 11| 10| 9| 8| 7| 6| 5| 4| 3| 2| 1|


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy