Writing Servlets the J2EE 6 / Servlet 3.0 way

In the past whenever you wrote a Servlet you had a lot of work to do. First you wrote your servlet, then you had to add configuration for that servlet into web.xml so that your application would use it.

For simple applications this was fine but the more servlets you wrote the harder it became as web.xml started to become large and unwieldy.

Now with J2EE 6 you got the ability to add annotations to your servlets. No more do you need to add anything to web.xml as the container scans the classpath for any servlet that’s annotated with @WebServlet getting the required configuration from there.

This also has the benefit that you can write libraries of servlets. Those servlets get deployed automatically and you don’t have to worry about duplicating the config.

Both Tomcat 7 and TomEE 1.5.1 support the J2EE 6 Web Profile as standard. I’m not certain of other containers as I’ve only tested this on those two.

Continue reading “Writing Servlets the J2EE 6 / Servlet 3.0 way”

The problem of mixing library versions

Usually you wouldn’t mix versions. Build environments like Maven should handle this for you – although even maven can get things wrong.

Yesterday I was playing with Apache TomEE looking at migrating some webapp’s away from Apache Tomcat 7. The impetus for this was to get Rest services working and one of the TomEE profiles supports JAX-RS rest services. Tomcat only supports JAX-WS as thats in Java EE6 Web profile.

Anyhow, with the theory that the webapp’s should just run with minor config changes to the container (declaring datasources, that sort of thing) I installed TomEE and got Netbeans to deploy to it.

Bang!

None of the apps would deploy. They all complained about slf4j missing a method:

java.lang.NoSuchMethodError: org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;[Ljava/lang/Object;Ljava/lang/Throwable;)V
    at org.apache.commons.logging.impl.SLF4JLocationAwareLog.debug(SLF4JLocationAwareLog.java:133)
    at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager$1.getConnection(ThreadSafeClientConnManager.java:221)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:401)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)

Now if you ever see this, it’s down to having mixed versions of slf4j in your classpath – and unlike Tomcat, TomEE uses the most recent version of slf4j.

The solution

Many hours later I find this article where someone had a similar problem last year: SLF4J Dependencies and LocationAwareLogger.log Method Not Found Exception.

Now my webapps also use the latest standalone tiles library for layout so after getting maven to show me the dependency tree (mvn dependency:tree) on the webapp it showed that I was pulling in an old slf4j version into the webapp.

So, with a minor change to the pom telling maven to exclude slf4j it finally worked.

Here’s the changes I ended up making to the pom:

<dependency>
  <groupId>org.apache.tiles</groupId>
  <artifactId>tiles-extras</artifactId>
  <!-- needed to exclude slf4j which causes incompatibilities -->
  <exclusions>
    <exclusion>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-nop</artifactId>
    </exclusion>
    <exclusion>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
    </exclusion>
    <exclusion>
      <groupId>org.slf4j</groupId>
      <artifactId>jcl-over-slf4j</artifactId>
    </exclusion>
  </exclusions>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <scope>provided</scope>
</dependency>

The first dependency is for tiles. The <version/> element is missing as I declare that in the project’s root pom and ensures all webapps use the same version.

The important bit is the <exclusions/> element, here we tell maven to exclude those artifacts from the war. There’s one or more <exclusion/> elements which contain just the <groupId/> and <artifactId/> elements – no <version/> here.

The second dependency is for slf4j. It’s here to allow any code provided by the war to have access to it. Again <version/> is missing as its in the root pom but we provide a <scope/> of provided. This tells maven it can use it for compiling but does not include it in the war.

Finally in the project’s other modules I just make sure that any pom with a reference to slf4j is also declared as provided so they don’t cause maven to include it either.

That’s about it. It only took me about 4 hours of hunting around to find the underlying cause. In the Windows world this is known as DLL Hell – well it can happen to any OS/Environment & it is hell when it strikes.

Notes:

  • If you use another logging library within your webapp then that shouldn’t be affected. log4j should just work, this problem appears just to be for slf4j.
  • If you use apache commons logging then you can declare that as provided if you wanted. This is because TomEE provides it at the container level as well as slf4j.

Setting up a Weather WebCam on Linux

Weather webcams are always popular and it is easy and free to set one up yourself.  This article will show how to setup a simple USB webcam to produce still images and serve them on a local apache webserver.

Continue reading “Setting up a Weather WebCam on Linux”

Google Nexus 7 3G 32Gb

On Tuesday Google made available their new tablets so I ordered the new Nexus 7 3G (which also had 32Gb of storage). I needed 3G as my existing MIFI dongle was on it’s last legs so needed something to replace it & one less item to carry is a good thing.

Anyhow ordered it around 3pm and Google said 3-5 business days for delivery. No problem I’m not in a rush for it.

3pm the following day, one brand new tablet in my hands 🙂

So I’ve been playing with the thing for two days & I have to say I’m well pleased with it. Batter life is good, 3G reception is brilliant (actually better signal than my phone) and it’s even downloading faster.

So expect a few more articles about the nexus over the next few weeks.

But as a start, the Nexus 7 3G has Android 4.2 on it – in fact the first thing mine did when I plugged it into wifi was to download and install an update for it. The downside is that the developer tools button isn’t present anymore – perhaps non-developers were accidentally selecting it.

Well, the option is still there, just hidden, in a developer sort of way. To turn on the developers you need to convince the tablet you are one.

  1. Open settings
  2. Press About Tablet
  3. Press Build Number. Nothing happens.
  4. Keep on pressing Build Number, quickly.
  5. After a short while you’ll see a message along the lines of “you are 3 clicks away from being a developer” which will count down.
  6. You’ll end up with a message “You are now a developer” & the tools will be present.

Alan Turing on £10 note petition has had a response

The e-petition ‘Put Alan Turing on the next £10 note’ signed by you recently reached 21,996 signatures and a response has been made to it as the e-petition has received more than 10 000 signatures:

The Bank of England has been including historic characters on its notes since 1970. The Bank welcomes suggestions from members of the public for individuals who might feature on future banknotes, and publishes a list of these suggestions on its website. These suggestions inform the process when a new note is under consideration.

The mathematician and cryptographer Alan Turing features on the list which can be found at: http://www.bankofengland.co.uk/banknotes/Documents/about/banknote_names.pdf

This e-petition remains open to signatures and will be considered for debate by the Backbench Business Committee should it pass the 100 000 signature threshold.

You can add to the petition here.

 

Who voted yesterday in the badger cull debate

Yesterday parliament had a back bencher debate on the badger cull after the online e-petition gained over 150,000 signatures against the culling of badgers in an effort to combat bovine tuberculosis.

The following list are the MP’s who voted at the end of the debate. Those who voted ayes we those who are against the cull (the motion was about stopping the cull).

For most the noes list are the ones of interest, it’s those MP’s who are for a cull.
Continue reading “Who voted yesterday in the badger cull debate”

The Badger Cull is to be debated next week

After getting over 157,000 people signing an e-petition about the Badger Cull, it’s going to be debated in a weeks time, Thursday October 25th.

Earlier the speaker of the house announced next week’s order of business with the following entry: (emphasis is mine)

Thursday 25 October—Presentation of a report by the International Development Select Committee: DFID’s work in Afghanistan. This is expected to last 20 minutes. It will be followed by a debate on a motion relating to the badger cull. The subject for this debate has been nominated by the Backbench Business Committee.

© Parliamentary material is reproduced with the permission of the Controller of HMSO on behalf of Parliament. Licence No: P2011000006