Monday, April 23, 2007

Quiclky Create a Maven-aware project

If you are (or want to start) using Maven as a build tool for your project, a quick way to start is to use an archetype to create a pom.xml and the default folder structure. This is the command:

mvn archetype:create






Friday, April 13, 2007

Debugging Information passed to a JSP Page

Well, scriptlets inside a JSP page Suck. Alright, I agree!!
But sometimes it is necessary to check what are the parameters in the request or session scopes reaching your jsp page.

I found this code somewhere on the web sometime ago. One day (yeah, right) I will right a version of it using JSTL tags, but for now I allow myself to add this scriptlet in one of the pages that are included everywhere in my system. As I use Struts Tiles in my system, I do have a bottom.jsp page which is included everywhere.

So I leave the debug attribute in false state until I need to debug on page or another. At this moment, all I have to do is turn on the debug information in my page by setting the attribute to true.

Below is the scriptlet and page includes that should be added to the page where you want the debug information to be displayed.

<%@ taglib prefix="c" uri="" %>

<%@ page import="java.util.*" %>
<%@ page import="org.apache.struts.*" %>

<%@ page import="org.apache.struts.util.*" %>
<%@ page import="org.apache.struts.action.*" %>

// Print all attributes in the request object
out.println("<p><b>All Attributes in request scope:</b>");
Enumeration paramNames = request.getAttributeNames();
while (paramNames.hasMoreElements()) {
String name = (String) paramNames.nextElement();
Object values = request.getAttribute(name);
out.println("<br> " + name + ":" + values);

// Print all attributes in the session object
out.println("<p><b>All Attributes in session scope:</b>");
paramNames = session.getAttributeNames();
while (paramNames.hasMoreElements()) {
String name = (String) paramNames.nextElement();
Object values = session.getAttribute(name);
out.println("<br> " + name + ":" + values);

out.println("<p><b>Data in ActionMessages:</b>");

// Get the ActionMessages
Object o = request.getAttribute(Globals.MESSAGE_KEY);
if (o != null) {
ActionMessages ae = (ActionMessages)o;

// Get the locale and message resources bundle
Locale locale =
MessageResources messages =

// Loop thru all the labels in the ActionMessage's
for (Iterator i =; i.hasNext();) {
String property = (String);
out.println("<br>property " + property + ": ");

// Get all messages for this label
for (Iterator it = ae.get(property); it.hasNext();) {
ActionMessage a = (ActionMessage);
String key = a.getKey();
Object[] values = a.getValues();
out.println(" [key=" + key +
", message=" +
messages.getMessage(locale,key,values) +

Tuesday, April 10, 2007

EJB3 QL: Using the COUNT function

If you are using EJB3 Query Language to build your application, you will probably need to use some functions other than the regular SELECT statements and logical WHERE clauses. One of the most simplest of these functions is the COUNT function.
The syntax for the COUNT function is very simple, and it receives only a parameter which is the identifier to be count, as in:


This query will count all the Customers who live in Brazil.

The COUNT function can be used with an identifier, in which case it always counts entities (as the example above demonstrates), or with path expressions but this last one can always be converted into an expression that counts entities only by managing the conditions in the WHERE clause.

Below is an example on how you could write a piece of code that would count the Patients from a medical database, depending on which Clinic they are registered to:

Query query = entityManager.createQuery("SELECT COUNT (p) FROM Patients p WHERE = :idtClinic");
query.setParameter("idtClinic", idtClinic);
return (Long)query.getSingleResult();

I will write about other EJB QL functions later! :)