tag:blogger.com,1999:blog-52844379193869030432024-03-13T02:12:37.347-07:00JAVA EnthusiastBasic JAVA and J2EE technologies(JSP,JDBC,JavaBeans,SERVLETS), Web designing (HTML,CSS,JAVASCRIPT,Ajax)Harmender dhakahttp://www.blogger.com/profile/14082625144310167954noreply@blogger.comBlogger15125tag:blogger.com,1999:blog-5284437919386903043.post-633580349935902962012-08-25T08:06:00.001-07:002012-08-26T21:55:31.980-07:00NoClassDefFoundError<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Verdana, sans-serif;">This is a very common error which causes headache for most of the beginners when they try to execute their first java program,it compiles without any error, class file is also generated but when you try to run the program </span><br />
<span style="font-family: Verdana, sans-serif;">the most frustrating error pop up,that is:</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-size: small;"><span style="background-color: white; color: #333333; font-family: Arial; line-height: 25.8833px;">Exception in thread "main" </span><span style="color: #333333; font-family: 'Courier New'; line-height: 25.8833px;"><span style="background-color: white;">java.</span><span style="background-color: white;">lang.NoClassDefFoundError</span></span></span>
<br />
<span style="color: #333333; font-size: small; line-height: 25.8833px;"><span style="background-color: white; font-family: Arial, Helvetica, sans-serif;"><br /></span></span>
<span style="font-size: small; line-height: 25.8833px;"><span style="background-color: white; font-family: Arial, Helvetica, sans-serif;"><span style="color: red;">Note:</span><span style="color: #333333;"> don't misunderstand this with ClassNotFoundException,i'll mention the difference between the two similar looking exceptions at the end of this post.</span></span></span><br />
<br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">I have faced it a lot of times in the beginning,so i want to share it here</span><br />
<span style="font-family: Verdana, sans-serif;">what is the reason behind this error and how to rectify </span><br />
<span style="font-family: Verdana, sans-serif;">here is the reason:</span><br />
<span style="font-family: Verdana, sans-serif;">you should know that when we try to run a program like this </span><br />
<span style="font-family: Courier New, Courier, monospace;">command prompt:/java classname</span><br />
<span style="font-family: Verdana, sans-serif;">the JVM searches the classpath to locate the class name you provide to it.</span><br />
<span style="font-family: Verdana, sans-serif;">If JVM doesn't find definition of a class in the CLASSPATH which was available at the time of compilation it generates the error </span><br />
<br />
<span style="font-family: Verdana, sans-serif;"><span style="color: red;"><span style="background-color: white; font-size: 9pt; line-height: 25.883333206176758px;">Exception in thread "main" </span><span style="font-size: 9pt; line-height: 25.883333206176758px;"><span style="background-color: white;">java.</span><span style="background-color: white;">lang.NoClassDefFoundError</span></span></span></span>
<span style="font-family: Verdana, sans-serif;"><span style="color: red;"><span style="font-size: 9pt; line-height: 25.883333206176758px;"><span style="background-color: white;">
</span></span></span></span>
<br />
<span style="font-family: Verdana, sans-serif;">So you can easily understand that the problem is not with the code but it is because the JVM is unable to locate the class file,so to rectify the problem you need to :</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;"> >> identify the class which is creating the problem, you can easily find out it from the error message</span><br />
<br />
<span style="font-family: Times, Times New Roman, serif;">take an example </span><br />
<span style="font-family: Times, Times New Roman, serif;"><br /></span></div>
<pre class="brush:java">//hello.java
class SayHello
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
</pre>
<span style="font-family: Verdana, sans-serif;">in the above program there is only one class i.e SayHello</span>
<span style="font-family: Verdana, sans-serif;">after compiling this program </span><br />
<span style="font-family: Verdana, sans-serif;">promt:/javac hello.java</span><br />
<span style="font-family: Verdana, sans-serif;">a class "SayHello.class" will be automatically generated.</span>
<span style="font-family: Verdana, sans-serif;">and when you try to run your program</span>
<span style="font-family: Verdana, sans-serif;">from the same directory </span>
<span style="font-family: Verdana, sans-serif;">like this </span><br />
<span style="font-family: Verdana, sans-serif;">prompt:/java SayHello</span><br />
<span style="font-family: Verdana, sans-serif;">
</span>
<span style="font-family: Verdana, sans-serif;">you get the error <span style="color: red;"><span style="background-color: white; font-size: 9pt; line-height: 25.883333206176758px;">Exception in thread "main" </span><span style="font-size: 9pt; line-height: 25.883333206176758px;"><span style="background-color: white;">java.</span><span style="background-color: white;">lang.NoClassDefFoundError</span></span></span></span><br />
<span style="font-family: Verdana, sans-serif;"><span style="line-height: 25.866666793823242px;">the only reason behind this is you haven't provided the classpath for you class.</span></span>
<span style="font-family: Verdana, sans-serif;"><span style="line-height: 25.866666793823242px;">Provide the class path for you class file and the error will go away automatically.</span></span><br />
<span style="font-family: Verdana, sans-serif;"><span style="line-height: 25.866666793823242px;">there are two options </span></span><br />
<span style="font-family: Verdana, sans-serif;"><span style="line-height: 25.866666793823242px;">1> declare the CLASSPATH environment variable </span></span><br />
<span style="font-family: Verdana, sans-serif;"><span style="line-height: 25.866666793823242px;"><span style="font-size: xx-small;"><span style="font-size: x-small;">"Control Panel\System --> advanced system settings -->environment variables"</span> </span></span></span>
<span style="font-family: Verdana, sans-serif;"><span style="line-height: 25.866666793823242px;">which sets the classpath permanently. or</span></span><br />
<span style="font-family: Verdana, sans-serif;"><span style="line-height: 25.866666793823242px;">2> run your program like this </span></span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><span style="line-height: 25.8667px;">prompt:/java -cp . SayHello.java</span></span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><span style="line-height: 25.866666793823242px;">
</span></span><span style="font-family: Verdana, sans-serif;"><span style="line-height: 25.866666793823242px;">the -cp argument stands for classpath and "." for the current directory,it tells JVM to look in current directory for the class file. </span></span><br />
<span style="font-family: Verdana, sans-serif;"><span style="line-height: 25.866666793823242px;">
</span></span>
<span style="font-family: Verdana, sans-serif;"><span style="line-height: 25.866666793823242px;">this is all you need to do,to get rid of this error.</span></span><br />
<span style="font-family: Verdana, sans-serif;"><span style="line-height: 25.866666793823242px;"><br /></span></span>
<span style="font-family: Verdana, sans-serif;"><span style="line-height: 25.866666793823242px;">
</span></span>
<span style="font-family: Verdana, sans-serif;"><span style="line-height: 25.866666793823242px;">difference between </span></span>
<br />
<br />
<table cellpadding="5"><tbody>
<tr><th>ClassNotFoundException</th><th>NoClassDefFoundError</th></tr>
<tr><td>Occurs when a class is not found which is referenced in the program you are trying to execute</td><td>Occurs when a class is unavailable to JVM at the time of execution which was there at the time of compilation</td></tr>
<tr><td>Reason may be a missing package or jar file which contains the class</td><td>only possible reason is problem with CLASSPATH which is explained above</td></tr>
<tr><td>Problem may be in manifest file which may not have mentioned some jar file which is the source of the class</td><td><br /></td></tr>
</tbody></table>
<br />
<br /></div>
Harmender dhakahttp://www.blogger.com/profile/14082625144310167954noreply@blogger.com0tag:blogger.com,1999:blog-5284437919386903043.post-76130783774361510712012-08-03T12:42:00.000-07:002012-08-03T12:42:21.602-07:00Interface<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
An Interface is collection of methods with empty bodies.<br />
Interface contains only<br />
<br />
<ul style="text-align: left;">
<li>static and final fields.</li>
<li>abstract methods.</li>
</ul>
<br />
Interface is declared as<br />
<br />
interface abc<br />
{<br />
//static & final fields<br />
//empty body methods are declared here.<br />
}<br />
<br />
<b>Constraints:</b><br />
<br />
<ul style="text-align: left;">
<li>An interface can contain only abstract methods and any class implementing the interface thereby inherits the abstract methods of the interface.</li>
<li>Unless the class that implements the interface is abstract,all the methods of the interface must be defined in the class. </li>
</ul>
<div>
<b>Comparison between a normal class and interface:</b></div>
<div>
<br />
<table border="1" cellpadding="10">
<tbody>
<tr><th>class</th><th>interface</th></tr>
<tr><td>can have constructors</td><td>don't have any constructor</td></tr>
<tr><td>can be instantiated</td><td>can't be instantiated</td></tr>
<tr><td>is extended</td><td>is implemented</td></tr>
</tbody></table>
</div>
</div>
<div>
<br /></div>
<div>
<b>how to implement an interface ?</b><br />
<br />
here is a sample code<br />
<br />
<div style="background: #999999;">
interface Vehicle<br />
{<br />
public static final int speed;<br />
public void displayInfo();<br />
}<br />
class Car implements Vehicle<br />
{<br />
speed=150;<br />
public void displayInfo()<br />
{<br />
System.out.println("it's a car");<br />
}<br />
}</div>
<h4 style="text-align: left;">
</h4>
</div>
</div>Harmender dhakahttp://www.blogger.com/profile/14082625144310167954noreply@blogger.com0tag:blogger.com,1999:blog-5284437919386903043.post-42992696742716304342012-08-03T10:45:00.002-07:002012-08-03T12:49:28.761-07:00Abstract Class<div dir="ltr" style="text-align: left;" trbidi="on">
Abstract class is a class which contains methods without implementation.<br />
If a class contains even a single abstract method you need to declare your class as abstract<br />
like this<br />
<div style="background: #999999;">
abstract class <classname> <br />
{ <br />
//abstract methods <br />
} </div>
Abstract class can't be instantiated because an abstract class contains abstract methods without definitions so creating object for such a class is meaningless.<br />
Any subclass of an abstract class must provide the definition for the all the abstract methods of it's super class or declare itself also as abstract.<br />
<br />
Now the question is<br />
<b>Q)</b> Why we need an Abstract method ?<br />
<b>Answer:</b><br />
If you are unable to decide the exact definition of a method,lets take an example like if there is a method area() in your class,i bet you will get confused while providing definition for this method.you will definitely think whose area you are going to compute in this method either rectangle,triangle or circle ?<br />
and finally you will come up with <b>abstract area() </b>so that any subclass of your abstract class will provide the definition of area() according to the requirement.<br />
<br />
i am taking an example of an Automobile class<br />
<br />
<br />
<div style="background: #999999;">
abstract class Car<br />
{<br />
int price;<br />
String manufacturer;<br />
public void display()<br />
{<br />
System.out.println("its a car");<br />
}<br />
abstract public String getPrice();<br />
<br />
}<br />
<br />
class Bmw extends Car<br />
{<br />
public String getPrice()<br />
{<br />
return "40 lacs";<br />
}<br />
<br />
}<br />
class Hyundai extends Car<br />
{<br />
<br />
public String getPrice()<br />
{<br />
return "10 lacs";<br />
}<br />
}<br />
<br />
public class mainclass<br />
{<br />
public static void main(String args[])<br />
{<br />
Bmw car1=new Bmw();<br />
Hyundai car2=new Hyundai();<br />
car1.display();<br />
car1.getPrice();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> car2.display();<br />
car2.getPrice();<br />
}<br />
}</div>
<br />
<br />
in this example getPrice() method is abstract method in the Car class which is abstract itself.<br />
getPrice() is overrided in the subclasses Bmw and Hyundai.<br />
<br />
<br /></div>Harmender dhakahttp://www.blogger.com/profile/14082625144310167954noreply@blogger.com0tag:blogger.com,1999:blog-5284437919386903043.post-29499640998449790082012-07-28T02:02:00.002-07:002012-08-03T12:44:54.826-07:00Adapter Class<div dir="ltr" style="text-align: left;" trbidi="on">
In this article you will learn about adapter classes their use and how to write adapter classes manually.<br />
Before going for adapter classes you need to have an idea about Abstract classes and Interfaces.<br />
<br />
<ul style="text-align: left;">
<li><span style="background-color: white;"><a href="http://www.hsd-techonly.blogspot.in/2012/08/abstract-class.html" target="_blank">Abstract classes.</a></span></li>
<li><span style="background-color: white;"><a href="http://www.hsd-techonly.blogspot.in/2012/08/interface.html" target="_blank">Interfaces</a></span></li>
</ul>
<div>
as we know if we declare an interface all its methods must be abstract and need to be override in the sub class which implements that interface.</div>
<div>
>> So whats the problem here ? </div>
<div>
Its very time consuming and difficult task to provide definitions for each and every method of interface in the sub-classes.</div>
<div>
>> what is the need of adapter classes ?</div>
<div>
If you are not interested to override all the methods of interface you can make use of adapter classes.</div>
<div>
<b><br /></b></div>
<div>
<b>Adapter class:</b> it is an abstract class implementing any interface and it contains definitions of some of the methods of the interface it is implementing.<span style="background-color: white;">this Adapter class can be inherited now by any class without any bounds of overriding all the methods of the interface because the adapter class contains the definitions of that methods.</span></div>
<div>
Note: if <span style="background-color: white;">a method isn't </span><span style="background-color: white;">overridden in the adapter class then it is necessary to be overridden it in the sub class otherwise compiler will give an error.</span></div>
<div>
<br /></div>
<div>
here is an example showing adapter class :</div>
<div>
<br /></div>
<div>
<div>
import java.io.*;</div>
<div>
import java.util.*;</div>
<div>
interface iface </div>
<div>
{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void input(int x,int y);</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void biggest();</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void display();</div>
<div>
}</div>
<div>
<br /></div>
<div>
abstract class adapter implements iface</div>
<div>
{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>int d,e;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void input(int x,int y)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>d=x;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>e=y;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
}</div>
<div>
class big extends adapter</div>
<div>
{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>int c;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void biggest()</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>if(d>e)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>c=d;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>else</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>c=e;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void display()</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("biggest number is :"+c);</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<br /></div>
<div>
}</div>
<div>
public class main</div>
<div>
{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>public static void main(String args[])</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Scanner sc=new Scanner(System.in);</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>big a=new big();</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>int g=sc.nextInt();</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>int h=sc.nextInt();</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>a.input(g,h);</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>a.biggest();</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>a.display();</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
}</div>
</div>
<div>
<br /></div>
<div>
In the above example the <i>input()</i> method is optional to override in the subclass because it is already overridden in the adapter class.</div>
<div>
This example is just my attempt to make the concept clear,you may not find adapter classes much useful here in this example but in real time where interfaces contains tens of methods the programmers never like to override all of them directly in their classes so they use adapter classes and its a very effective technique.</div>
<div>
It reduces code and saves time.</div>
</div>Harmender dhakahttp://www.blogger.com/profile/14082625144310167954noreply@blogger.com0tag:blogger.com,1999:blog-5284437919386903043.post-10089065394614461362012-07-27T10:49:00.000-07:002012-08-03T22:26:02.122-07:00Creating a SSL Certificate<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
>> First of all You need JDK installed on your system to proceed further.<br />
>> Now using the keyTool utility available with JDK we can create SSL certificates like shown in the screenshot.<br />
use the command "keytool -genkey -alias tomcat -keyalag RSA"<br />
* here RSA is the algorithm used for encryption of data.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOqothnnfXKd1oEEzkphgz6IeU9rx-n3mNfLLRzHB7CdGZG5tBTqL96c2R0iYzAzsz5kW4ApZS2SZh5njKytg7zpiI_eZ_i-VnVg_KOq0He7cv_1xB0uREupM7tvsKKE1ATdiKFWBX86U/s1600/doswin.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" eda="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOqothnnfXKd1oEEzkphgz6IeU9rx-n3mNfLLRzHB7CdGZG5tBTqL96c2R0iYzAzsz5kW4ApZS2SZh5njKytg7zpiI_eZ_i-VnVg_KOq0He7cv_1xB0uREupM7tvsKKE1ATdiKFWBX86U/s1600/doswin.png" /></a></div>
<br />
>> You will see all the steps as shown in the above screenshot if you are going right.<br />
>> After creation of .keystore file(SSL certificate) it is stored in the user' home folder automatically.<br />
>> Now you need to configure the .keystore file in the server.xml file which is located inside %TOMCAT_HOME%\conf<br />
<br />
add the following element to the server.xml file.<br />
<span style="background-color: #cccccc;"><Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" keystoreFile="C:/Users/hsd/.keystore" secure="true" keystorePass="hsdhaka" clientAuth="false" sslProtocol="TLS" /></span><br />
>> Now start the tomcat server<br />
>> Enter the URL https://localhost:8443/ in addressbar of any browser and you will see the following page<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYD97Aw-r-_FwYnU4iVGNall0jjtHElFBd4PfyHu8xxecB35KwYY4vXCwl1COJQ-UY6h3UgNOxp3jFR22SjI5rsyoUVkFTreS0Tuk6JAaq89yC980oNzgOsJSdDi107YPiAwrM58LTzqU/s1600/https.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" eda="true" height="301" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYD97Aw-r-_FwYnU4iVGNall0jjtHElFBd4PfyHu8xxecB35KwYY4vXCwl1COJQ-UY6h3UgNOxp3jFR22SjI5rsyoUVkFTreS0Tuk6JAaq89yC980oNzgOsJSdDi107YPiAwrM58LTzqU/s640/https.png" width="640" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
This page indicates that your certificate is not created by a trusted vendor and thus can't be trusted,but the SSL security is on for your applications by doing so.</div>
<br />
If you will click on<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHIhEmpcUKwkFvldmdk8VEphW0XQViqz7_x-gMj_i88JQiTMywsf9x84zVHIdfU6sVVxAi6zyXjcWrZOAbdA_foa5ANxbd_MJgrElkZUKU8X2wuV-2uUFhtq4kf1Imy4SN5uFLOjmDDLM/s1600/continue.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" eda="true" height="37" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHIhEmpcUKwkFvldmdk8VEphW0XQViqz7_x-gMj_i88JQiTMywsf9x84zVHIdfU6sVVxAi6zyXjcWrZOAbdA_foa5ANxbd_MJgrElkZUKU8X2wuV-2uUFhtq4kf1Imy4SN5uFLOjmDDLM/s320/continue.PNG" width="320" /></a></div>
you will see the requested page as you expect it to be<br />
but you can easily notice address bar of your browser<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgR6JNG-xlLL141NqoS3FkmoIGtu_cs9gKcdG_Xm897oHwNt1SK6uiVunIoi-fXWep_Yhqq7viPnB7W4CRx3Wi7vaSxfm8esq_kHRWkPfH705ZMmaIgunVaIqCysZrNcKmikhYx9rSHd5w/s1600/notrecommended.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" eda="true" height="34" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgR6JNG-xlLL141NqoS3FkmoIGtu_cs9gKcdG_Xm897oHwNt1SK6uiVunIoi-fXWep_Yhqq7viPnB7W4CRx3Wi7vaSxfm8esq_kHRWkPfH705ZMmaIgunVaIqCysZrNcKmikhYx9rSHd5w/s320/notrecommended.PNG" width="320" /></a></div>
this shows that the resource you have requested is not trust worthy.<br />
If you don't want to see this kind of alerts before your website is displayed get,your certificate designed and signed by a trusted SSL vendor in the market like thawte.<br />
<br /><br />
<br /></div>Harmender dhakahttp://www.blogger.com/profile/14082625144310167954noreply@blogger.com0tag:blogger.com,1999:blog-5284437919386903043.post-68862318424073468832012-07-04T05:16:00.002-07:002012-08-04T02:47:33.303-07:00how to deploy a servlet application in tomcat<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="font-size: 20px;">
<div dir="ltr" style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; text-align: left;" trbidi="on">
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
Step to Deploy servlet application on tomcat web-server:</div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<br /></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<b>Step-1 Installing Tomcat :</b></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
If you have tomcat server already installed on your system go to step-2 directly.</div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
Otherwise download the tomcat setup or zip file from <a href="http://tomcat.apache.org/">http://tomcat.apache.org/</a> and extract the files wherever you prefer.Extracting is enough in case of zip file download,no special installation setup is required.</div>
<br />
<span style="background-color: white;"><b>Step-2 Setting <a href="http://hsd-techonly.blogspot.com/2012/06/environment-variables.html" target="_blank"><span style="color: black;">environment variables</span></a></b></span><br />
<span style="background-color: white;"> >> Set JAVA_HOME variable to your JDK installation directory i.e </span><span style="background-color: white;">C:\Program Files\Java\jdk1.6.0 in my</span><br />
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="background-color: white;"> System.It may be different on your system so check for it.</span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="background-color: white;">>> Set classpath for servlet-api.jar file which contains all the servlet libraries which is essential for compiling</span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="background-color: white;"> any servlet application.This servlet-api.jar file is provided with your server product in a location similar to</span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="background-color: white;"> "D:\apache-tomcat-6.0.29\lib\servlet-api.jar" ,this path may vary according to the installation location of </span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="background-color: white;"> tomcat server.</span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="background-color: white;"> you need to execute this command from command prompt to set classpath:</span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="background-color: white;"> <span style="font-family: 'Trebuchet MS', sans-serif;"> set classpath=</span></span><span style="background-color: white;"><span style="font-family: 'Trebuchet MS', sans-serif;">D:\apache-tomcat-6.0.29\lib\servlet-api.jar;</span></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="background-color: white;"> and your classpath is set proceed to next step.</span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="background-color: white;"><br /></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="background-color: white;"><b>Step-3 Designing Basic components like a servlet,html page.</b></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="background-color: white;"><b><br /></b></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="background-color: white;"><b>>> </b>Create a simple html file which will be used to invoke the servlet class.</span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="background-color: white;"><br /></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="background-color: white; color: blue;">//index.html</span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"><br /></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"><html></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"><center></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"><form action="./myServlet" method="get"></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"><input type="submit"/></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"></form></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"></center></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"></form></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<br /></div>
<br />
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
>> Now its time to create a servlet class.</div>
<br />
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;">//Myservlet.java</span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"><br /></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;">import javax.servlet.*;</span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;">import javax.servlet.http.*;</span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;">import java.io.*;</span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;">import java.util.*;</span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"><br /></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;">public class Myservlet extends HttpServlet</span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;">{</span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"> public void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException</span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"> {</span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"> PrintWriter pw=resp.getWriter();</span></div>
<span style="color: blue;"> pw.println("<html><center>this is servlet response</center></html>");</span><br />
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"> }</span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"><br /></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;">}</span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<br /></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<b>Step-4 Compile the servlet class.</b></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<b><br /></b></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
Open the command prompt</div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
Change current directory to the one which contains your Myservlet.java file</div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
Now execute command: <i> "</i><span style="font-family: 'Trebuchet MS', sans-serif;">javac Myservlet.java" </span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
It will generate the class file for your servlet if there is no error.</div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<br /></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<b>Step-5 Create a deployment descriptor file i.e web.xml</b></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
deployment descriptor contains the servlet information like servlet class,servlet mapping and url pattern for which the container will invoke the servlet.</div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<br /></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;">//web.xml</span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"><br /></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"><web-app></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"><servlet></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"><servlet-name>myServlet</servlet-name></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"><servlet-class>Myservlet</servlet-class></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"></servlet></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"><servlet-mapping></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"><servlet-name>myServlet</servlet-name></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"><url-pattern>/myServlet</url-pattern></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"></servlet-mapping></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<span style="color: blue;"></web-app></span></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<br /></div>
<br />
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<b>Step-6 Arranging all the above components in a standard directory structure</b></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<b><br /></b></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
Create a root folder for your application i.e Sample here in my example</div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
Inside root folder create a new folder WEB-INF.</div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
Inside WEB-INF create sub-folder classes and arreange the all the files as shown in the directory structure.</div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
Your final directory structure should look like the below one.</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYOrg2_LLf8fGuzfjyvVQY9jy5-l52tydklBO2kvwhPacldor3x_VMRXQnRrpGxrXzrLXHalNSxpRxLfr-jrd923jfbU_bXsfKf8url1a8mldyBKik9z54soQyzq5CSVH5S1wWJNOmpIE/s1600/di.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" eda="true" height="488" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYOrg2_LLf8fGuzfjyvVQY9jy5-l52tydklBO2kvwhPacldor3x_VMRXQnRrpGxrXzrLXHalNSxpRxLfr-jrd923jfbU_bXsfKf8url1a8mldyBKik9z54soQyzq5CSVH5S1wWJNOmpIE/s640/di.png" width="640" /></a></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<br /></div>
<b>Step-7 Deploy the Application </b><br />
<br />
Deployment is nothing but placing root folder of your application inside the<br />
"D:\apache-tomcat-6.0.29\webapps " folder that is inside the server installation directory.<br />
<br />
<b>Step-8 Make a request to your application:</b><br />
>> start the tomcat server by clicking on startup.batch file located inside the bin folder of your tomcat installation directory.<br />
>>open any browser and type the following url "http://localhost:8080/Sample/index.html "<br />
it will show your html page with a submit button which will call Myservlet on clicking.<br />
<br />
You are done,Good luck<br />
<br />
<br />
<br />
<br /></div>
</div>
</div>
</div>Harmender dhakahttp://www.blogger.com/profile/14082625144310167954noreply@blogger.com0tag:blogger.com,1999:blog-5284437919386903043.post-22742511578482342212012-06-30T11:59:00.002-07:002012-08-23T07:48:26.355-07:00Session Tracking using Cookies<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="font-family: Verdana,sans-serif;">
<span style="font-size: small;"><b>Cookies</b></span> are piece of information sent by any web application to a web browser. and this information is further used to track the client state(data).</div>
<div style="font-family: Verdana,sans-serif;">
In my <a href="http://www.hsd-techonly.blogspot.in/2012/06/session-tracking.html" target="_blank">last post</a> i wrote about session tracking with the help of hidden form fields but i also mentioned there that the hidden form fields are not effective in most of the situations.</div>
<div style="font-family: Verdana,sans-serif;">
So cookies are our next hope and cookies are nice indeed as they sound, I am going to discuss how.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
Whenever a client or end user sends a request to a web application by supplying some information in the form of request body or through URL.</div>
<div style="font-family: Verdana,sans-serif;">
The web appliction does :</div>
<ul style="font-family: Verdana,sans-serif; text-align: left;">
<li>Grab all the information from the request body or URL(Depends on the request method either get or post). </li>
</ul>
<ul style="font-family: Verdana,sans-serif; text-align: left;">
<li>Create cookies for each of these information elements.(!! creating cookies is far easy you will see very soon)</li>
</ul>
<div>
<ul style="font-family: Verdana,sans-serif; text-align: left;">
<li>Now that the information is encapsulated in the cookies the web application sends these cookies back to the requesting browser as a response. </li>
</ul>
<div style="font-family: Verdana,sans-serif;">
Now the browser plays its role :</div>
<ul style="font-family: Verdana,sans-serif; text-align: left;">
<li>The duty of browser is to store the cookies received from the web-application in some private location with Domain name and path of web-application from where the cookies arrived.</li>
</ul>
<div>
<ul style="font-family: Verdana,sans-serif; text-align: left;">
<li>Now when the cookies are stored,whenever next request is made from the same browser to the same domain the browser automatically embeds all the cookies matching that particular domain to the request body.</li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<span style="color: red;">Note:</span> Every Browser keep its cookies separately. ex. Internet Explorer don't share its cookies with Mozilla or any other,and same is the case with any other browser.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUFgMGumqX4gH-FUtXtQ59nZFa3VZGkwtPhFqbsRo02x4DldYBR34RW9rO67_C1sdRKeShdQ7jdznEaaGzUnjwBfCMTC9VwG2KNt1ssv-xY2oN43PXYdq7nYg2ET6wqoh2uVjqSIYgbug/s1600/cook.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" eda="true" height="426" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUFgMGumqX4gH-FUtXtQ59nZFa3VZGkwtPhFqbsRo02x4DldYBR34RW9rO67_C1sdRKeShdQ7jdznEaaGzUnjwBfCMTC9VwG2KNt1ssv-xY2oN43PXYdq7nYg2ET6wqoh2uVjqSIYgbug/s640/cook.png" width="640" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">pictorial representation of cookie transfer mechanism</td></tr>
</tbody></table>
<div style="font-family: Verdana,sans-serif;">
 <br />
<div class="separator" style="clear: both; text-align: center;">
</div>
</div>
<div style="font-family: Verdana,sans-serif;">
As you can observe above the mechanism of cookies is very simple.This was the general concept of how the cookies work.This concept remain same, doesn't matter on whether you use JAVA, PHP or .net.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
But i am totally java oriented person so Lets relate the concept of cookies with the java technology.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif; text-align: left;">
<span style="color: #351c75; font-size: large;"><b>Cookie :</b></span> is a class in java come with javax.servlet.http package with one and only two argument constructor.</div>
<div style="font-family: Verdana,sans-serif;">
Cookie(String ,"value"); </div>
<div style="font-family: Verdana,sans-serif;">
second argument to the constructor can be of any type i.e String,int,double,float,boolean etc.</div>
<div style="font-family: Verdana,sans-serif;">
<b>>></b> Creating a cookie:</div>
<div style="font-family: Verdana,sans-serif;">
You can't create a cookie object without providing these two arguments i.e name and value.</div>
<div style="font-family: Verdana,sans-serif;">
Syntax for creating a Cookie object is :</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
Cookie cuki=new Cookie("uasername","wilson");</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
in the above line of java code "cuki" is a complete cookie with name "username" and value "wilson".</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
Every cookie in the world is created in the same way as shown above. </div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b>>></b> Now the second minor challenge is to pass the cookie to the browser back.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
we do it like this:</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
response.addCookie("reference to cookie object");</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
After adding the cookie to the response the role of Server side application is almost over.</div>
<div style="font-family: Verdana,sans-serif;">
<b>>></b> Now the browser will store the cookie in a predefined fashion</div>
<div style="font-family: Verdana,sans-serif;">
that is:</div>
<div style="font-family: Verdana,sans-serif;">
whenever the browser receives a cookie it first checks whether there is prior cookie from the same domain with same name.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
case1: if a cookie is already present there with same name and domain</div>
<div style="font-family: Verdana,sans-serif;">
browser just replace it with the new one.</div>
<div style="font-family: Verdana,sans-serif;">
case2: if the cookie is new to the browser then it stores the cookie normally.</div>
<div style="font-family: Verdana,sans-serif;">
<b><br /></b></div>
<div style="font-family: Verdana,sans-serif;">
<b>>></b> After storing the cookie,the prime duty of browser is to attach all the associated cookies for a domain to any of request made for that domain.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b>>></b> And the web application utilizes these cookies to identify client states.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
This was all theoretical view of working of cookies.</div>
<div style="font-family: Verdana,sans-serif;">
now its time for a real and working example which will show you the complete picture:</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="background-color: #f3f3f3; font-family: 'Trebuchet MS',sans-serif;">
<i>//index.html the starting page.</i></div>
<div style="background-color: #f3f3f3; font-family: 'Trebuchet MS',sans-serif;">
<i><br /></i></div>
<div style="background-color: #f3f3f3; font-family: 'Trebuchet MS',sans-serif;">
<i><html><br /><head><br /><%@page language="java"%><br /><title>cookies tutorial</title><br /></head><br /><body bgcolor="gray"><br /><form name="login" action="Storecookies"><br />UserName:<input type="text" name="uname"/><br/><br />Password: <input type="password" name="pass"/><br/><br /><input type="submit" value="login"/><br /></form><br /></body><br /></html></i></div>
<div style="background-color: #f3f3f3; font-family: 'Trebuchet MS',sans-serif;">
<i><br /></i></div>
<div style="background-color: #f3f3f3; font-family: 'Trebuchet MS',sans-serif;">
<div style="background-color: white;">
<br /></div>
<i>// this is the source of Storecookies servlet.</i></div>
<div style="background-color: #f3f3f3; font-family: 'Trebuchet MS',sans-serif;">
<i><br /></i></div>
<div style="background-color: #f3f3f3; font-family: 'Trebuchet MS',sans-serif;">
<i>import java.io.*;<br />import javax.servlet.*;<br />import javax.servlet.http.*;<br />import java.util.*;<br />public class Storecookies extends HttpServlet<br />{<br /> public Storecookies(){<br /> }<br style="background-color: #f3f3f3;" /><span style="background-color: #f3f3f3;"> </span><br style="background-color: #f3f3f3;" /><span style="background-color: #f3f3f3;"> public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException</span><br style="background-color: #f3f3f3;" /><span style="background-color: #f3f3f3;"> {</span><br style="background-color: #f3f3f3;" /><span style="background-color: #f3f3f3;"> Enumeration paras=req.getParameterNames();</span><br style="background-color: #f3f3f3;" /><span style="background-color: #f3f3f3;"> PrintWriter pw=resp.getWriter();</span><br style="background-color: #f3f3f3;" /><span style="background-color: #f3f3f3;"> String rem=req.getParameter("remember")</span></i></div>
<div style="font-family: 'Trebuchet MS',sans-serif;">
<i><span style="background-color: #f3f3f3;"><span style="background-color: #f3f3f3;"> while(paras.hasMoreElements()) </span></span><br style="background-color: #f3f3f3;" /><span style="background-color: #f3f3f3;"> { </span><br style="background-color: #f3f3f3;" /><span style="background-color: #f3f3f3;"> String pname=(String) paras.nextElement(); </span><br style="background-color: #f3f3f3;" /><span style="background-color: #f3f3f3;"> String pvalue=req.getParameter(pname); </span><br style="background-color: #f3f3f3;" /><span style="background-color: #f3f3f3;"> Cookie c=new Cookie(pname,pvalue); </span><br style="background-color: #f3f3f3;" /><span style="background-color: #f3f3f3;"> c.setPath("/*"); </span><br style="background-color: #f3f3f3;" /><span style="background-color: #f3f3f3;"> resp.addCookie(c); </span><br style="background-color: #f3f3f3;" /><span style="background-color: #f3f3f3;"> } </span><br style="background-color: #f3f3f3;" /><span style="background-color: #f3f3f3;"> pw.write("<b>cookies are stored successfully</b>"); </span><br style="background-color: #f3f3f3;" /><span style="background-color: #f3f3f3;"> pw.write("<a href='Showcookies'>show me the cookies</a>");</span><span style="background-color: #f3f3f3;"> </span><br style="background-color: #f3f3f3;" /><span style="background-color: #f3f3f3;"> }</span><span style="background-color: #f3f3f3;"> </span><br style="background-color: #f3f3f3;" /><span style="background-color: #f3f3f3;">} </span></i></div>
<div style="font-family: 'Trebuchet MS',sans-serif;">
<i><br /></i>
<i><br /></i></div>
<div style="background-color: #f3f3f3; font-family: 'Trebuchet MS',sans-serif;">
<i>// Source of Showcookies Servlet.</i></div>
<div style="background-color: #f3f3f3; font-family: 'Trebuchet MS',sans-serif;">
<i><br /></i></div>
<div style="background-color: #f3f3f3; font-family: 'Trebuchet MS',sans-serif;">
<i><br /></i></div>
<div style="background-color: #f3f3f3; font-family: Verdana,sans-serif;">
<i><span style="font-family: 'Trebuchet MS', sans-serif;">import javax.servlet.http.*;</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;">import javax.servlet.*;</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;">import java.io.*;</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;">public class Showcookies extends HttpServlet</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;">{</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> </span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> public void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> {</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> </span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> Cookie c[]=req.getCookies();</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> </span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> PrintWriter pw=resp.getWriter();</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> pw.write("<html><body bgcolor='gray'>");</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> for(int i=0;i<c.length;i++)</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> {</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> String name=c[i].getName();</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> String value=c[i].getValue();</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> </span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> pw.println(name+" -> "+value+"<br/>");</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> }</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> pw.write("</body></html>");</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> }</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> public void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> {</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> doGet(req,resp);</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> }</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;">}</span></i></div>
<div style="font-family: 'Trebuchet MS',sans-serif;">
<i><br /></i></div>
<div style="background-color: #f3f3f3; font-family: 'Trebuchet MS',sans-serif;">
<i>// web.xml file with Servlet mappings</i></div>
<div style="background-color: #f3f3f3; font-family: 'Trebuchet MS',sans-serif;">
<i><br /></i></div>
<div style="background-color: #f3f3f3;">
<i><span style="font-family: 'Trebuchet MS', sans-serif;"><?xml version="1.0" encoding="UTF-8"?></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"</span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> <servlet></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> <description>just testing servlets</description></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> <servlet-name>Storecookies</servlet-name></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> <servlet-class>Storecookies</servlet-class></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> </servlet></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> <servlet-mapping></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> <servlet-name>Storecookies</servlet-name></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> <url-pattern>/Storecookies</url-pattern></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> </servlet-mapping></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> <servlet></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> <servlet-name>Showcookies</servlet-name></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> <servlet-class>Showcookies</servlet-class></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> </servlet></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> <servlet-mapping></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> <servlet-name>Showcookies</servlet-name></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> <url-pattern>/Showcookies</url-pattern></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> </servlet-mapping></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> <welcome-file-list></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> <welcome-file>index.jsp</welcome-file></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"> </welcome-file-list></span><br style="font-family: 'Trebuchet MS',sans-serif;" /><span style="font-family: 'Trebuchet MS', sans-serif;"></web-app></span></i></div>
<br />
<br />
<div style="font-family: Verdana,sans-serif;">
This was a Simple application imlementing cookies to store client data.You can easily <a href="http://hsd-techonly.blogspot.in/2012/07/how-to-deploy-servlet-application-in.html" target="_blank">Deploy</a> this simple and ready made application and checkout the outcome.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
Now i want to add some methods which are provided by Cookie class for providing advance control over cookies.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<h3 style="font-family: Verdana,sans-serif; font-weight: normal; text-align: left;">
<span style="font-size: small;"><b>>></b>What is Age for a cookie?</span></h3>
<div style="font-family: Verdana,sans-serif;">
Its the time period for which the browsers keep a cookie and after expiring of that time period they delete the cookie.</div>
<div style="font-family: Verdana,sans-serif;">
We can set Maximum age for any cookie using the below method </div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<i>setMaxAge(time in seconds);</i></div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
Default MaxAge for any cookie is -1 which instructs browser to keep the cookie till the browser is running,once the browser is closed cookie is lost.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b>>></b> We can set Domain name for a cookie explicitly if we like to do so.otherwise its automatically set by the web containers. </div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<i>setDomain("Domain name");</i></div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b>>></b> We can decide the paths for which the browser should send the cookie within a applicatin,like if you want a cookie to be sent only for the login page in your web applicatin.you can do it like this </div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<i>setPath("/login.jsp");</i></div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b>>></b> Cookies are of two types secured and insecure</div>
<div style="font-family: Verdana,sans-serif;">
Secured cookies use https(hypertext secure socket layer) protocol.But cookies are insecure by default.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<i>setSecure("boolean");</i> //provide true as argument to make a cookie secure.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b>>></b> To get value of a cookie object we use the below method on the cookie object:</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<i>getValue();</i></div>
<div style="font-family: Verdana,sans-serif;">
<b><br /></b></div>
<div style="font-family: Verdana,sans-serif;">
<b>>></b> To get name of a cookie :</div>
<div style="font-family: Verdana,sans-serif;">
<i>getName();</i></div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b>>></b> If you want to know MaxAge of a cookie use </div>
<div style="font-family: Verdana,sans-serif;">
<i>getMaxAge();</i><br />
<br />
<br />
<i></i><i></i><br />
<i> --------------------------------------------------------------------------------------</i><br />
<br />
<br />
<i> support by providing your valuable feedback !! </i></div>
<br /></div>
</div>
</div>
Harmender dhakahttp://www.blogger.com/profile/14082625144310167954noreply@blogger.com0tag:blogger.com,1999:blog-5284437919386903043.post-45516254712193849692012-06-28T11:31:00.000-07:002012-08-03T22:35:03.917-07:00Session Tracking<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="font-family: Verdana,sans-serif; text-align: left;">
What is Session ?</h3>
<div style="font-family: Verdana,sans-serif; text-align: left;">
Session is nothing But the time for which a client is recognizable to the web application.</div>
<div style="font-family: Verdana,sans-serif; text-align: left;">
When a client first time makes a request for some web resource by providing some information about itself the Session starts, and when the serving web application loses information about the client the session ends.</div>
<div style="font-family: Verdana,sans-serif; text-align: left;">
For example : when you click on login button after providing a valid email id and password in the Gmail login screen your session is started, because from that time onward google can recognize you untill you logout. </div>
<div style="font-family: Verdana,sans-serif; text-align: left;">
<br /></div>
<div style="text-align: left;">
<span style="font-family: Verdana,sans-serif;">Here in this Post you will learn about the requirement of session tracking and various mechanisms to achieve session tracking.</span><br />
<br />
<h3 style="text-align: left;">
<span style="font-family: Verdana,sans-serif;">What is Session Tracking ? </span></h3>
<div style="text-align: left;">
<span style="font-family: Verdana,sans-serif;">Session tracking is mechanism of tracking the client provided data and making it available to the next request from the same client.and this process is continued until the user choose to LogOut or terminate the session.</span></div>
</div>
<h3 style="text-align: left;">
<span style="font-family: Verdana,sans-serif;">Why we need Session Tracking ? </span></h3>
<div style="font-family: Verdana,sans-serif; text-align: left;">
Hypertext Transfer Protocol(HTTP) we all are familiar with this protocol because it is totally unavoidable.</div>
<div style="font-family: Verdana,sans-serif; text-align: left;">
The must know characteristic of HTTP is its stateless nature.</div>
<div style="font-family: Verdana,sans-serif; text-align: left;">
Stateless means it can't sustain a relationship with the clients.HTTP protocol sends a response back to a client and then it just forget about this activity, and go for serving next request. </div>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKjK5HUAARQxUMOqmyh9f_FYIjbdDiAPceHdiNPGG0VJ0vHilzKupSPiwTs16CdAQDiJiSOMwsG8XaqFum7R5LjnUc2q8nIL7mBDn4_cXEOVgWn-0YXxCyv4Ai36N1T6W6QZ-AlbA23Z4/s1600/http.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" eda="true" height="355" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKjK5HUAARQxUMOqmyh9f_FYIjbdDiAPceHdiNPGG0VJ0vHilzKupSPiwTs16CdAQDiJiSOMwsG8XaqFum7R5LjnUc2q8nIL7mBDn4_cXEOVgWn-0YXxCyv4Ai36N1T6W6QZ-AlbA23Z4/s400/http.png" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">StateLess nature of Http protocol</td></tr>
</tbody></table>
<div style="font-family: Verdana,sans-serif; text-align: left;">
If our requirement is limited to static content only then HTTP is very effective because it is faster than any other protocol,but what to do if you want to be recognized by the application you are using on the web. <br />
like if you want to reserve air tickets and you are on website of Kingfisher Airlines,how would you feel if they ask you for your account information and then shows you a welcome note, obviously you will feel good to be welcomed<br />
but what happens if they just forget about you on the next request and again ask you to provide account information,everybody is supposed to lose patience at that time this is also obvious.<br />
So the Conclusion is,we need to maintain client data to design effective dynamic applications.<br />
<br />

<div class="separator" style="clear: both; text-align: center;">
</div>

<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<h3 style="text-align: left;">
Session tracking mechanisms :</h3>
1. Hidden Form Fields.</div>
<div style="font-family: Verdana,sans-serif; text-align: left;">
2. Cookies.</div>
<div style="font-family: Verdana,sans-serif; text-align: left;">
3. URL rewriting.</div>
<div style="font-family: Verdana,sans-serif; text-align: left;">
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
4. HttpSession interface. </div>
</div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; font-family: Verdana,sans-serif; text-align: left;">
<br />
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<b>Hidden Form Fields Mechanism:</b> as we are aware about html forms,they are used to accept input from client or end user.</div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
Form fields are of three types regular,password and hidden.</div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
we can define them like this:</div>
<div style="font-family: 'Trebuchet MS',sans-serif;">
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<i><input type="text" name="<any anme>" value="<default value>"/> </i></div>
</div>
<div style="font-family: 'Trebuchet MS',sans-serif;">
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<i><input type="password" name="<any name>" value="<default value>"/></i></div>
</div>
<div style="font-family: 'Trebuchet MS',sans-serif;">
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<i><input type="hidden" name="<any name>" value="<value of field>"/></i></div>
</div>
<div style="font-family: 'Trebuchet MS',sans-serif;">
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<i> </i><span style="font-family: Verdana,sans-serif;">The last one with <i><span style="font-family: "Trebuchet MS",sans-serif;">type="hidden"</span></i></span> <span style="font-family: Verdana,sans-serif;">is not displayed by the browser</span> while rendering the response.So these hidden form fields become effective tools to embed information about the client inside the html forms without displaying it to the end user.</div>
</div>
<div style="font-family: 'Trebuchet MS',sans-serif;">
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
The wonderful quality of hidden fields is,they are automatically sent with the request object along other fields when user clicks on the submit button.</div>
</div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
<br /></div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
Example: </div>
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
here is an example of a simple servlet based application using hidden form fields to maintain client data.</div>

<div class="separator" style="clear: both; text-align: center;">
</div>

<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDyHLDARs3BMK374pUrzjaLdgg5-kH3NsgaTE4ZVwZBwIM5lGFND6Et9nF1oZTuOpMlzjS7yYs4E_Xry0bH0gho8C5IhwMNUFvas88H-aiOqyRcUZTyNZO06FIRQqIjpauId_vKo49dn0/s1600/sample.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" eda="true" height="241" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDyHLDARs3BMK374pUrzjaLdgg5-kH3NsgaTE4ZVwZBwIM5lGFND6Et9nF1oZTuOpMlzjS7yYs4E_Xry0bH0gho8C5IhwMNUFvas88H-aiOqyRcUZTyNZO06FIRQqIjpauId_vKo49dn0/s400/sample.png" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Bank account refilling application</td></tr>
</tbody></table>

<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;">
In the above sample application flow diagram,first of all user sees a login form:</div>
<div style="font-family: 'Trebuchet MS',sans-serif;">
<i><form action="Authorization" method="post"></i></div>
<div style="font-family: 'Trebuchet MS',sans-serif;">
<i><input type="text" name="userid"/></i></div>
<div style="font-family: 'Trebuchet MS',sans-serif;">
<i><input type="password" name="upasswd"/></i></div>
<div style="font-family: 'Trebuchet MS',sans-serif;">
<i><input type="submit" value="login"/></i></div>
<div style="font-family: 'Trebuchet MS',sans-serif;">
<i></form></i></div>
</div>
<br />
User name: <input name="userid" /><br />
Password: <input name="upasswd" type="password" /> <br />
<input type="submit" value="login" /> <br />
<div style="text-align: left;">
<br />
<div style="font-family: Verdana,sans-serif;">
when the user click on login the control goes to Authorization servlet which accesses database to match the input data and if the data provided by the user is valid it produces a dynamic html form like this:</div>
<div style="font-family: 'Trebuchet MS',sans-serif;">
<i><form action="refill" method="post"></i></div>
<div style="font-family: 'Trebuchet MS',sans-serif;">
<i><input type="hidden" value=request.getParameter("userid")/> </i></div>
<div style="font-family: 'Trebuchet MS',sans-serif;">
<i><input type="text" name="bank"/></i></div>
<div style="font-family: 'Trebuchet MS',sans-serif;">
<i><input type="text" name="amount"/> </i></div>
<div style="font-family: 'Trebuchet MS',sans-serif;">
<i><input type="submit"/></i></div>
<i><span style="font-family: "Trebuchet MS",sans-serif;"></form> </span></i><br />
<br /></div>
</div>
<input type="hidden" value="hsd" /> Bank name: <input name="bank" /><br />
Amount to be Filled: <input name="amount" /> <input type="button" value="refill" /> <br />
<br />
<span style="font-family: Verdana,sans-serif;">when the user clicks on the refill button the control automatically goes to refill servlet which then communicate with database to refill the account of client with userID embedded in the hidden form field. </span><br />
<br />
<span style="font-family: Verdana,sans-serif;">So now i think its clear to you that how the information is shared between multiple requests using hidden form fields. </span><br />
<br />
<span style="font-family: Verdana,sans-serif;">now its time to discuss some drawbacks of this mechanism of hidden form fields</span><br />
<br />
<span style="font-family: Verdana,sans-serif;">Drawbacks:</span><br />
<span style="font-family: Verdana,sans-serif;">1.It is not supported by hyperlinks :means if the control transfer is through the means of form submission then its k but if a user clicks on an anchor tag then all the information in hidden fields is just lost.</span><br />
<span style="font-family: Verdana,sans-serif;">2.We cant track sensitive data like passwords in the hidden form fields because,even a average technical person can view the HTML source of the pages retrieved from server and take out passwords very easily.</span><br />
<span style="font-family: Verdana,sans-serif;">3.Useful only if the forms are generated by server side scripts,these hidden fields are of no use in session tracking if the forms are static. </span><br />
<span style="font-family: Verdana,sans-serif;"><br /></span><br />
<span style="font-family: Verdana,sans-serif;">The alternative to the mechanism of hidden form fields are :</span><br />
<br />
<a href="http://www.hsd-techonly.blogspot.in/2012/06/session-tracking-using-cookies.html" target="_blank"><b><span style="font-family: Verdana,sans-serif;">Cookies </span></b></a><br />
<br />
<b><span style="font-family: Verdana,sans-serif;">URL rewriting </span></b><br />
<span style="font-family: Verdana,sans-serif;"><br /></span><br />
<span style="font-family: Verdana,sans-serif;"><b>HttpSession interface.</b></span><br />
<span style="font-family: Verdana,sans-serif;"><br /></span></div>Harmender dhakahttp://www.blogger.com/profile/14082625144310167954noreply@blogger.com0tag:blogger.com,1999:blog-5284437919386903043.post-44067203667093095612012-06-27T12:12:00.000-07:002012-08-04T08:19:11.410-07:00Environment variables<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
Environment variables:</h2>
<div style="font-family: Verdana,sans-serif;">
These are variables which controls the behavior of applications and various processes dynamically.</div>
<div style="font-family: Verdana,sans-serif;">
for
example %TEMP% environment variable can be used by a processes to store
temporary data used by it at the path provided by %TEMP% variable.</div>
<div style="font-family: Verdana,sans-serif;">
Environmental variables enhances the flexibility of applications in two ways: </div>
<div style="font-family: Verdana,sans-serif;">
1. user can edit the environment variables an application uses to change its behavior,any time.</div>
<div style="font-family: Verdana,sans-serif;">
2.
As the environmental variables are external to the applications they
are loaded at run-time.So its possible to change the behavior of
application without recompiling them.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<span style="color: red;">Note:</span>
Environment variables can be provided only specific names which can be
detected by the processes which use them.So it is not in our hands to
decide the Environment Variable Name but We can change the paths to
which Environment variables points. </div>
<div style="font-family: Verdana,sans-serif;">
For Example: </div>
<div style="font-family: Verdana,sans-serif;">
%HOME%
Environment Variable is used by various processes to locate the root
user's home directory which is by default "C:\users\<user
account>" in every windows operating system.</div>
<div style="font-family: Verdana,sans-serif;">
but we can change this path to any other say: "d:\home"</div>
<div style="font-family: Verdana,sans-serif;">
but
if we attempt to change variable name from %HOME% to any other say
%MYHOME% then this Environment Variable is of no use to any process
which are coded to use %HOME% Environment variable.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
There are many standard Environment variables:</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<ul style="text-align: left;">
<li><b>HOME</b> (points to the user home directory)<b> </b></li>
</ul>
<ul style="text-align: left;">
<li><b>TEMP</b> or <b>TMP</b> (point to the path used for storing temporary data)</li>
</ul>
<ul style="text-align: left;">
<li><b>NUMBER_OF_PROCESSORS</b> (no of processors deployed in your system)</li>
</ul>
<ul style="text-align: left;">
<li><b>OS</b> (represents Operating System)</li>
</ul>
<ul style="text-align: left;">
<li><b>PROCESSOR_ARCHITECTURE</b></li>
</ul>
<ul style="text-align: left;">
<li><b>CLASSPATH:</b></li>
</ul>
<div style="font-family: Verdana,sans-serif;">
classpath variable is used to locate class files used inside java </div>
<div style="font-family: Verdana,sans-serif;">
applications. </div>
<div style="font-family: Verdana,sans-serif;">
whenever a class is need to
be loaded in a java application the JVM </div>
<div style="font-family: Verdana,sans-serif;">
searches for appropriate class
file in all locations starting from first path </div>
<div style="font-family: Verdana,sans-serif;">
in the CLASSPATH
variable.If the
class file is present in the current </div>
<div style="font-family: Verdana,sans-serif;">
working directory and you dont want JVM to search through all path </div>
<div style="font-family: Verdana,sans-serif;">
locations in the CLASSPATH variable just
add ".;" to the beginning of </div>
<div style="font-family: Verdana,sans-serif;">
your CLASSPATH variable.
in ".;" the dot "." represents current working </div>
<div style="font-family: Verdana,sans-serif;">
directory and ";" is the separator. </div>
<ul style="text-align: left;">
<li><b>path:</b></li>
</ul>
<b> </b><span style="font-family: Verdana,sans-serif;">path</span><b> </b><span style="font-family: Verdana,sans-serif;">environment variable is used to provide paths to the executable </span><br />
<span style="font-family: Verdana,sans-serif;"> </span> <span style="font-family: Verdana,sans-serif;">files and commands of the operating syatem or some other application </span><br />
<span style="font-family: Verdana,sans-serif;"> </span><span style="font-family: Verdana,sans-serif;">like Compilers and MySQL. We use path variables for individual </span><br />
<span style="font-family: Verdana,sans-serif;"> </span><span style="font-family: Verdana,sans-serif;">commands so that we are not required to provide full directory path </span><br />
<span style="font-family: Verdana,sans-serif;"> every </span><span style="font-family: Verdana,sans-serif;">time we execute the commands. </span><br />
<span style="font-family: Verdana,sans-serif;"> Example: we provide path "c:\program files\java\jdk1.6.0\bin" in the </span><br />
<span style="font-family: Verdana,sans-serif;"> </span><span style="font-family: Verdana,sans-serif;">path variable because we need to use <b>javac</b> and <b>java commands </b>to </span><br />
<span style="font-family: Verdana,sans-serif;"> compile and execute java applications from command prompt and these </span><br />
<span style="font-family: Verdana,sans-serif;"> executable commands are located inside the bin directory of our java </span><br />
<span style="font-family: Verdana,sans-serif;"> installation directory.after setting path variable we can directly use javac</span><br />
<span style="font-family: Verdana,sans-serif;"> and java commands in command prompt like any other DOS command. </span><span style="font-family: Verdana,sans-serif;"> </span><br />
<div>
<ul style="text-align: left;">
<li><b>USERNAME</b> (stores the username for admin account) etc.</li>
</ul>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
Most
of the above Environment variables must have are already declared on
your system if you will check for them,because these above Environment
variables are Operating System oriented.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b>How to check Environment Variables:</b></div>
<div style="font-family: Verdana,sans-serif;">
there are two ways</div>
<div style="font-family: Verdana,sans-serif; text-align: left;">
1. Open command prompt</div>
<div style="font-family: Verdana,sans-serif; text-align: left;">
give command: set</div>
<div style="font-family: Verdana,sans-serif; text-align: left;">
It will display all the variables on the console. </div>
<div style="font-family: Verdana,sans-serif; text-align: left;">
<span style="color: red;">note:</span>If you want to look at some specific variables for ex. if you want to </div>
<div style="font-family: Verdana,sans-serif; text-align: left;">
display variables with names starting with character L </div>
<div style="font-family: Verdana,sans-serif; text-align: left;">
give command: set L </div>
<div style="font-family: Verdana,sans-serif; text-align: left;">
</div>
<div style="font-family: Verdana,sans-serif;">
2. Open control panel</div>
<div style="font-family: Verdana,sans-serif;">
select System > advance system settings > Environment variables</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b>How to set Environment Variable:</b></div>
<div style="font-family: Verdana,sans-serif;">
there are two ways</div>
<div style="font-family: Verdana,sans-serif;">
1. Open command prompt</div>
<div style="font-family: Verdana,sans-serif;">
give command: set <variable name>=<path></div>
<div style="font-family: Verdana,sans-serif;">
2. Open control panel > system > advance system settings >Environment </div>
<div style="font-family: Verdana,sans-serif;">
variables > new </div>
<div style="font-family: Verdana,sans-serif;">
then provide variable name and value in the fields provided.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b>How to delete Environment variable</b></div>
<div style="font-family: Verdana,sans-serif;">
again there are two ways</div>
<div style="font-family: Verdana,sans-serif;">
1. Open command prompt</div>
<div style="font-family: Verdana,sans-serif;">
give command: set <variable name to be deleted>=</div>
<span style="font-family: Verdana,sans-serif;"> this way the value for the variable will be set to null value in other words </span><br />
<span style="font-family: Verdana,sans-serif;"> it </span><span style="font-family: Verdana,sans-serif;">will be removed.</span><br />
<span style="font-family: Verdana,sans-serif;">2. Open control panel > system > advance system settings >Environment</span><br />
<div style="font-family: Verdana,sans-serif;">
variables select variable and click on delete.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<span style="color: red;">note:</span> You may have seen environment variable names enclosed in %---%</div>
<div style="font-family: Verdana,sans-serif;">
this syntax is used with those Environment variables which are used in other paths like path=%JAVA_HOME%/bin;</div>
<div style="font-family: Verdana,sans-serif;">
this way %JAVA_HOME% will be automatically replaced with value of JAVA_HOME Environment variable.</div>
<span style="font-family: Verdana,sans-serif;"> </span><br />
<span style="font-family: Verdana,sans-serif;"> </span></div>
</div>Harmender dhakahttp://www.blogger.com/profile/14082625144310167954noreply@blogger.com0tag:blogger.com,1999:blog-5284437919386903043.post-88037565355375400912012-06-21T03:54:00.000-07:002012-08-02T08:02:44.481-07:00Using Type-4 DB2 driver for Communication between java application and DB2 server<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="font-family: Verdana,sans-serif;">
This is my second Post on JDBC the <a href="http://www.hsd-techonly.blogspot.in/2011/11/jdbc.html">first was based on Type-1 architecture based JDBC Drivers</a> and this one is for Type-4<br />
<br />
I suppose that you have Installed the DB2 database server and JDK/JRE on your system if you have not installed yet Do it now,before starting this tutorial.<br />
<br />
Now to create an application which can communicate with DB2 server you need<br />
1. JDBC driver.<br />
2. <a href="http://hsd-techonly.blogspot.in/2012/06/environment-variables.html">CLASSPATH environment variable</a> of our system set to JDBC driver provided by DB2.<br />
3. Little knowledge of basic JAVA and following interfaces<br />
a.) Driver interface.<br />
b.) Statement and PreparedStatement Interfaces.<br />
c.) ResultSet Interface.<br />
<br />
<b style="color: #0b5394;">>></b> JDBC driver requirement is not a reason to worry,because type-4 drivers are shipped with database products by default by the database vendors.<br />
For your DB2 version you can find it in the installation directory of your DB2<br />
for windows OS it is mostly -> c:\program files\IBM<br />
then you can find drivers in the SQLLIB\java directory in the IBM folder<br />
the driver names are db2jcc.jar and db2jcc_license_cu.jar<br />
<br />
<b style="color: #0b5394;">>></b> Now that you have located your drivers you need to add complete path for these driver to the <a href="http://hsd-techonly.blogspot.in/2012/06/environment-variables.html">CLASSPATH environment variable</a> of your system.<br />
To do this proceed with following steps:<br />
<ol>
<li>open control panel.</li>
<li>select system option.</li>
<li>select advanced system settings.</li>
<li>a new small window will pop up select environment variables from here </li>
<li>now see if there is already a CLASSPATH variable in system or user variables or not</li>
</ol>
case 1: If it is already there select it to edit and add full path for both the drivers mentioned above separated by a semicolon(;) and save the settings by clicking on ok button.</div>
<div style="font-family: Verdana,sans-serif;">
case 2: If the CLASSPATH variable is not there already create a new one provide the name CLASSPATH to it and add path for both drivers.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b style="color: #0b5394;">>></b>Half of your JOB is done already. Its time for coding the application</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
Keep in mind:</div>
<ul style="font-family: Verdana,sans-serif; text-align: left;">
<li>Default port no for DB2 is : 50000</li>
</ul>
<ul style="font-family: Verdana,sans-serif; text-align: left;">
<li>Host name : localhost ( if the server is on your own machine).</li>
</ul>
<ul style="font-family: Verdana,sans-serif; text-align: left;">
<li>Driver URL: </li>
</ul>
<div style="font-family: Verdana,sans-serif;">
For type-4 Drivers:</div>
<div style="font-family: Verdana,sans-serif;">
"jdbc:db2://localhost:50000/test","username","password"</div>
<div style="font-family: Verdana,sans-serif;">
and for type-2 Drivers it is: </div>
<div style="font-family: Verdana,sans-serif;">
"jdbc:db2:database","username","password"</div>
<ul style="font-family: Verdana,sans-serif; text-align: left;">
<li>Never forget to close the connection after using it,its not a matter of consideration here in our sample application.but if you are designing some enterprise level application for your employer,and you forget to close the connections then only god can save you from getting fired .. mind it.</li>
</ul>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
import java.util.*;</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
import java.sql.*;</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
class test</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
{</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
public static void main(String args[])</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
{</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
try</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
{</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
/* load the driver class and registers the driver with driver pool automatically*/</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
Class.forName("com.ibm.db2.jcc.DB2Driver");</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
/* create a connection object using DriverManager class of java.sql package*/ </div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
Connection conn=DriverManager.getConnection("jdbc:db2://localhost:5000/test","hsd","hsdhaka91");</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
/*create statement class object and use it to send query to the connected database*/</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
ResultSet rs=conn.createStatement().executeQuery("select * from test.employee");</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
while(rs.next())</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
{</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
// access the data records/rows here. </div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
// if there is no record in the tablename table this block will not be executed.</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
}</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
conn.close();</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
} </div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
catch(Exception ex)</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
{</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
System.out.println(ex);</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
}</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
<br /></div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
} </div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
}</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
Same application but without using the DriverManager</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
import java.util.*;</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
import java.sql.*;</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
class test</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
{</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
public static void main(String args[])</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
{</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
try</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
{</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif; text-align: left;">
/* load the driver class,registers the driver with driver pool and creates an object to Driver class*/</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
com.ibm.db2.jcc.DB2Driver obj=new com.ibm.db2.jcc.DB2Driver();</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
/* Properties is an inbuilt class in the java.util package used to store properties */</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
Properties p=new Properties();</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
p.setProperty("user","username"); //provide username as second argument.</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
p.setProperty("password","userpassword"); // provide password for the database user</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
Connection conn=obj.connect("jdbc:db2://localhost:50000/dbname",p);</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
<br /></div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
ResultSet rs=conn.createStatement().executeQuery("select * from tablename");</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
while(rs.next())</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
{</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
// access the data records/rows here.</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
// if there is no record in the tablename table this block will not be executed.</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
}</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
conn.close();</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
}</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
catch(Exception ex)</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
{</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
System.out.println(ex);</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
}</div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
<br /></div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
} </div>
<div style="background-color: #eeeeee; font-family: Times,"Times New Roman",serif;">
}</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
In the second application we are not taking support of DriverManager class to create connection object</div>
<div style="font-family: Verdana,sans-serif;">
but we are directly calling connect("URL",property object); method to create the connection. </div>
<div style="font-family: Verdana,sans-serif;">
This connect method is a member of Driver class object.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b style="color: #0b5394;">>></b> You can also use PreparedStatement Interface to increase the user interactivity of your application.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
I think This tutorial will be helpful for my readers.</div>
<div style="font-family: Verdana,sans-serif;">
Please provide your valuable Feedback for this post. </div>
</div>
Harmender dhakahttp://www.blogger.com/profile/14082625144310167954noreply@blogger.com0tag:blogger.com,1999:blog-5284437919386903043.post-5277949312582470712012-06-17T05:54:00.001-07:002012-08-02T08:03:12.238-07:00timing events in javascript<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
<span style="font-size: large;"><b>setInterval() <span style="font-size: small;">and</span> setTimeout()</b></span></h2>
<div style="font-family: Verdana,sans-serif;">
are the two methods available in javascript which we can use to invoke timing events.</div>
<span style="font-family: Verdana,sans-serif;"><b>setInterval()</b> method takes two arguments,first one is the name of the function to be called after the time interval that we have provided as argument to the setInterval() method of ours.</span><br />
<span style="font-family: Verdana,sans-serif;">and Second argument is the time interval itself in milliseconds</span>.<br />
<i style="font-family: "Trebuchet MS",sans-serif;">i.e setInterval(functionName(),1000);</i><br />
<div style="font-family: Verdana,sans-serif;">
It will keep on calling functionName() after every 1000 milliseconds</div>
<div style="font-family: Verdana,sans-serif;">
We can stop this repeated calling of function explicitaly using another javascript method that is</div>
<div style="font-family: Verdana,sans-serif;">
<span style="font-size: small;">clearInterval(intervalvar)</span></div>
<div style="font-family: Verdana,sans-serif;">
where intervalvar is variable for setInterval()</div>
<div style="font-family: Verdana,sans-serif;">
var intervalvar=setInterval("function name",1000);</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
here is an interesting example using setInterval() to implement a slideshow of images:</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="background: #999999; font-family: Verdana,sans-serif;">
<html><br />
<head><br />
<title>changing Image</title><br />
<script type="text/javascript"><br />
var images= new Array();<br />
images[0]="image1.jpg";<br />
images[1]="image2.jpg";<br />
images[2]="image3.jpg";<br />
images[3]="image4.jpg";<br />
images[4]="image.jpg";<br />
var x=0;<br />
function timer()<br />
{<br />
setInterval(function() {slider()},7000);<br />
}<br />
function slider()<br />
{<br />
document.getElementById("slide").src=images[x];<br />
if(x<4)<br />
{<br />
x=x+1;<br />
<br />
}<br />
else<br />
{<br />
x=0;<br />
}<br />
}<br />
</script><br />
</head><br />
<body onload="timer()"></div>
<div style="font-family: Verdana,sans-serif;">
<br />
<image id="slide" src="image.jpg" style="border-style:solid; border-width:5px; border-color:green;" width="500" height="400"/><br />
<br />
</body><br />
</html> </div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<b>setTimeout()</b> is another timing event method,it also accept two arguments function to be called after timeout and time period.</div>
<div style="font-family: Verdana,sans-serif;">
i.e setTimeout(function(),3000);</div>
<div style="font-family: Verdana,sans-serif;">
It will call function() after 3000 milli seconds,but only once ! !</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
example for setTimeout():</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="background: #999999;">
<span style="font-family: Verdana,sans-serif;"><html></span><br />
<span style="font-family: Verdana,sans-serif;"><head></span><br />
<span style="font-family: Verdana,sans-serif;"><title>changing Image</title></span><br />
<span style="font-family: Verdana,sans-serif;"><script type="text/javascript"></span><br />
<span style="font-family: Verdana,sans-serif;"> var images= new Array();</span><br />
<span style="font-family: Verdana,sans-serif;"> images[0]="image2.jpg";</span><br />
<span style="font-family: Verdana,sans-serif;"> images[1]="image3.jpg";</span><br />
<span style="font-family: Verdana,sans-serif;"> images[2]="image4.jpg";</span><br />
<span style="font-family: Verdana,sans-serif;"> images[3]="image5.jpg";</span><br />
<span style="font-family: Verdana,sans-serif;"> images[4]="image1.jpg";</span><br />
<span style="font-family: Verdana,sans-serif;"> var x=0;</span><br />
<br />
<span style="font-family: Verdana,sans-serif;"> var myinterval;</span><br />
<span style="font-family: Verdana,sans-serif;">function timer()</span><br />
<span style="font-family: Verdana,sans-serif;">{</span><br />
<span style="font-family: Verdana,sans-serif;"> myinterval=setInterval(function(){slider()},3000);</span><br />
<br />
<span style="font-family: Verdana,sans-serif;"> setTimeout(function(){stopslideshow()},30001); //setTimeout invoked</span><br />
<span style="font-family: Verdana,sans-serif;">}</span><br />
<span style="font-family: Verdana,sans-serif;">function slider()</span><br />
<span style="font-family: Verdana,sans-serif;">{</span><br />
<span style="font-family: Verdana,sans-serif;"> document.getElementById("slide").src=images[x];</span><br />
<span style="font-family: Verdana,sans-serif;"> if(x<4)</span><br />
<span style="font-family: Verdana,sans-serif;"> {</span><br />
<span style="font-family: Verdana,sans-serif;"> x=x+1;</span><br />
<span style="font-family: Verdana,sans-serif;"> </span><br />
<span style="font-family: Verdana,sans-serif;"> }</span><br />
<span style="font-family: Verdana,sans-serif;"> else</span><br />
<span style="font-family: Verdana,sans-serif;"> {</span><br />
<span style="font-family: Verdana,sans-serif;"> x=0;</span><br />
<span style="font-family: Verdana,sans-serif;"> }</span><br />
<span style="font-family: Verdana,sans-serif;">}</span><br />
<br />
<span style="font-family: Verdana,sans-serif;">function stopslideshow()</span><br />
<br />
<span style="font-family: Verdana,sans-serif;">{</span><br />
<br />
<span style="font-family: Verdana,sans-serif;"> clearInterval(myinterval);</span><br />
<br />
<span style="font-family: Verdana,sans-serif;">}</span><br />
<span style="font-family: Verdana,sans-serif;"></script></span><br />
<span style="font-family: Verdana,sans-serif;"></head></span><br />
<span style="font-family: Verdana,sans-serif;"><body onload="timer()"></span><br />
<br />
<span style="font-family: Verdana,sans-serif;"><image id="slide" src="image1.jpg" style="border-style:solid; border-width:5px; border-color:green;" width="500" height="400"/></span><br />
<br />
<span style="font-family: Verdana,sans-serif;"></body></span><br />
<span style="font-family: Verdana,sans-serif;"></html> </span><br />
<div style="font-family: Verdana,sans-serif;">
<br /></div>
</div>
<div style="font-family: Verdana,sans-serif;">
we can use </div>
<div style="font-family: Verdana,sans-serif;">
<u>clearTimeout(timeoutvar)</u> to suspend the setTimeout() event.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
where timeoutvar=setTimeout("function name",10000);</div>
<div style="font-family: Verdana,sans-serif;">
</div>
<span style="font-family: Verdana,sans-serif;">suppose you have provided 10 seconds of time interval in the setTimeout(function(),10000) and then you invoke clearTimeout(var) before 10 seconds from invoking setTimeout(),the function() will not be executed.</span><br />
<br /></div>
Harmender dhakahttp://www.blogger.com/profile/14082625144310167954noreply@blogger.com0tag:blogger.com,1999:blog-5284437919386903043.post-31481851471071088312012-06-15T23:47:00.000-07:002012-06-27T12:36:34.876-07:00Passing Control Between Web Pages<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
Forward vs Redirect</h2>
<div style="font-family: Verdana, sans-serif;">
These are the two methods of transferring the control between web pages,the purpose of both is almost same but there is some difference which i am going to tell you here.</div>
<div style="font-family: Verdana, sans-serif;">
<br /></div>
<div style="font-family: Verdana, sans-serif;">
<u><b>Redirect:</b></u> If you want to implement this redirecting process in jsp you have to follow this syntax:</div>
<div style="background-color: #eeeeee; font-family: "Trebuchet MS", sans-serif;">
<i><c:redirect url="targetPageURL"></i></div>
<div style="font-family: Verdana, sans-serif;">
<span style="color: lime;"> </span><b style="color: #6aa84f;"> </b></div>
<div style="font-family: Verdana, sans-serif;">
<b style="color: #6aa84f;">>></b> where c is nothing but prefix for core JSTL actions we set at the beginning of every jsp page to use Java Standard Tag Library .</div>
<div style="font-family: Verdana, sans-serif;">
<br /></div>
<div style="font-family: Verdana, sans-serif;">
we include tag libraries in our page this way :</div>
<div style="background-color: #eeeeee; font-family: "Trebuchet MS", sans-serif;">
<i><%@taglib prefix="c" uri="http://java.sun.com/jstl/core"%></i></div>
<div style="color: #e06666; font-family: Arial, Helvetica, sans-serif;">
Note:you can provide some other prefix aswell but c is recommended so that you don't catch a reserved keyword.</div>
<div style="font-family: Verdana, sans-serif;">
Now moving to the main point</div>
<div style="font-family: Verdana, sans-serif;">
Redirecting will pass the control to the page specified in the url attribute of <redirect>tag. This control transfer make browser to request the new page in a formal way,and the address in the address bar of browser changes to the url of new page as the page is loaded. </redirect></div>
<div style="font-family: Verdana, sans-serif;">
<br /></div>
<div style="font-family: Verdana, sans-serif;">
<redirect><u><b>Forward:</b></u> </redirect></div>
<div style="font-family: "Trebuchet MS", sans-serif;">
<i><redirect><jsp:forward page="targetPageURL"> </redirect></i></div>
<div style="font-family: Verdana, sans-serif;">
<redirect>this way the new page will be loaded for processing the same request by the jsp container implicitly without telling Browser of client machine about control transfer.<br />
So the URL in the address bar of the browser will remain same even though new page has been loaded. </redirect></div>
<div style="font-family: Verdana, sans-serif;">
<redirect>Another specification of foreward method is,it automatically transfers all the parameters with Request scope to the nest page.</redirect></div>
<div style="font-family: Verdana, sans-serif;">
<redirect>Differences b/w both: </redirect><br />
<br />
<br /></div>
<table cellpadding="5" cellspacing="2" style="border-color: green; border-style: solid; border-width: 1px; width: 600px;">
<tbody>
<tr>
<th>Forward</th>
<th>Redirect</th>
</tr>
<tr>
<td><b><span style="color: red;">-></span></b> It request the new page implicitly without letting browser know about the control transfer.</td>
<td><span style="color: red;">-></span> It request the new page with the help of browser in a formal way. </td>
</tr>
<tr>
<td><span style="color: red;">-></span> URL in the address bar of the browser remain intact indicating previous page. </td>
<td><span style="color: red;">-></span> URL changes to the new page automatically</td>
</tr>
<tr>
<td><span style="color: red;">-></span> Transfers all the request parameters to the target page.</td>
<td><span style="color: red;">-></span> It doesn't transfer request scope parameters to the target page.</td>
</tr>
</tbody></table>
<redirect></redirect><redirect> </redirect><br />
<br />
<div style="background-color: #b6d7a8; font-family: "Courier New", Courier, monospace;">
<redirect></redirect><u><b>Advatage of Forward:</b></u> It is fast because browser don't interfere in requesting of target page.</div>
<div style="background-color: #b6d7a8; font-family: "Courier New", Courier, monospace;">
<redirect><u><b>Drawback of Forward:</b></u> URL doesn't change with the loading of target page,so if the user try to refresh the target page,browser will again open the previous page because the URL in the address bar is still indicating the previous page.</redirect></div>
<div style="background-color: #b6d7a8; font-family: "Courier New", Courier, monospace;">
<redirect><u><b><br /></b></u></redirect></div>
<div style="font-family: "Courier New", Courier, monospace;">
<br /></div>
<div style="background-color: #d9ead3; font-family: "Courier New", Courier, monospace;">
<redirect><u><b>Advantage of Redirect:</b></u> If user try to refresh the target page,there's no problem.</redirect></div>
<div style="background-color: #d9ead3;">
<redirect><u style="font-family: "Courier New", Courier, monospace;"><b>Disadvantage of Redirect:</b></u><span style="font-family: "Courier New",Courier,monospace;"> it is slow because of Request and Response procedure followed by browser adds extra time in loading of target page.</span></redirect></div>
<redirect><br /></redirect></div>Harmender dhakahttp://www.blogger.com/profile/14082625144310167954noreply@blogger.com0tag:blogger.com,1999:blog-5284437919386903043.post-26193303744142817782012-06-15T09:48:00.000-07:002012-06-15T10:48:42.357-07:00Singleton classes in java<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="color: #674ea7; font-size: large;">Speaking in a layman's language "classes that stay single throughout their life are called singleton classes" is not wrong at all</span><br />
<div style="color: red; font-family: Georgia, "Times New Roman", serif;">
<span style="font-size: small;">but</span></div>
<div style="color: #674ea7;">
<span style="font-size: large;">As<span style="font-size: large;"> you cant b</span><span style="font-family: inherit;"><span style="font-size: large;">e a layman if you have surfed through here to read about singleto</span>n classes.</span></span></div>
<div style="color: #674ea7;">
<span style="font-size: large;">Here is the technical description for Singleton classes in java:</span><br />
<div style="background-color: #f3f3f3;">
<br /></div>
</div>
<div style="background-color: #f3f3f3; font-family: "Courier New", Courier, monospace;">
<span style="font-size: large;">Classes with their constructor declared as private is called singleton class,simply speaking.</span></div>
<div style="font-family: "Courier New", Courier, monospace;">
<br /></div>
<div style="font-family: Verdana, sans-serif;">
<b style="color: red;">>></b><span style="font-size: large;"> <span style="color: #674ea7; font-family: Times, "Times New Roman", serif;">As we are aware about the purpose of constructors and consequences of declaring constructor for a class as private.</span></span></div>
<div style="background-color: #f3f3f3; font-family: Verdana, sans-serif; text-align: left;">
<h3 style="color: #990000; margin-bottom: 0px; text-align: left;">
Constructor:</h3>
is a kind of method inside a class with same identifier(name) as of class,constructor is invoked by default whenever we create an object for a class.There can be multiple constructors declared for a single class,each with different no of arguments. </div>
<div style="background-color: #f3f3f3; font-family: Verdana, sans-serif; text-align: left;">
constructors are mostly used to initialize the member variables of the class by using constructors with arguments.</div>
<br />
<div style="color: #20124d; font-family: Times, "Times New Roman", serif; text-align: left;">
<span style="font-size: large;">Suppose if there is only one constructor which is declared as private, means what ?</span></div>
<div style="color: black; font-family: Verdana, sans-serif; text-align: left;">
<span style="font-size: small;">if you know a little about the access specifiers public , private and protected you will say "if the constructor is declared as private, we can't invoke it from outside of the class" </span></div>
<div style="color: #674ea7; font-family: Times, "Times New Roman", serif; text-align: left;">
<span style="font-size: large;"><span style="font-family: Verdana, sans-serif; font-size: small;"><span style="color: black;">that's right !!</span></span></span><br />
<span style="font-size: large;"><span style="font-family: Verdana, sans-serif; font-size: small;"><span style="color: black;"> </span></span></span><i><span style="font-family: Verdana, sans-serif;">but</span> </i><br />
<div style="color: black; font-family: Verdana, sans-serif;">
to create an object of a class we need to call its constructor infact the default constructor is called automatically.</div>
</div>
<div style="font-family: Verdana, sans-serif;">
when we write something like</div>
<i>ClassName obj=new ClassName; </i><br />
<div style="font-family: Verdana, sans-serif;">
<b style="color: red;">>></b>This simply concludes that we cant create object from outside of a class with private constructor.</div>
<br />
<div style="background-color: #fce5cd; font-family: Verdana, sans-serif;">
But there is a facility by which we can create a object for a singleton class but keep in mind only one object not more than one !!</div>
<div style="background-color: #fce5cd; font-family: Verdana, sans-serif;">
<br /></div>
<div style="font-family: Verdana, sans-serif;">
Using static bock because static functions of a class can be called from anywhere using the class name even if there is no object existing for the class.</div>
<div style="font-family: Verdana, sans-serif;">
like shown in the java code snippet below:</div>
<div style="background-color: #d0e0e3;">
<br /></div>
<div style="background-color: #d0e0e3;">
Class Singleton</div>
<div style="background-color: #d0e0e3;">
{</div>
<div style="background-color: #d0e0e3;">
int a;</div>
<div style="background-color: #d0e0e3;">
String s; </div>
<div style="background-color: #d0e0e3;">
static Singleton obj;</div>
<div style="background-color: #d0e0e3;">
private singleton()</div>
<div style="background-color: #d0e0e3;">
{</div>
<div style="background-color: #d0e0e3;">
a=10;</div>
<div style="background-color: #d0e0e3;">
s="hello";</div>
<div style="background-color: #d0e0e3;">
}</div>
<div style="background-color: #d0e0e3;">
public static singleton getobject()</div>
<div style="background-color: #d0e0e3;">
{</div>
<div style="background-color: #d0e0e3;">
if(obj==NULL)</div>
<div style="background-color: #d0e0e3;">
return(new singleton());</div>
<div style="background-color: #d0e0e3;">
else</div>
<div style="background-color: #d0e0e3;">
System.out.println("object of singleton class already exist"); </div>
<div style="background-color: #d0e0e3;">
} </div>
<div style="background-color: #d0e0e3;">
public static displaydata()</div>
<div style="background-color: #d0e0e3;">
{</div>
<div style="background-color: #d0e0e3;">
System.out.println("a ="+a);</div>
<div style="background-color: #d0e0e3;">
System.out.println("s ="+s);</div>
<div style="background-color: #d0e0e3;">
} </div>
<div style="background-color: #d0e0e3;">
<br /></div>
<div style="background-color: #d0e0e3;">
}</div>
<div style="background-color: #d0e0e3;">
Class test</div>
<div style="background-color: #d0e0e3;">
{</div>
<div style="background-color: #d0e0e3;">
public static void main(String args[])</div>
<div style="background-color: #d0e0e3;">
{</div>
<div style="background-color: #d0e0e3;">
Singleton obj=singleton.getobject();</div>
<div style="background-color: #d0e0e3;">
singleton.displaydata();<br />
singleton.getobject(); </div>
<div style="background-color: #d0e0e3;">
} </div>
<div style="background-color: #d0e0e3;">
}<br />
<br />
<u><b>output:</b></u><br />
a=10<br />
s=hello <br />
object of singleton class already exist</div>
<br />
<div style="color: #660000; font-family: Verdana, sans-serif;">
<b><span style="color: red;">>></span></b> <span style="color: black;">whenever a static member of a class is called from outside of the class the class is automatically loaded into the memory.</span></div>
<div style="color: #660000; font-family: Verdana, sans-serif;">
<b><span style="color: red;">>></span></b> <span style="color: black;">The static content of a class is loaded only once into the memory even if we try to load a class multiple times.</span> </div>
<div style="background-color: #d9d2e9; font-family: Verdana, sans-serif;">
so in above example: </div>
<div style="background-color: #d9d2e9; font-family: Verdana, sans-serif;">
<b><span style="color: orange;">-></span></b> there is a class with name Singleton with private constructor.</div>
<div style="background-color: #d9d2e9; font-family: Verdana, sans-serif;">
<b><span style="color: yellow;">-></span></b> there is a static member variable <i>obj </i>of Class Singleton type.</div>
<div style="background-color: #d9d2e9; font-family: Verdana, sans-serif;">
<b><span style="color: lime;">-></span></b> when we are calling getobject() method first time from main method present in Test class,JVM(java virtual machine) look into the memory for class Singleton,if it is already present in the memory or not.</div>
<div style="background-color: #d9d2e9; font-family: Verdana, sans-serif;">
but it is first time we are loading it so JVM will load the class and its static members into the memory with no problem.</div>
<div style="background-color: #d9d2e9; font-family: Verdana, sans-serif;">
<b style="color: magenta;">-></b> Now the class is instantiated and the object is returned to the calling function main.</div>
<div style="background-color: #d9d2e9; font-family: Verdana, sans-serif;">
<b style="color: red;">-></b> If You will try again to call the getobject() method to prove me wrong you will see a message on the screen</div>
<div style="background-color: #d9d2e9; font-family: Verdana, sans-serif;">
"object of singleton class already exist"</div>
<br />
<div style="font-family: Verdana, sans-serif;">
So this is the way singleton classes are implemented and instantiated in realtime applications.</div>
<br />
<div style="color: red; font-family: "Courier New", Courier, monospace;">
Now you must be thinking !!! </div>
<div style="font-family: Verdana, sans-serif;">
What is the use of defining a singleton class when we can't create objects to use its member functions</div>
<br />
<h3 style="text-align: left;">
<span style="color: #38761d;">Answer:</span></h3>
<div style="background-color: #f3f3f3; font-family: Arial, Helvetica, sans-serif;">
We make our class singleton, when our class is to be used by multiple threads of an application.Because if all the threads are allowed to create a separate object of class for themselves then logically they will be working on different set of variables and cant solve the purpose of multiple threading.</div>
<div style="background-color: #f3f3f3; font-family: Arial, Helvetica, sans-serif;">
For Example: say there is an online shopping website,what they do.They have a products catalog in which all the products which are available in their stock are present and this product catalog is nothing but a class(singleton class).</div>
<div style="background-color: #f3f3f3; font-family: Arial, Helvetica, sans-serif;">
This product catalog class is shared between every customer(a thread speaking technically) on the website to look for their favorite product because the product catalog is same for every customer,it will not be a good idea if they load the same class separately for each customer.</div>
<br />
Thanx<br />
<br />
<br /></div>Harmender dhakahttp://www.blogger.com/profile/14082625144310167954noreply@blogger.com0tag:blogger.com,1999:blog-5284437919386903043.post-23798280561775570712012-06-12T23:42:00.001-07:002012-06-27T12:33:53.423-07:00HTTP the logic behind the web<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="color: #cc0000; font-size: large;">HTTP </span><br />
<div style="font-family: "Courier New", Courier, monospace;">
Stands for <span style="background-color: #f4cccc;">hypertext transfer protocol</span> uses <b>port no-80</b></div>
<div style="font-family: "Courier New", Courier, monospace;">
used for communication between a Browser (client) and web servers.<br />
<b>HTTP is stateless protocol:</b>stateless means it doesn't keep track of requests that have been processed or we can say HTTP cant recognize clients,if the same browser is making requests it will not be able to relate them in anyway.<br />
HTTP processes requests and forget about them.</div>
<div style="font-family: "Courier New", Courier, monospace;">
The communication between client and server happens in the form of</div>
<ul style="font-family: "Courier New", Courier, monospace; text-align: left;">
<li>Request.</li>
<li>Response.</li>
</ul>
<h4 style="font-family: "Courier New", Courier, monospace; text-align: left;">
<u>Request</u> : is a request message sent by Browser of end user to the server, this request contains following information:</h4>
<ol style="font-family: "Courier New", Courier, monospace; text-align: left;">
<li>Method of request(get/post).</li>
<li>Browser and Operating system information.</li>
<li>Query parameters.</li>
<li>server address.</li>
<li>URI(uniform resource identifier).</li>
<li>Protocol used for sending request(http).</li>
<li>Port no. used for request.</li>
<li>Security enable/disable information.</li>
<li>cookies.</li>
<li>and many other</li>
</ol>
example :<span style="background-color: orange;"> http</span>://<span style="background-color: #93c47d;">www.ctrl-f9.com</span>/<span style="background-color: #fce5cd;">login.jsp</span>?<span style="background-color: #9fc5e8;">name="hemant"&pass="123456"</span><br />
<br />
<span style="background-color: white;">Above example is a <b>http request</b> for server <b>"</b></span><span style="background-color: white;"><b>www.ctrl+f9.com"</b></span><span style="background-color: white;"> for a jsp page named <b>login.jsp</b> with two query attributes </span><span style="background-color: white;"><b>name=hemant</b> and <b>pass=123456</b>.</span><br />
<br />
<h4 style="font-family: "Courier New", Courier, monospace; text-align: left;">
<u>Response</u>: Response is to be provided by the web server on the other side.the response may be a simple HTML document or execution of a process,the result of which is then appended to the response.</h4>
<div style="font-family: "Courier New", Courier, monospace;">
<b><span style="color: red;">>></span></b> response is to be produced according to the request,where the request can be a static page,accessing database or updating some database records or some other type.</div>
<div style="font-family: "Courier New", Courier, monospace;">
<b><span style="color: red;">>></span></b> server uses the same port for sending response back to the client which was used by client to send the request. i.e a http request will always get a http response.</div>
<div style="font-family: "Courier New", Courier, monospace;">
<b><span style="color: red;">>></span></b> if response contains a html document which further contains some images or videos or some other data like shown in the example below:</div>
<div style="background-color: #cfe2f3; font-family: Times, "Times New Roman", serif;">
<i><html></i></div>
<div style="background-color: #cfe2f3; font-family: Times, "Times New Roman", serif;">
<i><head></i></div>
<div style="background-color: #cfe2f3; font-family: Times, "Times New Roman", serif;">
<i><title>example</title> </i></div>
<div style="background-color: #cfe2f3; font-family: Times, "Times New Roman", serif;">
<i></head></i></div>
<div style="background-color: #cfe2f3; font-family: Times, "Times New Roman", serif;">
<i><body></i></div>
<div style="background-color: #cfe2f3; font-family: Times, "Times New Roman", serif;">
<i><img src="http://www.ctrl-f9.com/images/hsd.jpg" width="200" height="100"/></i></div>
<div style="background-color: #cfe2f3; font-family: Times, "Times New Roman", serif;">
<i><embed src="http://www.ctrl-f9.com/videos/mysong.mpeg" width="200" height="100"/> </i></div>
<div style="background-color: #cfe2f3; font-family: Times, "Times New Roman", serif;">
<i></body></i></div>
<div style="background-color: #cfe2f3; font-family: Times, "Times New Roman", serif;">
<i></html></i></div>
<span style="font-family: "Courier New",Courier,monospace;">then, the image and video which is embedded in the page will be accessed separately with separate requests for each image or video.</span><br />
<div style="font-family: "Courier New", Courier, monospace;">
so we can say the html page will be loaded first then the other elements like images and audio video.</div>
<div style="font-family: "Courier New", Courier, monospace;">
<br /></div>
<h3 style="font-family: "Courier New", Courier, monospace; text-align: left;">
<b>Request/response Methods :</b></h3>
<h4 style="font-family: "Courier New", Courier, monospace; text-align: left;">
1. Get:-</h4>
<div style="font-family: "Courier New", Courier, monospace;">
<b><span style="color: red;">>></span></b> this method is most commonly and by default used to request static pages or the requests which don't require any processing from the server.</div>
<div style="font-family: "Courier New", Courier, monospace;">
<b><span style="color: red;">>></span></b> there is no problem if we refresh or request the same page/resources again in the same session.</div>
<div style="font-family: "Courier New", Courier, monospace;">
<b><span style="color: red;">>></span></b> uses URL to append request parameters, and don't use any separate message body for the request parameters. </div>
<h4 style="font-family: "Courier New", Courier, monospace; text-align: left;">
2. Post:-</h4>
<div style="font-family: "Courier New", Courier, monospace;">
<b><span style="color: red;">>></span></b> is most commonly used to transfer form data from client to server for performing some database transaction or some other processing with the form data.</div>
<div style="font-family: "Courier New", Courier, monospace;">
<b><span style="color: red;">>></span></b> uses a separate message body to send request parameters,it don't use URLs to append request parameters contrary to :<span style="background-color: white;"> http</span><span style="background-color: white;">:</span>/<span style="background-color: white;">/</span><span style="background-color: white;">www.ctrl+f9.com</span>/<span style="background-color: white;">login.jsp</span>?<span style="background-color: white;">name="hemant"&pass="123456"</span> </div>
<div style="font-family: "Courier New", Courier, monospace;">
<b><span style="color: red;">>></span></b> Refreshing the page or resending the page requests for pages with html forms can cause troubles sometime,so we use POST method.</div>
<span style="font-family: "Courier New",Courier,monospace;">POST method always alerts the end user before resending the form data. </span><br />
<br />
<br /></div>Harmender dhakahttp://www.blogger.com/profile/14082625144310167954noreply@blogger.com0tag:blogger.com,1999:blog-5284437919386903043.post-71729460623885143122011-11-08T06:09:00.000-08:002012-06-27T12:39:02.784-07:00jdbc<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="font-family: "Courier New",Courier,monospace;">
<span style="color: magenta;"><span style="color: black;">Guys</span> </span><u><b>JDBC</b></u><b> is a JAVA based technology</b> to provide communication between a JAVA application and any database. </div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="background-color: #ffe599; font-family: Arial,Helvetica,sans-serif;">
<i>Note: JDBC is not an acronym for <u>JAVA DATABASE CONNECTIVITY</u> it never was,it is a trademark from sun microsystem for its technology called JDBC. </i></div>
<br />
<div style="font-family: "Courier New",Courier,monospace;">
JDBC is used whenever the java windows based or Web based application u are working on needs data to be stored and accessed from a database server.</div>
<div style="background-color: white; font-family: "Courier New",Courier,monospace;">
JDBC have following advantages: </div>
<ul style="background-color: white; text-align: left;">
<li style="font-family: "Courier New",Courier,monospace;"> it is database independent. </li>
<li><span style="font-family: "Courier New",Courier,monospace;"> it is Operating System independent.</span> </li>
</ul>
<div style="font-family: "Courier New",Courier,monospace;">
But
JDBC can only be used in JAVA based application.</div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="background-color: #fce5cd; font-family: "Courier New",Courier,monospace;">
Any java process or program which communicate with database using JDBC APIs is Known as JDBC client. </div>
<div style="background-color: #fce5cd; font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="background-color: #fce5cd; font-family: "Courier New",Courier,monospace;">
A JDBC client has to: </div>
<div style="background-color: #fce5cd; font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="background-color: #fce5cd; font-family: "Courier New",Courier,monospace;">
1.) Establish connection with database server. </div>
<div style="background-color: #fce5cd; font-family: "Courier New",Courier,monospace;">
2.) Submitting Appropriate SQL statement to the database management system. </div>
<div style="background-color: #fce5cd; font-family: "Courier New",Courier,monospace;">
3.) Receiving the response from the DBMS and processing it for presentation to end user. </div>
<div style="background-color: #fce5cd; font-family: "Courier New",Courier,monospace;">
4.) Dealing with exception if any occurs. </div>
<div style="background-color: #fce5cd; font-family: "Courier New",Courier,monospace;">
5.) Dealing with transactions whenever required. </div>
<div style="background-color: #fce5cd;">
<span style="font-family: "Courier New",Courier,monospace;">6.) last and very important closing the connection.</span> </div>
<div style="background-color: #fce5cd;">
<br /></div>
<span class="Apple-style-span" style="font-size: large;"> <span style="background-color: #9fc5e8;"><br /></span></span><br />
<div style="background-color: #b6d7a8; font-family: "Courier New",Courier,monospace;">
<span class="Apple-style-span" style="font-size: large;">These are the basic components of JDBC technology:</span></div>
<div style="background-color: #b6d7a8;">
<span class="Apple-style-span" style="font-size: large;"><span style="font-size: small;">*</span><span style="font-size: small;"><span style="font-family: "Courier New",Courier,monospace;"><b>JDBC driver</b>:most important part of jdbc which acts as mediator between java programs(clients) and DBMS.</span></span></span></div>
<div style="background-color: #b6d7a8; font-family: "Courier New",Courier,monospace;">
<span class="Apple-style-span" style="font-size: small;">*<b>JDBC API</b>: is a collection of library interfaces and classes whose methods are used to perform CRUD(create,retrieve,update and delete) operations with database.</span></div>
<div style="background-color: #b6d7a8; font-family: "Courier New",Courier,monospace;">
<span class="Apple-style-span" style="font-size: small;">JDBC APIs are distributed into two packages in JAVA</span></div>
<div style="background-color: #b6d7a8; font-family: "Courier New",Courier,monospace;">
<span class="Apple-style-span" style="font-size: small;">1. java.sql;</span></div>
<div style="background-color: #b6d7a8; font-family: "Courier New",Courier,monospace;">
<span class="Apple-style-span" style="font-size: small;">2. javax.sql;</span></div>
<div style="background-color: #b6d7a8; font-family: "Courier New",Courier,monospace;">
<span class="Apple-style-span" style="font-size: small;">*<b>JDBC driver manager</b>: is a java based application we use to make our JDBC applications driver independent,because driver manager plays the role of choosing jdbc driver for our application at the run time.</span></div>
<div style="background-color: #b6d7a8; font-family: "Courier New",Courier,monospace;">
<span class="Apple-style-span" style="font-size: small;">*<b>JDBC test suits</b>: are used to test whether the selected JDBC driver is supported by the given DBMS features or not.</span></div>
<span class="Apple-style-span" style="font-size: large;"><br /></span><br />
<div style="color: orange;">
<span class="Apple-style-span" style="font-size: large;">JDBC driver architectures:</span></div>
<span class="Apple-style-span" style="font-size: large;"> <span style="color: #0b5394;">type 1 driver.</span></span><br />
<div style="color: #0b5394;">
<span class="Apple-style-span" style="font-size: large;"> type 2 driver.</span></div>
<div style="color: #0b5394;">
<span class="Apple-style-span" style="font-size: large;"> type 3 driver.</span></div>
<span class="Apple-style-span" style="font-size: large;"><span style="color: #0b5394;"> <a href="http://hsd-techonly.blogspot.in/2012/06/using-type-4-db2-driver-for.html" target="_blank">type 4 driver.</a></span></span><br />
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><br /></span></div>
<div style="background-color: #d0e0e3; color: black; font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;">type 1 driver architecture is believed to be the simplest architecture but not the most reliable one, so 98% of the Business applications of the world don't go for type 1 drivers.</span></div>
<div style="background-color: #d0e0e3; color: black; font-family: "Courier New",Courier,monospace;">
<span style="background-color: #f4cccc; font-size: small;"><span style="background-color: #d0e0e3;">the one and only driver available in type 1 architecture is</span><b style="background-color: #d0e0e3;"> JdbcOdbcDriver</b><span style="background-color: #d0e0e3;"> and is already present with the JAVA provided by sun microsystems upto java 1.6 version.</span></span></div>
<ul style="text-align: left;"></ul>
<div style="font-family: "Courier New",Courier,monospace;">
The JDBC:ODBC Bridge is installed automatically with the Java 2 SDK, Standard Edition, as package <span class="Apple-style-span" style="color: red;"><code>sun.jdbc.odbc</code>.</span>,and this is enough to start database connectivity for now.</div>
<div style="font-family: "Courier New",Courier,monospace;">
you don't need to bother about other drivers. </div>
<ul style="text-align: left;">
</ul>
<div style="font-family: "Courier New",Courier,monospace;">
<b><u>DSN(data source name):</u></b> to create a dsn for your database created with DBMS. follow these steps:-</div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<ul style="font-family: "Courier New",Courier,monospace; text-align: left;">
<li>go to control panel > system and security > admin tools > data sorces(ODBC)</li>
<li>a new window will appear.</li>
<li>select user DSN in this window.</li>
<li>click on add.</li>
<li>select the driver from the list shown.</li>
<li>click on finish.</li>
<li>enter the dsn name in the new opened pop-up window & select the database.</li>
<li>and finish</li>
</ul>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
Now you are done with creating DSN. your DSN is ready.</div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
Now that you have fullfilled basic requirements and have basic knowledge required to proceed you can start coding and follow these seven steps in your program:</div>
<div style="font-family: "Courier New",Courier,monospace;">
<b><span class="Apple-style-span" style="font-size: large;">seven basic steps in JDBC</span></b></div>
<div style="font-family: "Courier New",Courier,monospace;">
1. load the driver Class.</div>
<div style="font-family: "Courier New",Courier,monospace;">
2. define the connection URL.</div>
<div style="font-family: "Courier New",Courier,monospace;">
3. establish the connection.</div>
<div style="font-family: "Courier New",Courier,monospace;">
4. create a statement object.</div>
<div style="font-family: "Courier New",Courier,monospace;">
5. execute a querry.</div>
<div style="font-family: "Courier New",Courier,monospace;">
6. process the result.</div>
<div style="font-family: "Courier New",Courier,monospace;">
7. close the connection if not needed any more.</div>
<br />
<div style="font-family: "Courier New",Courier,monospace;">
here is an example to clarify the above steps :</div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<span class="Apple-style-span" style="font-family: Helvetica;"></span><br />
<pre></pre>
<pre style="background-color: #d9ead3;">import java.sql.* ;
class JDBC
{
public static void main( String args[] )
{
try
{
// Load the driver
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ) ;
// establish a connection to the dsn
Connection conn = </pre>
<pre style="background-color: #d9ead3;"> DriverManager.getConnection( "jdbc:odbc:DSNname","username","password");
// create a statement object using the connection object
Statement stmt = conn.createStatement() ;
// Execute the query
ResultSet rs = stmt.executeQuery( "SELECT * FROM Cust" ) ;
// process the result set
while( rs.next() )</pre>
<pre style="background-color: #d9ead3;"> {</pre>
<pre style="background-color: #d9ead3;"> System.out.println( rs.getString(column name)) ;</pre>
<pre style="background-color: #d9ead3;"> }
// Close the connection
conn.close() ;
}
catch( SQLException se )
{
System.out.println( "SQL Exception:" ) ;
System.out.println( e ) ;
}
}
}</pre>
<pre></pre>
<pre></pre>
<pre> </pre>
<pre style="font-family: "Courier New",Courier,monospace;">This was an Application using JdbcOdbcDriver which is a type-1 Driver.</pre>
<pre style="font-family: "Courier New",Courier,monospace;">If you want to learn about type-4 the most effective architecture used in realtime</pre>
<pre style="font-family: "Courier New",Courier,monospace;">have a look at my new post <a href="http://hsd-techonly.blogspot.in/2012/06/using-type-4-db2-driver-for.html">Type-4 based JDBC applications.</a></pre>
<pre style="font-family: "Courier New",Courier,monospace;"></pre>
<pre style="font-family: "Courier New",Courier,monospace;">I hope this will be helpful to you guys </pre>
<pre style="font-family: "Courier New",Courier,monospace;"> </pre>
<pre style="font-family: "Courier New",Courier,monospace;">i'll appreciate your feedback.</pre>
<pre style="font-family: "Courier New",Courier,monospace;"></pre>
</div>Harmender dhakahttp://www.blogger.com/profile/14082625144310167954noreply@blogger.com0