Websocket in Java Part 1: Before JAVA EE7

Learn Recipe: WebSocket using Java

In this first article on websocket we will learn what is websocket and its implementation in JAVA EE6.
In part2 we will see new Java websocket API JSR356

So lets KICKSTART Websockets.

Precook : Few tech facts

What is Websocket ? A brief introduction

  • New protocol introduced in HTML5 specification (RFC 6455)
  • Leverages HTTP upgrade technology
  • Upgrade HTTP to Websocket
  • Once connection established no headers, cookies required
  • A full duplex communication
  • Independent  message can be sent in both direction (server-client).
  • Supports binary data
  • Since uses TCP/HTTP, thus work on same port
  • Also support secure connection ie both ws:// or wss:// supported.
  • Intended to use for: Chat Applications,Interactive games,Media Streaming, Notifications,server push…
  • Browser Support : (All leading browser supports)Firefox,Chrome,Safari,IE10,etc…
  • Server support: Apcahe Tomcat 7,Oracle Glassfish3,Oracle Glassfish4
    • For JAVA EE 6: Third party API available.( Tomcat7 /Glassfish3 /Jwebsocket / Lightstramer )
    • For JAVA EE7: Implemented (JSR356) Java API for Web Socket.

Let’s Cook : Simple Eco application

Continue reading


Example : Database Pooling With Tomcat7 and SQLServer

This is an step by step guide to create Database Pooling for java web based application which is recommended for preformance.
Container used : Tomcat v7
Databse used : MS SQL Server 2008
IDE used : Eclipse

Get latest Microsoft JDBC Driver 4.0 for SQL server from here
Use sqljdbc4.jar for Java6.

Step 1:
Create dynamic web project in Eclipse say DBPoolingSample.

Step 2:

Create XML file in WebContentMETA-INFcontext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Specify a JDBC datasource -->
<Resource name="jdbc/testdb" auth="Container"
type="javax.sql.DataSource" username="<username>" password="<password>"
maxActive="10" maxIdle="4" />

Note: Check IP for SQL server instance.
Note: Change username password.

Step 3:

Create a java class to retrieve Connection object.
package db.;

package db;

import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

* @author abhishek.anne
public class DBConnect {
private DataSource dataSource;
private Connection connection;
private static DBConnect moDbConnect;

private DBConnect() {

try {
// Get DataSource
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
dataSource = (DataSource) envContext.lookup("jdbc/testdb");

} catch (NamingException e) {

public static Connection getConnection() throws NullPointerException{
if (moDbConnect != null) {
try {
moDbConnect.connection = moDbConnect.dataSource.getConnection();
} catch (SQLException sqle) {
return moDbConnect.connection;
} else {
moDbConnect = new DBConnect();
try {
moDbConnect.connection = moDbConnect.dataSource.getConnection();
} catch (SQLException sqle) {
return moDbConnect.connection;


Step 4:
Create anyservlet to use connection obejct to perform db operation.

package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import db.DBConnect;

public class DBServlet extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

Connection connection=null;
Statement statement=null;
ResultSet resultSet = null;
try {
connection = DBConnect.getConnection();
statement = connection.createStatement();
String query = "SELECT * FROM [coonectpool_db].[dbo].[user]";
resultSet = statement.executeQuery(query);

PrintWriter out=resp.getWriter();

while (resultSet.next()) {
out.println(resultSet.getInt(1) + resultSet.getString(2));
} catch (SQLException e) {
}finally {
try {
if (null != resultSet)
} catch (SQLException e) {
try {
if (null != statement)
} catch (SQLException e) {
try {
if (null != connection)
} catch (SQLException e) {

Step 5:

Sample web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


Step 6:

Deploy Application and browse

By making driver change you can connect to respective Database server
MY SQL:com.mysql.jdbc.Driver
Oracle :oracle.jdbc.driver.OracleDriver