How to blogs and code snippets - Java/JSF

Building Jersey 2.x RESTful Web services in Servlet 3 container with Maven

(Posted Monday June 20, 2016 16:02:28)
Building Jersey 2.x RESTful Web services in Servlet 3 container with Maven

This is our implementation of Jersey 2.23.1 RESTful web services running on Tomcat 8's Servlet 3.1. It is our practice to write a blog as way to document a new feature available in our servers. We use NetBeans 8.1 for our IDE.

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

(Posted Thursday March 07, 2013 00:57:25)

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.

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

(Posted Friday February 08, 2013 16:40:40)

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.

Generating XML RSS 2 feeds with JDOM 2 with a servlet

(Posted Thursday January 31, 2013 20:03:52)

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).

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

(Posted Friday December 21, 2012 22:55:02)

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.

Implementing a collapsible ui:repeat rows in JSF

(Posted Wednesday December 19, 2012 20:35:45)

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 />

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

(Posted Tuesday December 04, 2012 15:33:33)

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.

Download and crop an image direct from a servlet

(Posted Tuesday November 13, 2012 22:10:45)

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.

SSH tunneling with Java, a database connection example

(Posted Wednesday November 07, 2012 21:59:02)
SSH tunnel

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.

Asynchronous tasks in JSF applications

(Posted Wednesday October 17, 2012 01:27:23)

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.

Using Primefaces PrimePush with Tomcat 7

(Posted Thursday October 04, 2012 19:17:05)

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).

Speed up Primefaces page load with p:remoteCommand partial update

(Posted Thursday June 07, 2012 02:41:37)
Primefaces JSF

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.

Write your own URL rewrite filter for existing JSF pages

(Posted Saturday April 07, 2012 23:49:39)

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.

How to get Google +1 pluses count in Java

(Posted Sunday April 01, 2012 16:28:40)

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.