Interview Questions

Java

How to find the number of users logged in a web application?

To find the count of number of users logged in a web application, we can use HttpSessionListener.

Create a listener class as follows

SessionCounter.java

package in.techdive.http;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class SessionCounter implements HttpSessionListener
{
        private static int count;

        public SessionCounter()
        {
        }

        public void sessionCreated(HttpSessionEvent arg0)
        {
                count++;

Java Publishing RSS Feeds

In this article let's discuss about how to publish an RSS feed in java using Rome API.

What is RSS?

RSS(Really Simple Syndication) is a xml format that lets you pull content from another Web site and display it using an RSS feed reader in a standard format.
An RSS document includes full or summarized text, metadata such as publishing dates usually in an xml format.

Send Ringtone Using JSMPP

In this article let's discuss about how to send ring tone sms using jsmpp.
It is very similar to the one we used in the article Send SMS Using JSMPP, except for UDH.

What is UDH?
User Data Header is a header message to be appended before the actual message.
UDH is not required for sending text sms. It is only required for sending binary message.

UDH description is as follows
1. 06 - length of whole UDH i.e. 6 octet, now each octet has two char so 050415811581 count to 6.

Java - Password Generator

The following class is used for generating passwords containing 'n' characters.

PwdGenerator.java

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF

Send long SMS message using JSMPP

We have already seen how to send a SMS using JSMPP in the article Send SMS Using JSMPP. Since the length of a text SMS is 140, in order to send a long message containing more than 140 characters, we need to split the message and send with some headers to identify each message which is split.

Java - Random Number Generator

The following class contains utility methods to generate random numbers. The code is self explanatory with necessary comments.

RandomNumberGenerator.java

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR

Abstract Class and Interface

An Abstract class in java is a class for which objects cannot be created. It has the following properties.

1. It may contain zero or more abstract methods.
2. Abstract methods should be preceded with the keyword abstract.
3. It may or may not contain concrete methods.
4. Abstract classes cannot be instantiated. You cannot create objects for abstract class.
5. The concrete subclass which extends the abstract class, should implement all the abstract methods present in that extended abstract class.
6. It can have both private and public constructors.

Java Base64 Encoding

Base64Encoding.java

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS

XML Parsing using JDOM

JDOM is simply a Java representation of an XML document. It provides a way to represent that document for efficient parsing, manipulation, and writing. It has a straightforward API, which is optimized for the Java programmer. It's an alternative to DOM and SAX, although it integrates well with both DOM and SAX. It also integrates with other xml parsers like xerces.

The following class has methods for creating and parsing an xml document using JDom.

XMLJDomParser.java

package in.techdive.java;

import java.io.BufferedOutputStream;

Java Bean to XML conversion

In this article lets discuss about how to convert a Java bean to XML document.

Have look at the following Java bean.

Device.java

public class Device
{

        public Device(long deviceId, String deviceName, String productId, String serialNo)
        {
                super();
                this.deviceId = deviceId;
                this.deviceName = deviceName;
                this.productId = productId;
                this.serialNo = serialNo;
        }

        public Device()
        {

        }

        private long    deviceId;
        private String  deviceName;
        private String  productId;
        private String  serialNo;

        /**