Saturday, 29 May 2010

Oracle Sun JDK vs OpenJDK

These days Ubuntu comes with Open JDK installed as standard. Many Java programmers replace this immediately with the Oracle Sun JDK.

So what's the difference? Well, the OpenJDK is a 100% open-source implementation of the Java language specification. The Oracle Sun JDK is largely open-source but still contains some precompiled binaries that Sun didn't have copyright to release under an open-source license.

You can read more here:

Oracle - Free and Open Source Java

I've always tended to replace the OpenJDK with Sun Java, but since installing Ubuntu 10.04 Lucid Lynx, I've been seeing how I get on with the OpenJDK. To date, developing straightforward Java, JSP and servlets with Spring, Struts2, Hibernate and Maven deployed on Tomcat, has worked perfectly well. I've also been using the IcedTea plugin in Firefox with no issues.

But yesterday, I hit my first snag. If you read my previous post you'll know that I've been looking into the new annotations in the Servlet 3.0 specification: @WebServlet, @WebFilter and @WebListener. To get examples to run I had to install Glassfish v3. All was well until I tried to run the Admin Console and I got this error:

java.lang.UnsupportedOperationException: Cannot create XMLStreamReader or XMLEventReader from a
        at org.jvnet.hk2.config.ConfigParser.parse(
        at org.jvnet.hk2.config.ConfigParser.parse(
        at org.jvnet.hk2.config.ConfigParser.parse(
        at org.glassfish.admingui.plugin.ConsolePluginService.init(
        at org.glassfish.admingui.plugin.ConsolePluginService.getIntegrationPoints(
        at org.glassfish.admingui.common.handlers.PluginHandlers.getIntegrationPoints(
        at org.glassfish.admingui.handlers.ThemeHandlers.getThemeFromIntegrationPoints(
        ... 47 more

That didn't worry me too much at the time, but I was also getting some odd behaviour with Java Server Faces (JSF) applications that I hadn't seen on Tomcat. Most of the time I'm able to switch freely between one and the other with web applications.

So, based on a forum post about the problems with the Glassfish admin console, I installed all the Sun Java 6 components. My problems with the admin console disappeared and my JSF application started behaving normally again. So, on that basis, there seem to be glitches with JSF using the current version of the OpenJDK. The Glassfish admin console is based on JSF.

If you need to replace your OpenJDK installation on Ubuntu 10.04, these are the steps:

  1. Enable the partner repositories: System -> Administration -> Software Sources and check the box for lucid partner in the Other Sources tab. You might also want the sources checked too.
  2. Install the Sun Java6 packages: sun-java6-jdk, sun-java6-jre, sun-java6-source, sun-java6-fonts, sun-java6-bin and possibly sun-java6-plugin.

You'll then need to switch your default Java to the Oracle Sun version:

$ sudo update-java-alternatives -v -s java-6-sun

Do also check that you haven't hard-coded OpenJDK into any JAVA_HOME variables or application settings.

Thursday, 27 May 2010

Servlet 3.0 Annotations

The specification for JSR315: Servlet 3.0 was released in December last year with some interesting new ideas and features. This is still very new and the application servers haven't quite caught up with it yet. At the time of writing, the only server I know of that supports Servlet 3.0 is Glassfish v3. Tomcat 7 will provide support but is apparently not due for release until toward the end of this year.

In this post, I want to take a brief look at the new annotations in Servlet 3.0, and show how, although not ground-breaking, they will reduce the amount of configuration in web.xml.

To run any of these examples, you will need a copy of Glassfish 3.0 or, if you are feeling adventurous, you could try Tomcat 7.0 RC2.

Getting hold of the Servlet 3.0 API

Assuming you are using Maven, you'll need to find yourself a repository artifact that corresponds to the new specification. The central repository now has the required artifacts, so no need to mess around adding repositories:


Note you may also find an artifact called javaee-web-api. As far as I can tell from a cursory glance, this contains the same classes as javaee-api.

Servlet with Annotations

Servlet 3.0 provides the @WebServlet annotation to define a servlet, obviating the need for configuration in web.xml.
package blogger;

import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(value="/sampleServlet", loadOnStartup=1)
public class SampleServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.getWriter().write(new Date().toString());


Simply create the class, deploy the project and invoke the servlet using the path specified in the annotation, e.g. http://localhost:8080/<contextPath>/sampleServlet. Note that things like load on startup can be specified for servlets using the annotation.

Listener with Annotations

Listeners can be defined in much the same way. Again, no configuration is required in web.xml.
package blogger;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SampleListener implements HttpSessionListener {

    private static final Logger log = LoggerFactory.getLogger(SampleListener.class);

    public void sessionCreated(HttpSessionEvent arg0) {"Session created");

    public void sessionDestroyed(HttpSessionEvent arg0) {"Session destroyed");


I've used SLF4J for logging here, which is becoming more popular as a logging framework wrapper, used by Spring 3.0 for example. To make this work, you'll need to add the following to your pom.xml. Note there's no need to add an artifact for Log4J.





To configure the underlying Log4J, you'll need something like this copied out to your classpath:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//LOGGER" "log4j.dtd">

<log4j:configuration xmlns:log4j="">

    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="target" value="system.out"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %d{ABSOLUTE} %c{1} - %m%n"/>

        <priority value="info"/>
        <appender-ref ref="console" />


Filter with Annotations

Same idea again here. No configuration in web.xml.

package blogger;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SampleFilter implements Filter {

    Logger log = LoggerFactory.getLogger(SampleFilter.class);

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
        chain.doFilter(request, response);
        if (response instanceof HttpServletResponse) {
  "Applying cache control filter to response");
            HttpServletResponse httpServletResponse = (HttpServletResponse)response;
            httpServletResponse.setHeader("Cache-Control", "nocache");

    public void init(FilterConfig filterConfig) throws ServletException {}

    public void destroy() {}


In this case, the parameter in the @WebFilter annotation specifies the paths to which the filter is applied. In my case, just to the sample servlet above.

One of the limitations of the annotations for filters is that there is no way of specifying the ordering. If multiple filters were in use, and the ordering was important, you'd have to revert to a schema-based configuration in web.xml.

Web Deployment Descriptor

None of the above examples need a web.xml file. By default, the welcome file list includes index.html and index.jsp. If you need to configure things like session timeouts, prelude/coda, etc., you will need a web.xml file. This is an example of a basic Servlet 3.0 version:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=""

Settings in web.xml, by the way, can also be used to override the settings provided by the annotations. The web.xml takes precedence. You can also set metadata-complete to true in web.xml to tell the container only to consider configuration in web.xml and skip annotations altogether.


So, there you have it. Servlet 3.0 annotations. As I said in the introduction, they aren't ground-breaking in terms of setting up your application, but they do make it slightly easier to configure servlets, filters and listeners in your web applications. I suspect 99% of the time, we will still need a web.xml file, but it could be less cluttered.

Monday, 3 May 2010

In Praise of Twitter

To be honest, I always thought Twitter was a way for teenagers to amuse themselves by inflicting incomprehensible text-speak globally, instead of just upon their friends ... lol

But with the new release of Ubuntu incorporating the "social networking" application Gwibber, I thought it time to see what the fuss is all about.

Social networking aside, I have to say, it's a great way for people working in technology to keep tabs on what's going on. We all know how quickly things move and how it's often difficult to find time to keep up to date with websites like SpringSource and Hibernate. Well Twitter's text-speak-sized "tweets" are nice little bite-sized chunks of information and, more importantly, are regularly spat out in the name of publicity by the organisations we are interested in ... Apache Software Foundation, Hibernate, SpringSource, Oracle, etc, etc.

Certainly much easier to digest than irritating emails that are quarter-disclaimer and quarter-virus-check. Easy to ignore, easy to read and easy to pass on the juicy bit to your contacts. (You'll notice the new Twitter updates section in the blog).

Go on, do yourself a favour. Check out Twitter. You might be surprised how useful it, and similar "social networking" sites, can be.