Java and JSF howto blogs and code snippets

Using Google Custom Search engine in PrimeFaces Overlay Panels

The following is an example on how to implement a custom Google Search engine in PrimeFaces version 4 <p:overlayPanel />. First create an account with Google Custom search (CSE) here You will get your own Custom Search engine ID. From within your CSE account you will find a place to generate the following basic CSE Javascript code. Place this code in the head section or in the body just before the closing </body> tag, whichever is prefered.

Read more

Using Rhinoslider with image and youtube content in JSF pages

Rhinoslider demo   
Rhinoslider sample.   
Rhinoslider is a beautiful JQuery slider plugin that can handle images, video and other HTML content with many custom options. Using the plugin in HTML pages is quite simple and straightforward. Their website even has a generator that will give you a code and start customizing the plugin from there. Here in this blog, we will demostrate how to integrate this plugin on images and youtube videos into a JSF page based on version 1.05 of the plugin. We will provide 2 options of embedding the plugin. Live demo is available in the home page with this site.

Listing 1 below, is a sample generated page. Please make sure that items from 6 to 11 of the snippet are accessible from the page itself. These items are part of the plugin download. If using PrimeFaces, you may want to remove lines 6 to 8. Also lines 10 and 11 are optional and are controlled by the values of "easing" and "controlsMousewheel" parameters of the plugin (see Listing 7).

Read more

A dynamic standard sitemap.xml with Google image extension implemented as a Java Servlet

A sitemap.xml generated on demand is very helpful for websites that has lots of pages and with pages that are added frequently. According to Google, sitemaps help their crawlers find pages from websites. This is true for new websites or for pages that has fewer links to them.

The servlet herein described is based on protocol with Google image extension. We used JDOM2 library to create the XML sitemap structure. Below is how a standard sitemap looks like with the Google image extension. Please note that image extension is supported only by Google. Bing/Yahoo ignores them.

Read more

Creating a Facebook-like panel with slim scrollbars and infinite scrolling in PrimeFaces

Some of the Facebook panels with slim scrollbars and infinite scrolling are those non full-page panels used to display notifications and messages. These are activated by button-icons in the upper left corner of your timeline. In this blog, we will demonstrate how to create similar panels using slimScroll jQuery plugin in PrimeFaces.

Please note that the first section of this blog (slimScroll panel without the infinite scrolling) also works for standard JSF projects.

Read more

Generating XML RSS 2 feeds with JDOM 2 with a servlet

To better demonstrate how to generate XML RSS 2 feeds, we will use a servlet with JDOM 2 library. In this short blog, we will generate only the required elements plus a few options for feed reader interoperability. Please refer to the RSS 2 documentation for more of the optional elements and to learn more about the RSS 2 specifications. JDOM 2 is available for download here

Below is a sample RSS 2 feed generated by the servlet that follows further down (contents edited for clarity).

Read more

Building a page with infinite scroll in PrimeFaces using Waypoints jQuery plugin

Infinite scroll is a page feature where in data is automatically loaded as you scroll down to the bottom of a page. Facebook uses this feature in their "news feeds".

This demonstration uses PrimeFaces with a jQuery plugin called WayPoints.  Plugin download and documentation can be found here waypoints/.  We employed PrimeFaces' <p:remoteCommand /> and <p:outputPanel /> along with Waypoints, the main component of the process.   It is Waypoints that initiates the update process that call <p:remoteCommand />, and in turn into the backing code, as soon as a Waypoint element appears at the bottom of the page/viewport.

Read more

Implementing a collapsible ui:repeat rows in JSF

Here is simple implementation of a JSF <ui:repeat /> component with collapsible rows with the aid of JQuery.   It is very simple, no specials tricks, except that the components inside <ui:repeat /> must have a unique ID (or class) which is handled by the backing code.  Toggling the visibility of the frame (<div />) is taken cared of by the onclick event on the link, in this case <h:outputLink />

Read more

Validating an email address in PrimeFaces p:inputText field with p:ajax

The example that follows demonstrates how to validate an email address inputted from a PrimeFaces <p:inputText /> field.  It validates the correctness of the email address as well as its existence.  PrimeFaces version 3.4.1 was used to test this demonstration.

Read more

Download and crop an image direct from a servlet

This blog is a demonstration on how to download an image and crop them directly just before the servlet writes the image into the response. This is useful in instances where you don't want the downloaded image saved before they are consumed.

The following is that servlet, it takes two parameters, the url of the image, and the image type. Add parameters as your application may require. Please refer to the inline comments for more information.

Read more

SSH tunneling with Java, a database connection example

This blog is about using SSH tunnel in Java through jsch, a Java an implementation of SSH2.   As an example, we connect to an MySQL installation in a remote host behind a firewall which not accessible from the outside.   What an SSH tunnel does is forward accesses from a local port to a remote port on a remote host and transfer data in a secure manner.

First download jsch here and add it to your project.    The only requirements are,   you have to have an SSH account on the remote host,  and for the database example,    a database account with a "connect" permission.

Read more

Asynchronous tasks in JSF applications

This is an example on how to implement concurrency in JSF applications using the Executor framework.

We used, in our example below, java.util.concurrent.ThreadPoolExecutor as a static instance that is created and destroyed from the init and destroy events listened to by javax.servlet.ServletContextListener of our application. ThreadPoolExecutor execute threads from a queue of Runnable or java.util.concurrent.Callable using an unbounded java.util.concurrent.LinkedBlockingQueue that holds the tasks submitted by clients for execution.

Read more

Using Primefaces PrimePush with Tomcat 7

PrimePush comes bundled with PrimeFaces.  It uses Atmosphere,  a WebSocket/Comet framework for asynchronous server-browser communications.   Atmosphere however does not come with PrimeFaces out of the box.   You need add to your project the necessary Atmosphere runtime libraries.   In this blog  we will show you how to get going with PrimePush in PrimeFaces 3.4.1 for use with Tomcat 7 (tested with Tomcat 7.0.27).

Read more

Speed up Primefaces page load with p:remoteCommand partial update

Consider a Primefaces page with many components and one of the components in the page takes time to load may be because it loads a lot of data or the backing code of the component connects to slow servers. As a result the entire page is blocked and not visible to the user until that one component loads completely.

We overcome this scenario by using Primefaces' p:remoteCommand.

Read more

Write your own URL rewrite filter for existing JSF pages

This blog is based on our simple URL rewrite implementation for our existing JSF/PrimeFaces pages. As a demonstration, suppose you have an existing page with following URL pattern.

Read more

How to get Google +1 pluses count in Java

Here is a simple way to manually get the Google +1 pluses count for your web pages using Java. This is based on a PHP implementation from this blog post.

Read more

Using Tomcat JDBC Connection Pool as standalone instantiated bean

The Tomcat JDBC Connection Pool org.apache.tomcat.jdbc.pool is described as a replacement or alternative to the older commons-dbcp connection pool. It can be used either through a JDNI resource or standalone as instantiated bean outside of the container.

Tomcat JDBC (tomcat-jdbc.jar) is part of the Tomcat installation and depends on tomcat-juli.jar, which is also a standard Tomcat library, for logging.

Read more

Custom error pages for Tomcat JSF applications

In this blog we will demonstrate how and where to catch Tomcat application page access errors and display custom error pages to users.

Read more

Using Google Places API to search for places around a location in JSF/PrimeFaces

The use of Google Places is simple, you only need your Google API credentials and the end point URL of the API. The only additional task your application has to do is get the current location of the user. In this demonstration, we will use Google Latitude API for the user location.

Although we tested this demonstration only in desktop environment, the procedure should work for mobile websites as well.

Read more

A Core Java Server Faces (JSF) RSS feed reader

This is short demonstration on how to create a simple RSS feed reader in core Java Server Faces (JSF), implemented in a backing bean. The required libraries are Rome, and JDOM.

Read more

How to add tweet callback notification with Twitter Javascript Events in PrimeFaces pages

In this blog we will show you how to add notification when someone clicked on a tweet button in your PrimeFaces page using Twitter's Web Intents Javascript Events. A tweet button referred here is a share or follow button.

This blog is based on this Twitter documentation.

Read more

Building a mobile website with JSF 2 core and JQuery Mobile 1.0

This is to share our experience with the JQuery Mobile 1.0 used with Mojarra JSF 2.1.3. The link below points to the mobile website described in this blog. (This website has been updated to JQM version 1.3.2)

Read more

How to use Google implementation of OAuth 2.0 protocol to Access its APIs, Google Latitude example

This blog is about using the Google implementation of OAuth 2.0 authentication protocol using Java in JSF environment. Google refers to this process as Web server flow authentication. We will use Google Latitude as an example.

Read more

How to use Google URL Shortener service in JSF pages

Google URL shortener service is available free for up to 1,000,000 queries per day against shortened URLS of an account. Is it recommended to use an API key when using the service specially when shortening calls to the API are through an application. You may register to the service in this Google site. Visit the Google URL shortener service here to shorten a URL interactively.

We will demonstrate in this blog how to use Google URL shortener in JSF projects for use with Twitter share data-url. This blog assumes that you already have an API key.

Read more

How to use Google Translate's Text to Speech (TTS) services in your web page using Servlet

This is complete working servlet that demonstrates how to use Google Translate's Text to Speech services. Google Translate TTS is able to translate text to clear voice for many different languages.

Read more

Using and customizing Orbit image slider plugin for JSF/PrimeFaces pages

Orbit image slider JQuery plugin comes with many customizable features. It looks and feels similar to PrimeFaces galleria component. We will explore most of these features for integration to your JSF pages and also for possible replacement or option to PrimeFaces galleria component. See our home page to demo what we describe in this blog.

Read more

How to use Facebook comments system, manage events notifications and SEO boost using PrimeFaces/JSF

Here is how to use Facebook comments system for your web pages, subscribe to Facebook events notifications using PrimeFaces' remoteCommand component and boost your SEO chances by incorporating comments text directly into your PrimeFaces/JSF web pages. This is based on Facebook's documentation and partly based on our experience.

Read more

How to add a system try icon with bubble message to Java application

The system tray is an area in your desktop where currently running processes with associated tray icon are shown and accessed. Adding a tray icon to programs are specially useful in long running applications where status changes during its execution. Program status can be visually communicated to the user through updatable tray icons and/or bubble messages attached to the tray icon.

Read more

A safer way to code SQL queries in Java

This is not to say that the suggested code in this blog will block SQL injection attacks. What we have below is a generally accepted safer way to code SQL queries in Java.

Read more

My experience with some of the new Java 7 features

I recently upgraded my development and production environment to Java 7. I made no changes to my code at first. All running as it was before. Today I noticed that my Netbeans editor is showing some hints in some lines that needed Java 7 conversion. So I started coding changes for Java 7.

Read more

Content slide show using PrimeFaces carousel component

This is a working example on how to use PrimeFaces carousel component to do a slide show with a group of components. We call our group of components as "content". In this blog we use version 3.0.RC1-SNAPSHOT of the component library. This is a very simple guide but it will help you get started.

Read more

Dynamically generate PDF documents with pictures, hyperlinks and more from your JSF project using servlets

The servlet below is a complete working code that dynamically generates a PDF document. It will create the PDF document add pictures, hyperlinks and more. Our servlet uses iText PDF library for Java. Download your copy here and add it to your project library. This guide is suitable for use in your JSF project.

Read more

Loading an image or video from file system into web application pages - A servlet option

Here is how to load media files residing in your server file system outside of your webapps folder. This option specially helps if location of media files are not web accessible.

Read more

Encrypting plain text using Java Cryptography Extension

One way or another your project needs a string encrypter. From encrypting passwords saved in text files to encrypting entire files, this class is a handy companion.

Read more

A Java class for sending multipart Email messages through your Gmail account

Here is a Java class for sending multipart (text and file attachments) email messages through your Gmail account. This class is suitable for use in your JSF backing bean.

JavaMail library must be in your project. Get JavaMail here.

Read more

Using JCaptcha as an alternate to Recaptcha for your JSF project

If you are looking for a basic Recaptcha alternative, here it is. Start by getting a copy of JCaptcha library here. Add the library to your project. You will also need the Apache collections and logging library.

Our JCaptcha will need a Servlet, a Service, and Image Engine. Don't worry they are all small classes. Our service is a singleton as recommended by JCaptcha. The service creates our image engine. The service is instantiated by our servlet.

Sample outputs available near the bottom of this blog.

Read more

Using partial page update with PrimeFaces/JSF

This record has been deleted.

Below is a related article on PrimeFaces partial update.

Read more

List Facebook friends and groups post to their walls with graph API in JSF/PrimeFaces the simple way

Before we continue please make sure you already have a Facebook App and an access_token. Please visit here on how to get an access_token from your JSF application with the necessary access permissions. Please visit here to learn more about Facebook access permissions.

Read more

Implementing Facebook server flow authentication in Java/JSF

This is a Java implementation of Facebook's server flow authentication, documented in PHP here. Start by creating a Facebook App to get an APP_ID and APP_SECRET. You will need these credentials to authenticate your App when accessing Facebook data through API calls. An access_token is returned by Facebook for authentication requests. The goal of this demonstration is to obtain this access_token.

Read more

Changing domain names, redirect each JSF page to corresponding page in new domain using a JSF Filter

When changing domain names, Google recommends that redirection is done page by page in order to preserve page search rank. Below is how to redirect pages of a Java Server Faces (JSF) application to corresponding pages of a new domain name using a JSF Filter.

Read more