Interview Questions, Tips and Tricks, Tutorial

Singleton in Java

What is Singleton?

Singleton is a class that cannot have more than one instance.

In Java, there are many way to create singleton class, one of the example is:

class SingletonClass {

    private static volatile SingletonClass INSTANCE = null;

    private SingletonClass() {   }

    public SingletonClass getInstance() {
         if (INSTANCE == null) {
            synchronized (SingletonClass.class) {
                 if (INSTANCE == null) {
                    INSTANCE = new SingletonClass();
         return INSTANCE;

When to use singleton?

Singleton is used when you need system wide “global value” that need to be accessed by many part of systems. Example: Session Value, Logger, Cache, etc

Information Technology, Tips and Tricks

Change JSESSIONID in URL parameter

Having problem with JSESSIONID in URL ?

Well, this is not a bug. When the server received a request from client, it is not sure whether the client support cookie or not. So, it generate both of them in the URL. When it comes back for second time, and the server detects the cookie is present, it will drop JSESSIONID from URL automatically. However, when the cookie is not present, the server will continue to use it.

Having JSESSIONID in URL actually is not bad at all, however it has a few drawbacks:

  • Security Risk

By exposing SESSIONID in the URL, it allows attacker to attack victim and steal some information store in the session

  • SEO

Because SESSIONID is unique, search engine both may not recognise different pages that having unique SESSIONID.

How to solve this?

There are few ways to avoid JSESSIONID in URL

  1. Implementing Servlet Filter which will disable/skip URL based on SESSION ID generation.
  2. Since Servlet 3.0, we can use SessionTrackingMode Enumeration to specify how SessionTracking should be.
    So, in web.xml we just need to add the following:


So, who said it is difficult and complicated? Happy coding 🙂

Big Data, Information Technology, Tips and Tricks

Install MongoDB on CentOS 6.4

MongoDB can be easily to be installed on CentOS 6.4 (64 bits) with with the following 2 steps:

  1. Add/Create 10 gen repo into the system#> vi /etc/yum.repos.d/10gen.repo

    name=10gen Repository

  2. Install Packages#> yum install mongo-10gen mongo-10gen-server

Note: You may need to be root to do the installation.

Once the download complete, your MongoDB is ready to be used. You can start/stop MongoDB instance with the following command:

# >  service mongod start
# >  service mongod stop

and, to restart

# > service mongod start

You may want to let the MongoDB auto start when the system up, with the following command:

#> chkconfig mongod on

Now, your MongoDB is installed and ready to use. You can start to using MongoDB shell, with the following command:

# > mongo

Happy trying and using with it.


Information Technology, Tips and Tricks

Steps to create MySQL datasource in JBoss 7 (not jdbc 4 compliant driver)

  1. Create the directory structure for the MySQL module.
    Example: how to create a module for MySQL driver:

    cd $JBOSS_HOME
    mkdir modules/com/mysql
    mkdir modules/com/mysql/jdbc
    mkdir modules/com/mysql/jdbc/main

  2. Copy the driver(s) jar into the created directory
  3. Create a module.xml file to define dependencies and resources. For our mysql example it looks like this:
    jboss:module:1.0" name="com.mysql.jdbc"

    Note: replace the JAR name accordingly with the version that you have in your local machine

  4. Add driver to the standalone.xml
                        <driver name="h2" module="com.h2database.h2">
    		    <driver name="mysql" module="com.mysql.jdbc"> 
  5. Restart your JBoss
  6. Go to JBoss console http://localhost:8080/console, select “Connector –> DataSources” from menu on the left, and then click button “Add” on the top right corner. Afterwards, follow the steps and you will that “mysql” is now available as driver option.
Information Technology, Tips and Tricks

Progressive Enhancement

One of consideration used in web development architecture/web design is called “Progressive Enhancement“. It is a simple but very powerful technique. It defines layers of compatibility that allow any user to access the basic content, services, and functionality of a web and providing and enhanced experience for browser with better support of standards.

Progressive Enhancement consists of the following core principles:

  • Basic Content must be available/accessible to all browsers
  • Basic Functionality must be available/accessible to all browsers
  • Semantic Markup contains all content
  • Enhanced layout is provided by externally linked CSS
  • Enhanced behavior is provided by unobtrusive, externally linked JavaScript
  • End user browser preferences are respected

So, the objective is to have one only code that is compatible with all devices :D.


Information Technology, Tips and Tricks

Using Multiple “Submit” button in a single HTML Form without Javascript (For Mobile Browser)

When developing a web application, you might encounter a situation that you need to develop a single form with multiple submit button, and you want to know which submit button that user choose when submitting the form. As an example:

<input type=”submit” id=”submit” value = “ADD”>
<input type=”submit” id=”submit” value = “EDIT”>

How will you solve this problem?

For those who are familiar with JavaScript, it should not be a big deal, because most of the people will use JavaScript to solve this problem. Commonly, they will use onClick event attached to each submit button, so when the button get clicked, it will call a method that will assign certain value to the hidden field. In their backend, they just need to retrieve the value from this hidden field.

Well, using JavaScript will not give you any issue if you are developing a desktop web application. All desktop browser support JavaScript. The problem will only raise if your application will run on mobile browser? As we know, not all mobile browser support JavaScript .. specially old type, and more and more people are using their mobile browser now… so we need something that works across all the browser 🙂

So how do we solve this problem? Here is another trick, that actually is very easy:

<input type=”submit” name=”submit” id=”submit” value = “ADD”>
<input type=”submit” name=”submit” id=”submit” value = “EDIT”>

and on your backend servlet

String whichButtonClicked = request.getParameter("submit");

The trick here is giving the attribute “name” on your submit button. According to the HTML Form specification, the “name/value” pair will be sent during form submission 😀

Happy trying 😀