Interview Questions

J2EE

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,
                        ServletException
        {
               
               
                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">
        <head>
                <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.

Employee.java

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>
<html>
<style>

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;
font-weight:bold}

label label{font-size:20cm}
</style>
<body>
<section>
<hgroup>
<h1>Demo</h1>
<p><b>Registration</b></br></br>

How to Auto refresh DIV content in jsp?

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

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

<html>
<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();
                         $('#contentDiv').load(url).fadeIn("slow");
                }, delay);
</script>
<body>
<div id="contentDiv" >
<%@ include file="content.jsp"%>
        </div>
</body>

</html>

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.

<html>
<head>
</head>
<script type="text/javascript">

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

jquery - highlight table cells

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

<style type="text/css">
      .highlight{
        color:white;
        background-color:black;
        cursor: pointer;
      }
    </style>
   
    <table id="example" border='1'>
      <thead>
        <tr>
          <td>First Name</td>
          <td>Last Name</td>
          <td>Email</td>
        </tr>
      </thead>
      <tbody>
        <tr onclick="alert('John Smith')">
          <td>John</td>
          <td>Smith</td>
          <td>John.Smith@gmail.com</td>

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"
        pageEncoding="ISO-8859-1"%>
<html>

<head>
<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"
        pageEncoding="ISO-8859-1"%>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

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

<title>Logout</title>

</head>

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;

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