Interview Questions


Servlet Filter - Redirect Session Timeout

Use the following to code to override the doFilter method, which redirects to login page on session timeout. The web.xml should be configured to use this filter.

        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
                String ipAddress = request.getRemoteAddr();
                System.out.println("IP " + ipAddress + ", Time " + new Date().toString());

                boolean authorized = false;
                String username = null;
                String role = null;
                if (request instanceof HttpServletRequest)

Recursive Templates in Underscore Js

Use the following code to call a template recursively in Underscore JS.
This example use Backbone JS, JSTree(Jquery) to display tree nodes

<!DOCTYPE html>
<html lang="en">
                <meta charset="utf-8">
                <title>Tree Structure</title>
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                <meta name="description" content="">
                <meta name="author" content="">
                <!-- Le styles -->
                <link href="css/bootstrap.css" rel="stylesheet">
                <link href="css/bootstrap-responsive.css" rel="stylesheet">

Basic Example Using JPA

The following code example illustrates creating/upating database tables using JPA.
The two tables used are Employee and Department. When the main class EmpDAO is run the tables get created in the database and the table records can be queried using JPA API's. Hibernate has been used as implementation for JPA. The dependency jars are referred in the pom.xml.

import java.util.Date;
import java.util.UUID;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;

Basic CSS3 Selectors Usage in HTML

The following code sample illustrates the CSS3 selectors/combinators usage in html documents.

<!DOCTYPE html>

section > hgroup{color:#0FFFF}

#form1{margin:20px;font-size:30px;width:300px;border:5px solid green;padding-left:20px}

label:hover {color:red}

p{border:10px solid black}
form:hover {color:blue;font-weight:bold}

b{text-decoration:underline;text-shadow:2px 2px 2px gray,0 0 10px red;

label label{font-size:20cm}

How to Auto refresh DIV content in jsp?

Use the following code to auto refresh DIV content in jsp page.

<%@ taglib prefix="c"           uri=""  %>

<script language="javascript">
var delay = 10000;
var autoRefreshContent = setInterval(
                function ()
                                var url = "<c:url value='/tdd/content.htm'> </c:url>";
                                url = url +"?sId="+Math.random();
                }, delay);
<div id="contentDiv" >
<%@ include file="content.jsp"%>


Web Application Auto Login

In general, every web application will have an authentication mechanism, to authenticate the users accessing the web application.

Suppose if there is a requirement to autologin to a web application from a given page, use the following code.

<script type="text/javascript">

function autoLogin()
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

jquery - highlight table cells

  <script type="text/javascript" src="jquery.js"></script>

<style type="text/css">
        cursor: pointer;
    <table id="example" border='1'>
          <td>First Name</td>
          <td>Last Name</td>
        <tr onclick="alert('John Smith')">

jQuery Date Picker - Display Date range fields

The following is a JSP code to display date range fields using jQuery.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

<script type="text/javascript" src="<c:url value='/jquery/jquery-1.6.1.min.js'/>"></script>

<script type="text/javascript" src="<c:url value='/jquery/jquery-ui-1.8.15.custom.min.js'/>"></script>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

<title>jQuery Date Picker Example</title>

<script type="text/javascript">
        $(function() {

Jsp Logout page template

Here is a simple JSP logout page template which can be used in Java based web applications.

It just invalidates the current user session and redirects to login page.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

<%@ taglib prefix="c" uri=""%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">



<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">



Java - Send HTTP Post Request

Use the following code to send Http Post request to any URL from Java.

class HttpPost
        public HttpURLConnection getHTTPConnection(String sendUrl)
                URL url = null;
                HttpURLConnection urlConn = null;

                        url = new URL(sendUrl);
                        urlConn = (HttpURLConnection) url.openConnection();
                        urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                        urlConn.addRequestProperty("User-Agent", "TDD");