Filtering Ajax requests on session expiration

Do you need to detect and handle Ajax requests when the HTTP session has already expired? If that is so and you need a working implementation, here is a recipe for you. Enjoy…

Advertisements

Full Ajax Exception Handler

Take a look at this post regarding Ajax exception handling, from BalusC’s blog. Omnifaces has an interesting ajax exception handling… very cool and handy!

Java – are you sure you know how switch case works?

Three years ago, back when I was preparing for the Java programmer certification, I used to think I had a pretty good insight knowledge of my programming language. Although I realized that I wasn’t absolutely wrong, there were some things that I got surprised. One of them was the way that switch case really works. So, for instance, take a look at the following snippet, and try to predict what will be the result of executing the code (don’t peek at the solution without trying first the resolution of the code):

public class SwitchCaseTest {

  public static void main(String[] args) {
    int existingCase = 0;
    System.out.println("i = " + existingCase);
    doWithDefaultAtEnd(existingCase);
    doWithDefaultAtStart(existingCase);

    System.out.println();

    int nonExistingCase = 2;
    System.out.println("i = " + nonExistingCase);
    doWithDefaultAtEnd(nonExistingCase);
    doWithDefaultAtStart(nonExistingCase);
  }

  private static void doWithDefaultAtEnd(int i) {
    switch (i) {
      case 0:
        System.out.println("0");
        break;
      case 1:
        System.out.println("1");
        break;
      default:
        System.out.println("default@end");
    }
  }

  private static void doWithDefaultAtStart(int i) {
    switch (i) {
      default:
        System.out.println("default@start");
      case 0:
        System.out.println("0");
        break;
      case 1:
        System.out.println("1");
        break;
    }
  }
}

As you might have discovered by now, the basic difference resides in the fact of the positioning of the default case statement. When the default statement is at the beginning of method for which it gets a parameter that is not matched in any of the other case statements, what will happen? That was my doubt when I was studying Java for the certification… what about you? Do you know the answer? 🙂

Next, there’s the results of the execution of the previous class… was it what you were expecting? If so, congratulations. If not, don’t be sad because I’m pretty sure a lot of people (even those experienced in Java) would fail this, if they were asked… 😉

i = 0
0
0

i = 2
default@end
default@start
0

Recipe for using multilanguage strings in JPA

Lately, I have been working in an application that needs multilanguage contents stored in the database. We were using JPA 2.0 and we wanted a solution that would be generic and reusable in several other contexts. We came up with a solution that may be summarized in this recipe. Hope it fits your needs as well…

Primefaces hotkey trick

Do you need to conditionally use a keyboard key such as ‘esc’ to handle a given behaviour on a Web page such as cancelling the edition of a given page section? Are you facing any problems with multiple key bindings to that page? If that is the case, this is probably the right solution for you.