Interview Questions


JSP Error Page

In Web applications, it is generally a best practice to forward to an error page whenever an exception occurs.

The following JSP is a template for one such error page.


<%@ page language="java" isErrorPage="true" %>
<%@ taglib prefix="c"  uri="" %>


        <title>Error Page</title>

<body bgcolor="green">

        <div >
                <h3>Error Page</h3>
                <div >
                        This error page is only for debugging.

                <div >
                        <em>Error </em><br />
                        <% if (exception != null) { %>

How to configure Log4j for a Web application in JBoss?

To configure Log4j for a Web application in Jboss, perform the following.

1. Remove any file from your Web application.

2. Remove log4j.jar, common-logging.jar files from WEB-INF/lib directory in your Web application.

3. Add the following appender to /server/default/conf/jboss-log4j.xml

<appender name="TDD" class="org.jboss.logging.appender.RollingFileAppender">
       <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
       <param name="File" value="${jboss.server.log.dir}/TDD.log"/>

Card Game Using Java

Here is a simple game played with a pack of cards, usually by 2 or more players. Initially 'n' number of cards are distributed to all players. In each round, a player is given a chance to select a single card from his own set of cards. The player with maximum card number wins the round and gets a point. At last, player with most number of points wins the game. Friends, its time to play around in Java with cards. Happy Gaming!


public interface Game
        void playGame(int numberOfPlayers);

        void displayWinners();

ServiceLocator - To Lookup Datasource, EJB Beans

This code sample consist of ServiceLocator class which is a singleton class used for looking up DataSource, EJBLocalHome and EJBRemoteHome objects from JNDI context. This class can be used as a single point of contact to look up EJBHome, DataSource in enterprise applications. It also cache's those objects which are accessed for the first time, to prevent further lookup's in future.


Struts Tiles Integration

In this article let's discuss about integrating Tiles with Struts framework.

In a Struts based web application, Tiles framework is used for managing view (JSP) related information. In the Struts MVC architecture, when the request is returned from Action class, it is forwarded to Tiles framework with a defined name for the view specified in the tiles-defs.xml (tiles configuration file).

Have a look at the struts-config.xml file as follows.


<?xml version="1.0" encoding="ISO-8859-1" ?>

JAXB - Avoid converting < into &lt; and > into &gt; during Marshalling

In this article, let us see how to avoid converting < to &lt ; and > to &gt ; and & to &amp ; during JAXB Marshalling operation.

1. CharacterEscapeHandler creation
Create a custom Escape Handler by implementing the CharacterEscapeHandler interface as given below.

import com.sun.xml.bind.marshaller.CharacterEscapeHandler;

public class JaxbCharacterEscapeHandler implements CharacterEscapeHandler {

        public void escape(char[] buf, int start, int len, boolean isAttValue,
                        Writer out) throws IOException {

Java Square Root

This code sample contains methods to find a square root of both perfect and non perfect squares using java.

public class SquareRoot
        public static void main(String[] args)
                SquareRoot sR = new SquareRoot();
                System.out.println("Square Root of 100 -> " + sR.getPerfectSquareRoot(100));
                System.out.println("Square Root of 51 -> " + sR.getSquareRoot(51, 0.025f));

        public float getPerfectSquareRoot(int num)
                return getSquareRoot(num, 0.0f);

        public float getSquareRoot(int num, float errorFactor)
                float r = 0;

                float x = num / 2;

Java Prime Number

This code sample contains methods to generate Prime numbers in a given range using Java.

import java.util.ArrayList;
import java.util.List;

public class PrimeNumber
        public static void main(String[] args)
                PrimeNumber pN = new PrimeNumber();
                System.out.println("Is 73 a prime number -> " + pN.isPrime(73));
                System.out.println("List of prime numbers up to 200 -> " + pN.getListOfPrimes(200));

        public boolean isPrime(int n)
        int k = (int) Math.sqrt(n);
                for (int i = 2; i <= k; i++)
                        if (n % i == 0)
                                return false;

Send Bulk SMS using JSMPP

This article contains code examples to send sms to a huge set of subscribers. The input of MSISDN can be provided through a file as comma separated values. The class BulkSMSSender reads the input file of comma separated MSISDN and in turn initiates a pool of threads which executes sendTextMessage() method of SendSMSImpl class. Also there is a JSMPPServer class which acts an SMSC Simulator. It should be run first, which will listen to a port (example- 7001). The SendSMSImpl class sends text message to that port in which the JSMPPServer listens.

JAXB - Generate Stubs from XSD

This article describes the method of generating the JAXB Stubs from the XML schema using ANT build.

Consider the following ANT build xml which reads the XML schema from "C:\Sample.xsd" and generates the JAXB stubs in the "C:\jaxbSrc" location.

<?xml version="1.0" encoding="UTF-8" ?>

<project basedir="." default="build" name="Jaxb-StubGenerationDemo">

        <taskdef name="xjc" classname="">
            <fileset dir="C:\lib\" includes="jaxb-xjc.jar" />

    <target name="build">