Posts tagged with: java

2012-12-15

PHP's nl2br equivalent in JSPs

Some of PHP’s built-in functions can be quite handy. One of these is nl2br, which transforms newlines (\n and other variants) into HTML line breaks (<br>) — more precisely, it puts a <br> before each newline.
It has been used, and sometimes is still used, when echoing a CLOB on a webpage.

Actually, this function is no longer necessary: with the CSS property white-space you can do the same on the client-side in a neat and efficient way. However, this post is just an example on how to create something resembling PHP’s built-in functions using the instruments of Java EE. Read more...

Tags: java, php
2012-12-25

Wait until any thread terminates

In Java programming, it’s quite common to start a pool of threads and wait for all threads to terminate: this can be achieved in several ways, the most simple of which is the one of “joining” all the threads.

However, there are situations in which you may want to wait for any thread to terminate, i.e. stop waiting when at least one thread terminates — and abort all the others.
You may want to have such a behaviour when you have several threads working on the same problem (for example, a search problem) and you want to stop all threads when at least one of them finds a solution. Read more...

Tags: java
2013-02-28

Tutorial: exposing RESTful web services using EJB and Hibernate/JPA under JBoss AS

Some people think that Enterprise Java Beans (EJBs) deployed on EJB containers (Glassfish, JBoss, etc...) are “complicated” whereas RESTful web services deployed on servlet containers (e.g. Tomcat) are “simple”, and the two things cannot go together.

Actually, you can take the best of the two worlds, by exposing your EJBs with a REST interface. This allows you to exploit all the capabilities of an Enterprise Application Server (dependency injection, container-managed distributed transactions, etc...) while making life simpler for your clients, and without the need of shipping tons of libraries (e.g. Jersey, Hibernate, etc...) with your WAR.

In this tutorial we’ll create a web application interfaced with a MySQL database via Hibernate/JPA that runs in a EJB container (JBoss AS 7.1) and exposes a RESTful web service. Read more...

Fork me on GitHub