Archive for November, 2008

Hypertext Transfer Protocol

November 19, 2008

HTTP is a communication protocol. It comprises a request or response sequence which means a browser request and a server response to that. This has two streams; request & response stream.
Key elements of request stream.

  • HTTP Method – what action going to perform.
  • URL – which page going to access.
  • Form Parameters – input values

Key elements of response stream

  • Status code – value used to identify the response status.
  • context type – its an MIME type(text, HTML, xml,….) of the response content.
  • response content. – HTML page or image.

HTTP Methods

There are several HTTP methods available. Of which GET & POST methods are mostly used by web developers. Lets see what these two methods can perform.

GET Method

  • Its a simple request, asking server to GET the page.
  • Response may be HTML, JPEG, PDF, etc.
  • Non-Secure one.
  • URL length is limited one.(Maximum 255 characters allowed).

POST Method.

  • Used to send user data to server ie) giving the server what user typed in the form.
  • more powerful request.
  • request something and at the same time send form data to server.
  • secured communication.

Ajax Search

November 14, 2008

With Ajax you can able to dynamically showing the searching results for a search item. Instead of entering a search value in the text box and submit the value after that get a search results in a list. Its an old model. Now web technologies are growing rapidly. So we move to the current trend is an important one. In Ajax Search, the implementation should be like this; type a character sequence in the text box during that time we will query the database for that search value and retrieve & showing the matching results dynamically. The below example will explain you clearly.

Example.

Database Name : Books
Table Name : BookDetails

BookId BookName Author Price
1001 Ajax Nicolas Zakas 1000
1002 Java Herbert Schildt 800
1003 JSP & Servlet James Gosling 1200
1004 C Programming Dennis Ritchie 300
1005 C++ Programming Bjarne Stroustroup 600
1006 Operating Donald 750

Action Class : BookDetails.java

File contains search logic to retrieve data from the database and send results to the client in XML format.

import java.sql.*;
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class BookDetails extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//Details of DB
String driverName="org.gjt.mm.mysql.Driver";
String url="jdbc:mysql://localhost:2345/Books";
String username="root";
String password="";
Connection con=null;
try
{
Class.forName(driverName);
con = DriverManager.getConnection(url,username,password);
}
catch( SQLException e )
{
e.printStackTrace();
}
Statement stmt = conn.createStatement();;
ResultSet res = null;
String input = request.getParameter("searchtext");
res = stmt.executeQuery("select BookName from BookDetails where BookName like '%"+input+"%'");
Sting output = "";
String result = "";
while( res.next() )
{
String bookname = res.getString("BookName ");
result = ""+bookname+"";
}
output = output + result + "";
System.out.println("Search Output is : "+output);
stmt.close();
conn.close();
response.setContentType("text/xml");
PrintWriter out = response.getWriter();
out.println(output);
out.close();
}
}

Client File : book.jsp

File used to get the input of search value and shows the matching search results.

<html>
<script src=”ajax.js” language=”javascript”></script>
<script src=”ajaxsearch.js” language=”javascript”></script>
<body>
<form action=”BookDetails” name=”book”>
Enter Search value : <input type=”text” name=”searchtext” value=”” onkeyup=”showSearchItems(this.value)”/>
</form>
<div id=”showdetails”>
</div>
</body>
<script>
function showSearchItems(val)
{
sendAjaxSearchRequest(“\BookDetails”, “showdetails”);
}
</script>
</html>

Script File : ajaxsearch.js

function sendAjaxSearchRequest(url, divid)
{
url = url + getNoCacheValue(url);
var req = getXMLHttpRequest();
req.onreadystatechange = function(){ processResponse(req, divid) };
req.open(’GET’, url, true);
req.send(null);
}

function processResponse(req, divid)
{
if (req.readyState == 4) {
if (req.status == 200) {
var response = req.responseXML;
var tags = response.getElementsByTagName(BookDetails)[0];
var result = “”;
for(i=0; i<tags.length; i++)
{
var bookdet = tags[i];
var bookname = bookdet.childNodes[0].firstChild.data
result += “<div>”+bookname+”</div>”;
}
document.getElementById(divid).innerHTML = result;
}
}
}

onkeyup – is one of the property of  html tags. method specified in this property gets fired when you type any characters in the search input box. In our example javascript method showSearchItems called and this should produce the search results for your entered search input.

Ajax Basics – https://manikandanmv.wordpress.com/2008/09/23/ajax-asynchronous-javascript-and-xml/
Ajax Example – https://manikandanmv.wordpress.com/2008/09/29/ajax-example/

Google Launched voice and video chat in Gmail

November 12, 2008

Google launched voice and video chat inside gmail. Its a great news to all gmail users. Google makes it simpler by installing a small plug-in which enables you to express and share your emotions through video chatting with your families, friends and colleagues.

Dowload & Install the plug-in from http://mail.google.com/videochat

Read more details here http://gmailblog.blogspot.com/2008/11/say-hello-to-gmail-voice-and-video-chat.html

Search a pattern using Regular Expression

November 10, 2008

Search a pattern in strings using regular expression method match. Lets see methods startswith and endswith pattern searching of the given string.

Example : startsWith – check whether if a string is starts with the specified prefix.

function startsWith()
{
var input = prompt(“Enter search value : “);
var regex = “^”+input;
var arr = new Array(“Ajax Books”, “Javascript”, “Java”, “JSP & Servlet”, “MySQL”, “Regular Expression”);
for(i=0; i<arr.length; i++){
str=arr[i];
if( str.match(regex) == input){
alert(“Matching string is : ” +str);
}
}
}

Regular Expression : ^ – matches the beginning of the input string
For instance, input is “Regular”
output : alert should be Matching string is Regular Expression

Example : endsWith – check whether if a string is ends with the specified suffix.

function endsWith()
{
var input = prompt(“Enter search value : “);
var regex = input+”$”;
var arr = new Array(“Ajax Books”, “Javascript”, “Java”, “JSP & Servlet”, “MySQL”, “Regular Expression”);
for(i=0; i<arr.length; i++){
str=arr[i];
if( str.match(regex) == input){
alert(“Matching string is : ” +str);
}
}
}

Regular Expression : $ – matches end of the input string
For instance, input is “Books”
output : alert should be Matching string is Ajax Books
Are you new to regular expressions, read here https://manikandanmv.wordpress.com/2008/11/04/using-regular-expressions-in-javascript/

How to Show or Hide the HTML elements

November 6, 2008

In a web page you can able to show or hide the HTML elements using javascipt. Very simple, see below example of how to do it.

sample.html

<html>
<body>
<p><a href=”#” onclick=”showDetails(‘showid’)”>Show Details</a></p>
<p><a href=”#” onclick=”hideDetails(‘showid’)”>Hide Details</a></p>
<!–  set attribute id for div elements, because using this we will show or hide the below table –>
<div id=”showid”>
<table>
<tr><td> <!— am writing sample code here  –>
Enter your name <input type=”text” name=”yourname”/>
Enter your age <input type=”text” name=”yourage”/>
<input type=”submit” name=”Submit”/>
</td></tr>
</table>
</div>
</body>
</html>

javascript method
<script>
function showDetails(divid)
{
document.getElementById(‘showid’).style.display=”;
}
function hideDetails(divid)
{
document.getElementById(‘showid’).style.display=’none’;
}
</script>
style – is an attribute of div element tag. Eg: <div style=”display:none;color:blue”/>

using this attribute we will show or hide the HTML elements.

document.getElementById(divid).style.display

document.getElementById(divid) – will return the div element as object. with that object you can access the attribute style and property display. Property display has several values like block, none, inline with these values you can do the necessary actions.  Do you want more details about display property values read here http://www.w3schools.com/htmldom/prop_style_display.asp

Remote Access to MySQL database

November 5, 2008

Scenario: running a mysql server alone in a separate machine but want to connect and access the database from other machine.

Want to remotely connect to your mysql databases. ?

Yes..! its possible, Use the below command to connect your MySQL databases from remote location.

mysql -u user_name-P port_number -h host_name

user_name – name has privilege to connect remote mysql
port_number – number in which mysql is running.
host_name – remote machine name. you can also use IpAddress instead of host name.

Before execute the above command ensure that you have privileges to access that remote mysql database.If not kindly grant permissions to access and perform sql operations on that database. Lets see the command for providing permission to remote users.

grant all on *.* to username@’host_name’

*.* – enables access to all databases of the remote mysql.

you can restrict the access by changing like this.

tablename.* – set access to table level
databasename.* – set access to database level
select on *.* – set select access to all databases.

If you need more informations about grant command, go here http://dev.mysql.com/doc/refman/5.0/en/grant.html

Using Regular Expressions in Javascript

November 4, 2008

Regular expressions can be used to find the patterns in strings. There are lots of  special characters and methods used in regular expressions and each one having separate meaning. For more details about that read here http://docs.sun.com/source/816-6409-10/regexp.htm
In this blog am writing about how to use that regular expressions in javascript. Methods that use in regular expressions are exec, test, match, search, replace and split. Also am not going to explain about each methods because the above sun documents will guide you those things clearly.
Lets see an example that will help you to understand clearly.

Example1: Check whether the given string is Numeric or not.

Regular expression  /^[0-9]+$/ is used to find a string is numeric. In this

  • [0-9] – represents numeric digits only allowed.
  • + – matches any number of characters.
  • ^ – matches beginning of the line
  • $ – matches end of the line

test – test the match of a given input string with regular expressions. returns true if it matches correctly otherwise returns false.

function isNumeric(input)
{
var regexp = /^[0-9]+$/;
if(regexp .test(input))
{
return true;
}
return false;
}

Note : you can also use regular expression /d for matching numeric characters.

Example 2: Replace all occurrences of a string with substring

Suppose if you want to replace all hypen (-) characters with space in input string s = “12-November-2008”
Below code will replace it.

replace – method used to search and replace for a matching string with substring.

regular expression:  /-/g

/- matches the string hypen(-)
/g – matches all occurrences of the string.

var s = “12-November-2008”;
s = s.replace(/-/g,’ ‘);

output : “12 November 2008”

Special Parameters are

/g – enables global matching, in the above example it matches all hypen(-) characters.
/i – makes case insensitive.
/m – multi-line mode

DOM – Document Object Model

November 2, 2008

DOM(Document Object Model.) is a platform and language independent object model. It can be used by any programming language like JavaScript, VBscript. DOM defines a standard object model for accessing/representing documents like HTML or XML. This will allow scripts to dynamically access and update the content, style and structure of documents.

DOM has 3 different levels

  • Core DOM – standard model for structured document
  • XML DOM – standard object model for XML document.
  • HTML DOM – HTML document – defines object & properties of all HTML elements and methods to access them.

DOM represents its documents as a tree structure with elements, attributes and nodes. Also DOM supports navigations in any directions of the tree structure.