Interview Questions

Java

JSP - Custom Tag Development

In this article, lets discuss about custom tag development in java based web application.

Custom tags can be created by the developer when, the tags provided by JSTL or any MVC framework like (Struts, Spring) are not sufficient enough to satisfy a particular requirement. Please create custom tags if it is absolutely necessary and not for sake. Developer should be aware that he is not reinventing the wheel by creating new custom tags.

Now let's create a simple custom tag for creating and populating a drop down list in a JSP. Have a look at the following TLD file

techdive.tld

Java Script Auto Submit form

The following JSP file automatically submits the form values when the page is loaded in browser.

Auto_Submit_Form.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c" %>

<html>
<head>
<title>Auto Login Form</title>
</head>

<script type="text/javascript">
window.onload = formSubmit;

function formSubmit() {

      document.forms[0].submit();
}
</script>

<body>

Java Script Page Refresh

Use the following code to refresh a JSP or HTML page with the specified time period.

<script language="javascript">
var delay=10000; // time period for refresh in milliseconds
function startClock()
{
      window.location.reload();
}

function windowRefresh()
{
      setTimeout("startClock()", delay);
}
windowRefresh();
</script>

Add the above code in any JSP file. When the page is accessed in a browser, it will refresh for every 10000 milliseconds.

Java Create PDF Document Using iText

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

Java Script Select All CheckBox in a JSP

Consider a web page where you display a list of values in a tabular format. Assume that each row in that table has a check box either as a first or last element. You may be faced with a requirement to select/deselect all the check boxes. Use the following JSP code which uses java scrip to select/deselect check boxes.

<%@page import="java.util.List" %>

<%@page import="java.util.ArrayList" %>  

<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c" %>

<html>
<head>
<title>CheckBox Example</title>
</head>
<body>
<h1>CheckBox Example</h1>
<%

Java File Operations

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

Java Script Progress Bar

The following code is used to generate a progress bar in a JSP or HTML page using JavaScript.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sample Progress Bar</title>

<script language="javascript">
var statusCnt=1;
function updateProgressBar()
{
        document.getElementById('td'+statusCnt).style.background='green';
        if(statusCnt==25)
        {
                document.getElementById('percentage').innerHTML="Success... "+(statusCnt*4)+" %";
        }
        else
        {

Java - Reflection

Reflection is used to examine the run time behavior of Java applications running in the Java Virtual machine.

Using reflection, instances can be created using the fully qualified names of the class.

Note: Reflection is a more powerful and advanced feature. It should not be used extensively unless the developer has a strong knowledge in Java.

In the below example, let us see how to create an object for the class and invoke its methods at run time using reflection.

Example:

Java - IP address (IPv4) format check

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

Java Directory Operations

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