Wednesday, July 16, 2008

How do you generate a PDF file from a JSP Java Server Page over the web?

I often get asked how to generate a PDF on the fly and deliver it to a client. Essentially, you need a tool that takes text and does the PDF generation, something like Apache FOP (Formatting Objects Processor) or iText, and then send a link to the generated file back to the client. When the client clicks on the link, the PDF file is displayed.

I guess you could wait a bit and embed the generated pdf file in the current JSP page and send that page back to the client also...That would work if the pdf can be generated on the sever quickly enough.

Here's a post from Bill Brogden from the JavaRanch:

Converting HTML to PDF Portable Dococument Format on the Fly within a JSP

"You can't generate PDF from a JSP template. You will have to generate it with a specific PDF generating tool such as FOP or iText and give a link to the binary data in a link to a servlet that can formulate the response correctly. Remember, JSP is for HTML formatted character data, binary data should be handled by servlets."



Which Jobs and Sun Certifications are Hot in the Java IT Industry?

I originally posted this on my website that deals with how to get a job in the IT industry, specifically the Java industry, but I thought the graphs showing the various certificaiton trends was equally valid for this blog as well. Here's the link to the original posting:

How to Get A Career in the Java Industry: IT, J2EE & JEE5 Stuff

So, which Java certification is the best one to get? That's a question that people are always asking me and debating. So, I figured I'd go to the job posting board and graph the demand for the various Sun Java certifications from various job want ads. The results are pretty surprising:

SCJA vs. SCJP vs. SCEA vs. SCWCD vs. SCBCD vs. SCDJWS vs. SCMAD Job Trends

As you can see, the demand for SCEA professionals is sky high. Makes me wonder if there's a second meaning to SCEA of which I don't know? I've always said that SCEA was the most respected of all of the Sun and Java certifications, and the demand for that particular designation demonstrates just that.

Of course, taking SCEA out of the graph, you get a slightly different picture of current Java skills demand in the industry:

SCJA vs. SCJP vs. SCEA vs. SCWCD vs. SCBCD vs. SCDJWS vs. SCMAD Skills Trends


As you can see, SCJP demand is high, as you would expect, with it being a requirement for all other exams except the SCJA and SCEA exams. SCJA demand is obvioulsly lower than many due to the fact that it is a new exam, and employers are only starting to ask for it, but you can clearly see that demand is rising.



SCWCD demand is strong, as web development is always a priority. EJB certification demand has its ups and downs, and web services certification seems to be going down. Of course, the fact that there hasn't been a web services certification book ever released can't help things.

And sadly, micro-device certification, SCMAD, is nascent. That's too bad. :(

Tuesday, July 15, 2008

unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details Vector Hashmap Hashtable

C:\_jdk1.6\bin>javac HashTest.java
Note: HashTest.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

If you get something like this error message when running your code, it's because you're not using generics, and you're not safely putting stuff into your collection classes.

If you actually have solid code that should compile, just use the -Xlint option. That's an ell (l) after the X, not a one (1)

C:\_jdk1.6\bin>javac HashTest.java -Xlint

SCJP 6 HashMap, Hashtable & hashCode examples equals comparisons sorting put get

Always focus your SCJP learning based upon the exam objectives: SCJP Exam Objectives

Section 6: Distinguish between correct and incorrect overrides of corresponding hashCode and equals methods, and explain the difference between == and the equals method.

What is the difference between a java.util.HashMap and a java.util.Hashtable?

a) HashMap permits null keys
b) Hashtable permits null keys
c) HashMap is unsynchronized
d) Hasttable is unsynchronized

Options a) and c) are correct.

You could consider the HashMap the lazier of the two collection classes being identified here. The HashMap allows both null objects and null keys. Furthermore, access to the elements in the HashMap are not synchronized, as they are in the HashTable.


#################################################

For a HashMap, a small load factor would:

a) cause a HashMap to increase in size more often
b) cause a HashMap to increase in size less often
c) cause a HashMap to manage larger object graphs more efficiently
d) cause a HashMap to manage larger object graphs less efficiently

Option a) is correct.

With a small load factor, a HashMap would have smaller increment sizes, and as a result, would increase in size more often as elements are added to it.

#####################################################

The number of buckets a HashMap contains when it is created is known as:

a) initial load factor
b) load factor
c) initial capacity factor
d) initial capacity

Option d) is correct.

The initial capacity is the term that is used to describe how large a HashMap will be, in terms of empty buckets, when it is initially created.

"An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the capacity is roughly doubled by calling the rehash method. "


##########################################################

Given the following code, what would be the result of running the HashTest class?

public class HashTest{

public static void main (String args[]){

java.util.Hashtable hashtable = new java.util.Hashtable();

hashtable.put("one", new Entity(1));
hashtable.put("two", new Entity(2));
hashtable.put("three", new Entity(3));

System.out.println(hashtable.size());
}
}


/**** Entity Class ****/


class Entity{

int id;

Entity(int id) {
this.id = id;
}

public int hashCode() {
return id;
}

}


a) 0 is printed to the console
b) 1 is printed to the console
c) 2 is printed to the console
d) 3 is printed to the console
e) compile time error
f) runtime error

Option d) is correct.

Three unique entities are placed into the HashMap, each with a unique key value. As a result, when we ask how many objects are stored in the HashMap, we get the number three in return.

###########################################################


Given the following code, what would be the result of running the HashTest class?

public class HashTest{

public static void main (String args[]){

java.util.Hashtable hashtable = new java.util.Hashtable();

hashtable.put(new Entity(1), new Entity(1));
hashtable.put(new Entity(2), new Entity(2));
hashtable.put(new Entity(3), new Entity(3));


System.out.println(hashtable.size());


}

}


/**** Entity Class ****/


class Entity{

public final int CODE = 007;

int id;

Entity(int id) {
this.id = id;
}

public int hashCode() {
return CODE;
}

public boolean equals() {
return true;
}

}

a) 0 is printed to the console
b) 1 is printed to the console
c) 2 is printed to the console
d) 3 is printed to the console
e) compile time error
f) runtime error

Option d) is correct.

The equals method is not overridden properly.


##########################################################


Given the following code, what would be the result of running the HashTest class?


public class HashTest{

public static void main (String args[]){

java.util.Hashtable hashtable = new java.util.Hashtable();

hashtable.put(new Entity(1), new Entity(1));
hashtable.put(new Entity(2), new Entity(2));
hashtable.put(new Entity(3), new Entity(3));


System.out.println(hashtable.size());


}

}



/**** Entity Class ****/


class Entity{

public final int CODE = 007;

int id;

Entity(int id) {
this.id = id;
}

public int hashCode() {
return CODE;
}

public boolean equals(Object o) {
return true;
}

}


a) 0 is printed to the console
b) 1 is printed to the console
c) 2 is printed to the console
d) 3 is printed to the console
e) compile time error
f) runtime error

Option b) is correct.

In this case, the value returned for console display is 1.

When placing an object into a HashMap, the JVM looks to see if an object with the given identified, or key, has been already added. If an object is added with a key that already exists, the object in the 'bucket' or 'place' associated with that key is essentially overwritten.

To figure out if the key being provided is already in use within the HashMap, the JVM calls the hashcode method of the key, looking for a match. From there, the equals method is called.

Since all instances of the entity class return the same value for the hashcode method, the JVM thinks all instances are pretty much the same. Furthermore, since .equals also returns true on every c

all, any secondary comparisons the JVM makes on the Entity key values return true as well. So, each time we use the entity class as a key, the JVM thinks we are just adding a replacement for the existing key, and as such, the current object held in the 'bucket' is replaced by the incoming one. In the end, in this example, there is only one entity in the Hashtable when we ask the Hashtable for its size.

################################################




public class HashTest{

public static void main (String args[]){

java.util.Hashtable hashtable = new java.util.Hashtable();

hashtable.put(new Entity(1), new Entity(1));
hashtable.put(new Entity(2), new Entity(2));
hashtable.put(new Entity(3), new Entity(3));


System.out.println(hashtable.size());


}

}


###################################################

Given the following code, what would be the result of running the HashTest class?



public class HashTest{

public static void main (String args[]){

java.util.Hashtable hashtable = new java.util.Hashtable();

hashtable.put(new Entity(1), new Entity(1));
hashtable.put(new Entity(2), new Entity(2));
hashtable.put(new Entity(3), new Entity(3));


System.out.println(hashtable.size());


}

}


/**** Entity Class ****/


class Entity{

public final int CODE = 007;

int id;

Entity(int id) {
this.id = id;
}

public int hashCode() {
return id;
}

public boolean equals(Object o) {
return true;
}

}



a) 0 is printed to the console
b) 1 is printed to the console
c) 2 is printed to the console
d) 3 is printed to the console
e) compile time error
f) runtime error


Option d) is correct.

In this case, the equals method always returns true, but if the hashcode method indicates that two instances are NOT the same, the equals method does not need to be called. As a result, the HashMap treats every Entity key as a unique entity or instance, despite the fact that all three instances would actually generate a true value when compared using the equals method.

Since the key values appear unique to the JVM using the HashMap and the hashcode method of the entities, each key being used by the HashMap is considered unique.


###################################################


Given the following code, what would be the result of running the HashTest class?




public class HashTest{

public static void main (String args[]){

java.util.Hashtable hashtable = new java.util.Hashtable();

hashtable.put(new Entity(1), new Entity(1));
hashtable.put(new Entity(2), new Entity(2));
hashtable.put(new Entity(3), new Entity(3));


System.out.println(hashtable.size());


}

}


/**** Entity Class ****/


class Entity{

public final int CODE = 007;

int id;

Entity(int id) {
this.id = id;
}

public int hashCode() {
return CODE;
}

public boolean equals(Object o) {
return false;
}

}


a) 0 is printed to the console
b) 1 is printed to the console
c) 2 is printed to the console
d) 3 is printed to the console
e) compile time error
f) runtime error

Option d) is correct.

Although the Hashcode value always returns the same number, the JVM will always call the equals method to absolutely confirm that two instances are exactly the same. Since the equals method always returns false, regardless of what the hashcode method returns, then every instance of the entity class is considered different from the other, even if we as cognizant human beings know that this is not the case.

##############################################



Given the following code, what would be the result of running the HashTest class?

public class HashTest{

public static void main (String args[]){

java.util.Hashtable hashtable = new java.util.Hashtable();

hashtable.put(new Entity(1), new Entity(1));
hashtable.put(new Entity(1), new Entity(1));
hashtable.put(new Entity(1), new Entity(1));


System.out.println(hashtable.size());


}

}


/**** Entity Class ****/


class Entity{

public final int CODE = 007;

int id;

Entity(int id) {
this.id = id;
}

public int hashCode() {
return CODE;
}

public boolean equals(Object o) {
return false;
}

}



a) 0 is printed to the console
b) 1 is printed to the console
c) 2 is printed to the console
d) 3 is printed to the console
e) compile time error
f) runtime error

Option d) is correct.

This is a nasty question, because it looks like we are always using the same key to keep track of instances. However, since the hashcode method always returns the same value, and the equals method always indicates that two instances are not the same, the HashMap thinks that every Entity key used in this example is different. As such, each object being added to the map is placed in a separate bucket.

Remember, it is important to always provide meaningful and proper implementations to the hashcode and equals methods when creating new classes. Not doing so can have unpredictable and unforseen consequences, especially when using various frameworks such as TopLink and Hibernate.


##############################################


Given the following code, what would be the result of running the HashTest class?

public class HashTest{

public static void main (String args[]){

java.util.Hashtable hashtable = new java.util.Hashtable();

hashtable.put(new Entity(1), new Entity(1));

Entity x = new Entity(1);
Entity y = new Entity(2);


System.out.println(x.equals(y));


}

}


/**** Entity Class ****/


class Entity{

public final int CODE = 007;

int id;

Entity(int id) {
this.id = id;
}

public int hashCode() {
return id;
}

public boolean equals(Object o) {
return true;
}

}



a) true is printed to the console
b) false is printed to the console
c) runtime error is thrown
d) compile time error is generated

Option a) is correct.

In this case, the equals method always returns true, so when we call the equals method, we get a value of true.

This sometimes confuses people, because various objects call the hashcode method first when comparing two entities. For example, in the case of the HashMap collection class, when the hashcode method returns two different numbers for two instances being compared, the instances are considered to be different, and the equals method is not invoked. Only when two instances produce the same hashcode are they then compared using the equals method.

As was mentioned, this type of comparison happens in various circumstances, making some people believe the answer to this question should be false. However, since we are calling the equals method directly, the hashcode method does not come into play at all, and we get the word true printed to the console.

Monday, July 14, 2008

is-a vs. has-a in Java. Extends vs instances in the SCJP 6 & SCJA Exams

Here's a goo mock SCJP 6.0 exam question on how associations and aggregations are implemented in Java code:

Programatically in Java, how would you express the fact that a cup sometimes has coffee in it?

a) public class Cup extends Coffee{}
b) public class Coffee extends Cup {}
c) public class Cup { Coffee coffee;}
d) public class Coffee { Cup cup; }

Option c) is correct.

The extends keyword expresses an is-a relationship, so Cup extends Coffee would imply that a Cup is Coffee, which doesn't really make sense.

When we declare instance variables within classes, we are expressing a has-a relationship. So, option c), where a cup has an instance of Coffee, would be best expressing the fact that a cup can sometimes contain coffee.

Option a) can be read as a Cup is Coffee
Option b) can be read as a Coffee is a Cup
Option c) can be read as a Cup has Coffee
Option d) can be read as a Coffee has a Cup

try..catch..finally SCJP 6 Sample Exam Questions (kewords & flow control too!)

Given the following Java code, what would be the result of compling and running the class?

public class JavaCertified extends Object {

public static void main(String args[]) {

long bow = 99;
int x = 100;

boolean flag = true;
boolean switch = false;

if (!flag) System.out.print(flag);
if (!switch) System.out.print(switch);

if (bow < x) {System.out.print("less");} else {System.out.print("more");}

}

}

a) The console would print trueless
b) The console would print falseless
c) The console would print truemore
d) the console would print falsemore
e) The code would not compile
f) The code would compile but not run


Option f) is the correct answer to this question.

The code will not run due to the fact that the keyword swtich is being used as a varible name.



***
Given the following Java code, what would be the result of compling and running the class?


public class JavaCertified extends Object {

public static void main(String args[]) {

long bow = 99;
int x = 100;

boolean flag = true;
boolean onSwitch = false;

if (!flag) System.out.print(flag);
if (!onSwitch) System.out.print(onSwitch);

if (bow < x) System.out.print("less"); else System.out.print("more");

}

}

Given the following Java code, what would be the result of compling and running the class?

public class JavaCertified extends Object {

public static void main(String args[]) {

long bow = 99; int x = 100;

boolean flag = true; boolean onSwitch = false;

if (!flag) System.out.print(flag);
if (!onSwitch) System.out.print(onSwitch);

if (bow < x) System.out.print("less"); else System.out.print("more");

}

}

a) The console would print trueless
b) The console would print falseless
c) The console would print truemore
d) the console would print falsemore
e) The code would not compile
f) The code would compile but not run

In this case, option b) is correct.

There are no tricks in this particular question, although the code, missing some of the formatting style that we expect from Java programs, looks a little bit peculiar. Nevertheless, the question is testing you on your ability to use the not operator, !, along with the ability to evaulate basic comparison operators, in this case, less than, <.

Since onSwitch is false, !onSwitch evaluates to true, so the value of onSwitch is printed to the console.

Furthermore, 99 is less than 100, so the word less is printed to the console after the word false.

Although the if..else block does not contain braces, as it really should in any good Java program, the fact remains that if the braces do not exist after an if or else block, the first, and only first, statement after the block is considered to make up the entire body of the block.

Sometimes, just due to the fact that the Sun Certification exam contents gets mangled as they go through various internationalizations, the syntax and formatting of the code will often violate Suns very own 'suggested coding standards.' Don't let non-standard formatting throw you off. If the code is good, and the syntax is solid, just concentrate on the contents of the question and answer it correctly.

Saturday, July 12, 2008

Google Search Engine Queries Leading to my Site

So, here are the latest search engine queries leading to my site.

Google has quickly and efficiently spidered my blog site, and included my latest postings in its index. The whole process has only taken days with blogger.com posts.

Seems like blogging is a good, solid way of getting top rankings with Google. The Search Engine Optimization experiment continues




So, h

09 Jul, Wed, 01:54:05
Google: when SCJP 6.0 books will be published?
09 Jul, Wed, 09:18:23 Google: scjp 6.0 mock exam book
09 Jul, Wed, 10:20:52 Google: cameron mckenzie
09 Jul, Wed, 10:44:24 Google: java 6 mock exam
09 Jul, Wed, 12:30:34 Google: scjp 6 mock exam
09 Jul, Wed, 13:25:24 Google: cameron mckenzie
09 Jul, Wed, 14:18:07 Google: Java 1.6 reserved words
09 Jul, Wed, 17:34:23 Google: Sun certified java associate opportunities
10 Jul, Thu, 14:25:41 Google: skip jpa annotation xdoclet
10 Jul, Thu, 15:49:49 Google: interview questions for entry level java programmer
11 Jul, Fri, 04:20:38 Google: Cameron's online mock exams
11 Jul, Fri, 05:19:22 Google: sun abstract class naming convention
11 Jul, Fri, 07:45:33 Google: hibernate add many-to-many association of existing object
11 Jul, Fri, 09:36:03 Google: hibernate annotations discriminator ignored in select
11 Jul, Fri, 18:16:56 Google: calling named query in jpa from hibernate mapping file
11 Jul, Fri, 18:29:13 Google: calling named query in jpa from hibernate mapping file
12 Jul, Sat, 01:29:17 Google: declaring variables in interfaces