Properties -- be aware the evil whitespaces!

Just a letter to my future self: When you use Properties and creates the entries, remember to avoid the whitespaces. The properties are inserted as-they-are, so no trimming occurs. It might sound trivial, but it wasted at least an hour of my time, while I was searching for a bug in a totally wrong department… Pure evil: Properties p = new Properties; p.put("java.naming.factory.initial", " org.apache.openejb.client.LocalInitialContextFactory");

May 19, 2011 · 1 min

Java EE 6 SCWCD Mock Exam

The mock exam has been moved to a separate application working on the OpenShift and is available at exam.piotrnowicki.com. This post will be still available because of the comments – at this point the exam simulator doesn’t have the commenting functionality so you can leave your opinion here. In time it’ll be modified and will allow to post comments directly next to the questions. If you’d like to get more information about the exam migration take a look at this post: New OCE WCD Exam Simulator....

March 27, 2011 · 1 min

Using Servlets 3.0 ServletContainerInitializer

Servlets 3.0 in Java EE 6, brings new interface called ServletContainerInitializer. The name is very self-explanatory, but the question is – how it’s different from the ServletContextListener? Well, technically, the ServletContainerInitializer is not a listener – it is an initializer and it’s executed before any servlet context will even be ready. You can use this initializer to do some programmatic servlet/filters/listeners addition (which is also possible in Servlets 3.0!). The ServletContainerInitializer....

March 20, 2011 · 2 min

Defining JSP version for Tag Files with implicit TLD

If you want to use new features of JSP >= 2.1 and EL, like deferred binding (the #{…} construction in EL), you’ll find that you cannot achieve it within a Tag File with implicit TLD generation. It is simply because the implicit TLD defines that the Tag File is using JSPs in version 2.0. How to solve it? There are two possible solutions: deliver an explicit TLD for your Tag Files, modify the implicit TLD form....

February 20, 2011 · 2 min

Precision and scale in BigDecimal and MathContext

Lately, after publishing the previous post, a friend of mine passed me a snipped of BigDecimal code. He was wondering, why the following execution: BigDecimal a = new BigDecimal(2.9); BigDecimal b = new BigDecimal(4000); MathContext mc = new MathContext(2, RoundingMode.HALF_DOWN); BigDecimal r1 = a.multiply(b, mc); BigDecimal r2 = a.multiply(b).setScale(2, RoundingMode.HALF_DOWN); System.out.println("r1 = " + r1); System.out.println("r2 = " + r2); is returning different values for r1 and r2: r1 = 1....

February 5, 2011 · 2 min

Float and double in Java "inaccurate" result

There is a well known problem with precise floating point numbers representation in a computer, and Java is no different in this case. If you want to see it for yourself, than try executing the below code: Double d = new Double(4.395f); System.out.println(d); You might expect the 4.395 will be printed out, right? Well, you’re defining an “exact” number as a float (32 bit), than casting it to the double (64 bit) so there should be no problem, as it shouldn’t affect the value....

February 4, 2011 · 3 min

Eclipse Go to... interface method implementation

In Eclipse you can open a method definition if you point the method name (its invocation), and hit ctrl + LMB. You can also achieve the same if you select the method call and hit F3, and probably in few other ways. Try to use this feature on the interface type… What’s annoying is that when you use this shortcut on a method which uses an interface type rather than the implementing class, you are forwarded to the interface method declaration....

January 31, 2011 · 1 min

Executing SSH commands from ANT

Sometimes, when building an application, there is a need for some SSH command execution on the remote server. An example of such command could be: Redeployment of an application, Server restart, Temporary files removal, Custom script execution. Have no fear – SSHExec is here! You can easily achieve remote command invocation, using an ANT Task sshexec. An example of its usage could be this snippet (note that remote.* variables are taken from the *....

January 31, 2011 · 1 min

Accessing web application resources from different context

You can access the resources which are available for one servlet context from the other. Let’s say you have two web applications deployed (in the same Servlet container): /myApp1 /myApp2 Basically, if you’re in /myApp1 and you will execute the getServletContext() method on ServletRequest object, it will return the ServletContext for the application related with this ServletRequest object (in this case – /myApp1). With the ServletContext object you can execute getResource(-) or getResourceAsStream(-) to get access to the needed resource from within the /myapp1 application....

January 27, 2011 · 2 min

JavaBeans -- getters with Boolean type

When you want to create a boolean property in a Java Bean (let’s name it visible), you most likely would want to use a boolean type variable and create accessors for it. Well, if you use a boolean primitive it is allowed to create a getter in one of two manners: public boolean getVisible() or public boolean isVisible() Notice that the second option is only allowed when your property is a boolean primitive....

January 13, 2011 · 1 min