The personal and professional ramblings of Nicholas Chase
Looking for help with one of my books? Try here. Like science fiction? Check out the
Vanguard Science Fiction Report. Looking for XML information? Try the XML Reference Guide. How about Native American books, music, videos, and so on? Try The Native Path.
"Problems cannot be solved at the same level of awareness that created
them." -- Albert Einstein
November 14, 2005
A Java book that might be fun
As you may know if you read this blog, I've got my fingers in more pies than ... well, than I've got fingers. So it's nice when I can have fun and learn something useful. Java™ After Hours: 10 Projects You'll Never Do at Work looks like one of those opportunities. Anybody read it? Recommendations? The reviews, at least, are very good... Ah, for more spare time! (OK, I'll settle for any spare time...)
Technorati tags: Java
0-672-32747-3
Posted by roadnick at
05:06 PM
|
Comments ()
|
TrackBack
May 13, 2005
Programmer How-to additions
I just realized it's been a month since I mentioned new additions to the Programmer How-To. Here's what you've missed:
Posted by roadnick at
01:04 AM
|
Comments ()
|
TrackBack
April 14, 2005
More programmer how-to additions
A couple of new additions to the Programmer How-To:
Technorati Tags: Linux | System Administration | Programming | PHP |
Posted by roadnick at
02:08 PM
|
Comments ()
|
TrackBack
April 05, 2005
New additions to the Programmer How-To
Apparently I've been remiss in pointing to new additions to the Programmer How-To. I've added the following items:
Technorati tags: Python | programming | how-to | how to | Linux
Posted by roadnick at
12:34 PM
|
Comments ()
|
TrackBack
March 08, 2005
Today's Hubble image
I've actually run out of room on this page for new stuff, no matter how fun I think it is. Like Today's Hubble Image:
I have no idea what you're seeing when you look at it; it changes periodically. But at least I've got it here for when I figure out where to put it. Here's the code, as described by HubbleSite:
<a href="http://hubblesite.org/newscenter/"><img src=
"http://hubblesite.org/newscenter/latest_thumb.php" width="80" height="80" alt="Latest News from HubbleSite" border="0" /></a>
Technorati tag: Hubble | space
Posted by roadnick at
08:06 PM
|
Comments ()
|
TrackBack
February 19, 2005
Jython and numeric directory names
After getting some inspiration from Sean McGrath, I've decided to expand the Programmer How-To a little bit, and add tips for those niggling things that people just generally spend hours and hours on. This first one involves Jython and numeric directory names when importing code.
Sean's right, though; if we all documented these things when we figured them out, we might not spend so much time floundering around pulling our hair out.
Posted by roadnick at
04:43 PM
|
Comments ()
|
TrackBack
December 23, 2004
Programmer How-To additions
OK, I've made some additions to the Programmer How-To:
I've also made it (I hope) a little bit clearer that when you add a tip, you can add a link back to the page on which it originally appeared, so maybe some of you might have a little incentive to participate...
Posted by roadnick at
04:16 PM
|
Comments ()
|
TrackBack
October 02, 2004
Programming tip: PHP objects as values
OK, here's something that's been holding me up for a little while. When you create an object in PHP and set it as a member of a second class, PHP actually creates a copy, rather than passing a reference to the original object. Any changes that you make to the original or to the copy do not get shared between them. To see that work, try running this script:
<?php
class Object1{
var $theObject;
function setObject($newObject){
$this->theObject = $newObject;
}
function getObject(){
return $this->theObject;
}
}
class Object2{
var $theValue;
function setValue($newValue){
$this->theValue = $newValue;
}
function getValue(){
return $this->theValue;
}
}
$object1 = new Object1;
$object2 = new Object2;
$object2->setValue("One");
$object1->setObject($object2);
$tempObject = $object1->getObject();
echo "Value of embedded object is "
echo $tempObject->getValue()."</p>";
$object2->setValue("Two");
echo "<p>Set value of object2 to "
echo $object2->getValue()."</p>";
$tempObject = object1->getObject();
echo "Value of embedded object is "
echo $tempObject->getValue()."</p>";
?>
You should see a result of:
Value of embedded object is One
Set value of object2 to Two
Value of embedded object is One
As a corrolary to that, that means that there's a difference between:
function setObjectValue($newValue){
$this->theObject->setValue($newValue);
}
and
function setObjectValue($newValue){
$tempobject = $this->theObject;
$tempobject->setValue($newValue);
}
Something to keep in mind.
[NOTE: In trying to figure something else out, I've run across a way to pass a variable by reference:
function myfunction(&$varname)
but that doesn't seem to help in this case.]
[NOTE on the NOTE: Problem solved by Jeffrey Taylor. Check out the comments for the solution.]
Posted by roadnick at
05:18 PM
|
Comments ()
|
TrackBack
August 18, 2004
Deleting locked files or folders
This isn't really a "programming" tip, but it's a little tidbit I'm glad to have found, having been plagued in the past with Files That Would Not Die. Corey Collins explains How to Remove "locked" Folders/Files on his blog:
First, open a command window and go to the directory where the illegal files and folders are. Type in dir /x. This will list the files with a truncated 8 character dos name. The files will now have a ~1 after them, they will only have legal characters in them, and only be 8 characters long. Now, type rmdir /s baddir~1. This will delete the directory and everything inside of it. This can be a little time consuming if there are a lot of files, but it works.
Posted by roadnick at
11:16 PM
|
Comments ()
|
TrackBack
August 12, 2004
Automatic browser redirects
Ever been to a page that says, "We've moved! You'll be redirected in 20 seconds," and then 20 seconds later (or thereabouts) a new page appears without your having clicked anything? Here's how it's done.
At the top of your page, add a "meta" tag. This tag passes information about the page to the browser. for example, if I wanted to redirect you to this blog's home page after 30 seconds, I'd use the tag:
<META HTTP-EQUIV="refresh" content="30;URL=http://www.nicholaschase.com/blog">
Here's what that does. First it tells the browser to refresh, or reload the page. The content attribute tells it when -- in this case, after 30 seconds -- and where to go when it does. When the browser refreshes, it will go to the specified URL.
You can also use this for simply refreshing a page by adding its own URL. Just make sure you set the delay high enough for the page to be useable!
Posted by roadnick at
12:03 AM
|
Comments ()
|
TrackBack
August 10, 2004
Overriding Java's base classes
> Hi Nicholas
>
> First I want to commend you for the excellent book. I was trying to run the sample on using Traversal on the DOM according to the DOM 2.0 recommendation. I am using Java SDK 1.4.2_03. I have attached the source code of the program I am using. I am running on a Windows XP laptop
>
> I am getting an error message
>
> C:\JXML\test17>java ShowDocument
>
> supported
>
> Exception in thread "main" java.lang.ClassCastException
>
> at ShowDocument.main(ShowDocument.java:45)
>
> Regards and Thanks - Ranjan
Hi, Ranjan!
The problem may be related to a bug that made it into Java's XML implementation. Java looks at its base classes, and then the classpath, so just replacing the offending classes on the classpath won't help. To solve the problem, download Xerces from xml.apache.org and use the "Endorsed standards" mechanism to make Java look at it first. To do that, you'll need to set the -Djava.endorsed.dirs switch. For
example, say your xerces.jar, xmlParserApis.jar, or whatever jars come with Xerces these days, are in C:\Xerces\lib. You would call your Java program using:
java -Djava.endorsed.dirs=C:\Xerces\lib ShowDocument
Please let me know if this solves your problem, and thanks for the kind words!
---- Nick
[NOTE: It turned out to be a permissions problem on the database, but the tip still stands. :) ---- Nick]
Posted by roadnick at
10:50 AM
|
Comments ()
|
TrackBack
July 22, 2004
Sending and receiving JavaMail
Need to send or recieve email using Java? Try this out, using the JavaMail API, available at http://java.sun.com/products/javamail:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class MailDemo {
public static void main (String[] args){
getMessages();
}
public static void getMessages(){
Properties props = System.getProperties();
Session session = Session.getDefaultInstance( props );
try {
String host = "www.yourserver.com";
String username = "acmessages";
String password = "mypassword";
Store store = session.getStore("pop3");
store.connect(host, username, password);
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
if (messages.length > 0){
Message[] messages = folder.getMessages();
MimeMessage thisMessage = (MimeMessage)messages[0];
System.out.println(thisMessage.getContent());
sendMessage("System event notification",thisMessage.getContent().toString());
thisMessage.setFlag(Flags.Flag.DELETED, true);
}
folder.close(true);
store.close();
} catch (Exception e){
e.printStackTrace();
}
}
public static void sendMessage(String subject, String content){
Properties props = System.getProperties();
props.put( "mail.transport.protocol", "smtp" );
props.put( "mail.smtp.host", "www.yourserver.com" );
Session session = Session.getDefaultInstance( props );
Message msg = new MimeMessage( session );
String from = "sender@example.com";
String to = "recipient@example.com";
try {
msg.setFrom( new InternetAddress( from ) );
msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( to ) );
msg.setSubject(subject);
msg.setText(content);
Transport.send( msg );
} catch (Exception e){
e.printStackTrace();
}
}
}
Posted by roadnick at
05:05 PM
|
Comments ()
|
TrackBack
July 12, 2004
Add Flash to an HTML page
Tearing your hair out trying to add Flash to a web page and make it work in different browsers? Apparently you need to use both OBJECT and EMBED tag syntax. Why? Because IE uses the object tag and Netscape uses Embed. For example:
<OBJECT
classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase=
"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"
WIDTH="550" HEIGHT="400" id="myMovieName">
<PARAM NAME="movie" VALUE="myFlashMovie.swf">
<PARAM NAME="quality" VALUE="high">
<PARAM NAME="bgcolor" VALUE="#FFFFFF">
<EMBED src="myFlashMovie.swf" quality="high" bgcolor="#FFFFFF"
WIDTH="550" HEIGHT="400" NAME="myMovieName" ALIGN=""
TYPE="application/x-shockwave-flash"
PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">
</EMBED>
</OBJECT>
For those not familiar with the object tag, if it can't display its own content (ie, the movie value) it displays the content of the element (ie, the embed tag).
Posted by roadnick at
03:05 AM
|
Comments ()
|
TrackBack
July 05, 2004
Filling the browser window
Running into problems filling your browser window? Apparently just setting the width and height to 100% doesn't always work, particularly in Mozilla derivative Firefox. Keith Peters explains over at BIT-101 that it's because of how the browser answers the question: "100% of what?"
The swf is in the body tag. The body tag is in the html tag, which is in the browser window. Most browsers will scale the html size to fill the browser window and scale the body to fill the html size. Not so with Mozilla/Firefox. It doesn't assume that html fills 100% of the browswer, or that body fills 100% of the html. So you have to tell it that.
Keith also provides a quick CSS workaround:
body,html {
margin:0px;
padding:0px;
height:100%;
}
(BTW, if you haven't visited BIT-101, you should, even if you don't develop in Flash.)
Posted by roadnick at
01:00 PM
|
Comments ()
|
TrackBack
June 30, 2004
File uploads in PHP
Writing a PHP page and need to accept files from a form? Here's the skinny, according to the PHP Manual entry on Handling file uploads:
First, you will of course need the HTML form:
<form enctype="multipart/form-data" action="_URL_"
method="post">
<input type="hidden" name="MAX_FILE_SIZE"
value="30000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
Then you'll need to save the file in the PHP page:
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES
//should be used instead
// of $_FILES.
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . $_FILES['userfile']['name'];
print "<pre>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'],
$uploadfile)) {
print "File is valid, and was successfully uploaded. ";
print "Here's some more debugging info:\n";
print_r($_FILES);
} else {
print "Possible file upload attack! Here's some ";
print "debugging info:\n";
print_r($_FILES);
}
print "</pre>";
?>
Posted by roadnick at
11:47 AM
|
Comments ()
|
TrackBack
June 28, 2004
Manipulating time and dates in Java
Need to get a certain date or time -- other than right now? Here's a hint: don't use java.util.Date. Use java.util.GregorianCalendar instead. For example, if you needed a date object that represented the moment twenty four hours ago:
java.util.GregorianCalendar lastAcceptable =
new java.util.GregorianCalendar();
lastAcceptable.add(java.util.Calendar.HOUR, -24);
You can use any of the constants defined in java.util.Calendar to add or subtract time from a date. Check out the GregorianCalendar API for more information on manipulating and retreiving dates and times.
Posted by roadnick at
03:08 PM
|
Comments ()
|
TrackBack
June 27, 2004
How to format a date in Java
Creating a java.util.Date gives you the date all right, but if you need to format it in a particular way, say, for a database query, you need to use a formatter, such as the SimpleDateFormat class. For example, this code turns today's date into the string "06-27-2004":
java.util.Date theDate = new java.util.Date;
java.text.SimpleDateFormat dateFormatter =
new java.text.SimpleDateFormat("MM-dd-yyyy");
String theFormattedDate = dateFormatter.format(theDate);
You can format it any way that you like. Check out Customizing Formats for more pattern options.
Posted by roadnick at
02:56 PM
|
Comments ()
|
TrackBack