2010/11/29

Formatting numbers

It´s possible format numbers and dates using the java.text API.

Formatting a number is pretty simple. We use the DecimalFormat class to specify a custom format:

NumberFormat formatter1 = new DecimalFormat("#,###");
System.out.println(formatter1.format(123123))  // displays 123,123

NumberFormat formatter2 = new DecimalFormat("0.00");
System.out.println(formatter2.format(123123))  // displays 123123.00

NumberFormat formatter3 = new DecimalFormat("#,##0.00");
System.out.println(formatter3.format(123123.123))  // displays 123,123.12

There are a lot of patterns you could use to format numbers.
Just take a look at http://download.oracle.com/javase/tutorial/i18n/format/decimalFormat.html

2010/11/28

Configuring a JSP error page

Hi guys.

It's awful to visit a web site and receive an error page that does not explain what the hell had happened. However it's very easy to configure a nice error page to be shown when an unexpected error happens.

1) Create a new JSP page, with the follow code:

<%@ page isErrorPage="true" import="java.io.*" %>
<html>
    <head>
        <title>Error Page</title>
    </head>

    <body>
        <h2>Unexpected error</h2>
        <p><b><%= exception.toString() %></b></p>
        <p><%=
            StringWriter sw = new StringWriter(); 
            PrintWriter pw = new PrintWriter(sw); 
            exception.printStackTrace(pw); 
            out.print(sw); 
            sw.close(); 
            pw.close();
        </p>
    </body>
</html>

Explaining the code:
  • The attribute errorPage=true identifies the JSP page as an error page
  • As an errorPage, an object called "exception" is made available. Its type is java.lang.Throwable
  • We used the method exception.toString() to return a description of the exception
  • We also used the method exception.printStackTrace to give more information about what happened
There are a lot of other usefull information that you may use in this error page. Just take a look at the java.lang.Throwable class.

2) Configure the application

Now that we have our error page, let's configure the application to call it when an unexpected exception happens. There are two ways to do that:

2a) Configure each JSP page to call the error page when an unexpected exception happens.

Include the attribute errorPage on the top of the page code:

<%@ page errorPage="errorPage.jsp" %>

2b) Configure the web.xml file to call the error page for the entire application

Use the tag error-page to configure a JSP page to be called according to the error code:

<error-page>
    <error-code>500</error-code>
    <location>errorPage.jsp</location>
</error-page>

Explaining the code:

  • The tag error-page tells the application server what to do when an error happens
  • The tag error-code specifies what error should be catch
  • The tag location specifies what page will be show when the error happens

Some common error codes are:

  • 404 - Page not found
  • 500 - Application error (exception)
  • You may find other code descriptions at this link

That's it! Now your application has a beautiful and useful error page.


2010/11/23

Using Google Code as a project repository

Hi guys.


What about using a free SVN host? And what about a Google one?
It's very easy to configure your project in Google Code SVN service. Just follow this steps (I'm using Eclipse Helios):
  1. First you must create your project at Google Code.
  2. Then install Subversion according to your Eclipse version.
  3. Now you are able to share your project. Just open your workspace, right click your project and select: Team > Share Project
  4. A new window will appear asking for a repository type. Just select "SVN" and click next.
  5. Select "Create a new repository location" and click next
  6. Enter your project URL (something like https://yourproject.googlecode.com/svn) and click next
  7. Select the option "Use project name as folder name" and click next
  8. Insert a comment (or just let the default one) and click finish
  9. Eclipse will ask to change to the Team Synchronizing perspective. Click yes.
  10. Right click your project in the Synchronize window and select: Commit...
  11. Insert a comment and click finish
Ok. Your project has just been shared.

Just a tip:
In order to check out your project, you must have a Google Code password. It's different from your Google account. You may find it at http://code.google.com/hosting/settings.