<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Servlet &#8211; Jitendra Zaa</title>
	<atom:link href="https://www.jitendrazaa.com/blog/category/java/servlet/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.jitendrazaa.com/blog</link>
	<description>AI, Salesforce, ServiceNow &#38; Enterprise Tech Guides</description>
	<lastBuildDate>Sat, 08 Sep 2012 19:31:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
<site xmlns="com-wordpress:feed-additions:1">87744916</site><atom:link rel="search" type="application/opensearchdescription+xml" title="Search Jitendra Zaa" href="https://www.jitendrazaa.com/blog/wp-json/opensearch/1.1/document" />	<item>
		<title>Simple guide to setup SSL in Tomcat</title>
		<link>https://www.jitendrazaa.com/blog/java/simple-guide-to-setup-ssl-in-tomcat/</link>
					<comments>https://www.jitendrazaa.com/blog/java/simple-guide-to-setup-ssl-in-tomcat/#comments</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Sat, 08 Sep 2012 19:31:49 +0000</pubDate>
				<category><![CDATA[JAVA]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[Servlet]]></category>
		<category><![CDATA[SSL]]></category>
		<category><![CDATA[tomcat]]></category>
		<guid isPermaLink="false">http://JitendraZaa.com/blog/?p=3070</guid>

					<description><![CDATA[I have enabled SSL in tomcat many times however initially I struggled to get it in running condition. So I thought to share a simple approach I am following now days. Step 1: Run tool &#8220;Keytool&#8220; provided by the JRE to create a &#8220;keystore file&#8221;. The command to run tool is: keytool -genkey -alias tomcat [&#8230;]]]></description>
										<content:encoded><![CDATA[<p style="text-align: justify;">I have enabled SSL in tomcat many times however initially I struggled to get it in running condition. So I thought to share a simple approach I am following now days.</p>
<p><strong>Step 1:</strong></p>
<p>Run tool &#8220;<strong>Keytool</strong>&#8220; provided by the JRE to create a &#8220;keystore file&#8221;.<br />
The command to run tool is:</p>
<blockquote><p>keytool -genkey -alias tomcat -keyalg RSA -keystore D:/.keyStore</p></blockquote>
<p>Where &#8220;D:/.keystore&#8221; is the path where file should be created.<br />
Instead of alias &#8220;tomcat&#8221; any other name can be used.<br />
After running above command, you will be asked many questions, so answer them correctly as shown in below image:</p>
<figure id="attachment_3071" aria-describedby="caption-attachment-3071" style="width: 627px" class="wp-caption aligncenter"><a href="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2012/09/Tomcat-SSL-keytool-to-create-keystore-file.png?ssl=1"><img data-recalc-dims="1" fetchpriority="high" decoding="async" class="size-full wp-image-3071" title="Tomcat SSL keytool to create keystore file" src="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2012/09/Tomcat-SSL-keytool-to-create-keystore-file.png?resize=627%2C319&#038;ssl=1" alt="Tomcat SSL keytool to create keystore file" width="627" height="319" /></a><figcaption id="caption-attachment-3071" class="wp-caption-text">Tomcat SSL keytool to create keystore file</figcaption></figure>
<p>Remember the password provided, as it will be needed in next step.<span id="more-3070"></span></p>
<p><strong>Step 2:</strong></p>
<p>Now, in next step go to &#8220;conf&#8221; folder of tomcat, and open file &#8220;server.xml&#8221;.<br />
There you will find lines of code something like:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;-- Define a SSL Coyote HTTP/1.1 Connector on port 8443 --&gt;
&lt;!--
&lt;Connector
           port=&quot;8443&quot; minProcessors=&quot;5&quot; maxProcessors=&quot;75&quot;
           enableLookups=&quot;true&quot; disableUploadTimeout=&quot;true&quot;
           acceptCount=&quot;100&quot; debug=&quot;0&quot; scheme=&quot;https&quot; secure=&quot;true&quot;;
           clientAuth=&quot;false&quot; sslProtocol=&quot;TLS&quot;/&gt;
--&gt;
</pre>
<p>So, to enable the SSL, uncomment above code and tweek like below:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;Connector
	protocol=&quot;org.apache.coyote.http11.Http11Protocol&quot;
	port=&quot;8443&quot; maxHttpHeaderSize=&quot;8192&quot;
    maxThreads=&quot;150&quot; minSpareThreads=&quot;25&quot; maxSpareThreads=&quot;75&quot;
    enableLookups=&quot;true&quot; disableUploadTimeout=&quot;true&quot;
    acceptCount=&quot;100&quot; scheme=&quot;https&quot; secure=&quot;true&quot;
    clientAuth=&quot;false&quot; sslProtocol=&quot;TLS&quot;
	keystoreFile=&quot;F:eclipseFrameworksapache-tomcat-5.5.31 - SSL Configured.keyStore&quot;
	keystorePass=&quot;YOURpwd&quot;/&gt;
</pre>
<p>As you can see, I have added few more attributes like:</p>
<p style="text-align: justify;"><strong>Protocol :</strong> If the APR (<strong>Apache Portable Runtime</strong>) is enabled in tomcat (maximum time it is enabled by default), then this approach will not work. so configure tomcat that we want to use Java (JSSE) connector, regardless of whether the APR library is loaded or not.<br />
<strong>keystoreFile :</strong> Full path of the keystore file creates in step 1.<br />
<strong>keystorePass:</strong> Password used while creating file in step1.</p>
<p style="text-align: justify;">After these changes, save Server.xml and navigate to: https://localhost:8443/ , As you can see in below image, SSL is enabled.</p>
<figure id="attachment_3072" aria-describedby="caption-attachment-3072" style="width: 323px" class="wp-caption aligncenter"><a href="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2012/09/Https-protocol-in-Tomcat-SSL-ENabled.png?ssl=1"><img data-recalc-dims="1" decoding="async" class="size-full wp-image-3072" title="Https protocol in Tomcat - SSL Enabled" src="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2012/09/Https-protocol-in-Tomcat-SSL-ENabled.png?resize=323%2C437&#038;ssl=1" alt="Https protocol in Tomcat - SSL Enabled" width="323" height="437" /></a><figcaption id="caption-attachment-3072" class="wp-caption-text">https protocol in Tomcat &#8211; SSL Enabled</figcaption></figure>
<p style="text-align: justify;">Now, as you can see, although we have created SSL certificate for local server, browser is showing that it is not secured.</p>
<p style="text-align: justify;">SSL verifies the authenticity of a site&#8217;s certificate by using something called a &#8220;chain of trust,&#8221; which basically means that during the handshake, SSL initiates an additional handshake with the Certificate Authority (CA) specified in your site&#8217;s certificate, to verify that you haven&#8217;t simply made up your own CA (Which actually we have done in our case <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> ).</p>
<p style="text-align: justify;">If you want to remove error, you have to get certificate from some Certificate Authority so that during handshake, accuracy of your certificate can be validated.<br />
If you have valid certificates then please <a title="Enabling SSL certificates in Tomcat" href="http://www.mulesoft.com/tomcat-ssl ">read this article</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/java/simple-guide-to-setup-ssl-in-tomcat/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3070</post-id>	</item>
		<item>
		<title>J2EE &#8211; Servlet , JSP , JSF and JMS Version Table</title>
		<link>https://www.jitendrazaa.com/blog/java/jsp/j2ee-servlet-jsp-jsf-and-jms-version-table/</link>
					<comments>https://www.jitendrazaa.com/blog/java/jsp/j2ee-servlet-jsp-jsf-and-jms-version-table/#comments</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Wed, 13 Apr 2011 10:24:00 +0000</pubDate>
				<category><![CDATA[JSP]]></category>
		<category><![CDATA[Servlet]]></category>
		<category><![CDATA[J2EE]]></category>
		<guid isPermaLink="false">http://JitendraZaa.com/blog/?p=1976</guid>

					<description><![CDATA[J2EE - Servlet , JSP , JSF and JMS Version Table]]></description>
										<content:encoded><![CDATA[<p>Hi Friends,</p>
<p>Below table gives the information about the versions of J2EE (Java Table Version).</p>

<table id="tablepress-1" class="tablepress tablepress-id-1">
<thead>
<tr class="row-1">
	<th class="column-1"><div> </div></th><th class="column-2"><div>J2EE 1.2</div></th><th class="column-3"><div><a href="http://java.sun.com/j2ee/1.3/docs/">J2EE 1.3</a></div></th><th class="column-4"><div><a href="http://java.sun.com/j2ee/1.4/docs/">J2EE 1.4</a></div></th><th class="column-5"><div>JEE 5</div></th><th class="column-6"><div> <a href="http://www.oracle.com/technetwork/java/javaee/tech/index.html">JEE 6</a><br/><a href="http://www.javabeat.net/articles/99-new-features-in-java-ee-60-1.html">new</a></div></th>
</tr>
</thead>
<tbody class="row-striping row-hover">
<tr class="row-2">
	<td class="column-1"><a href="http://en.wikipedia.org/wiki/Java_Servlet">Servlet</a></td><td class="column-2">2.2</td><td class="column-3">2.3</td><td class="column-4">2.4</td><td class="column-5">2.5 <a href="http://www.javabeat.net/articles/100-new-features-in-servlets-25-1.html">new</a></td><td class="column-6">3 <a href="http://www.javabeat.net/articles/97-new-features-in-servlets-30-1.html"> new </a></td>
</tr>
<tr class="row-3">
	<td class="column-1"><a href="http://en.wikipedia.org/wiki/JavaServer_Pages">JSP</a></td><td class="column-2">1.1</td><td class="column-3">1.2</td><td class="column-4">2.0 <a href="http://www.javabeat.net/tips/145-new-features-in-jsp-20.html">new</a></td><td class="column-5">2.1</td><td class="column-6">2.2</td>
</tr>
<tr class="row-4">
	<td class="column-1"><a href="http://en.wikipedia.org/wiki/JavaServer_Pages_Standard_Tag_Library">JSTL</a></td><td class="column-2">-</td><td class="column-3">-</td><td class="column-4">1.1</td><td class="column-5">1.2</td><td class="column-6">1.2</td>
</tr>
<tr class="row-5">
	<td class="column-1">Tomcat</td><td class="column-2">4.x</td><td class="column-3">5.x</td><td class="column-4">5.x</td><td class="column-5">6.x</td><td class="column-6">7.x</td>
</tr>
<tr class="row-6">
	<td class="column-1"><a href="http://en.wikipedia.org/wiki/Java_Message_Service">JMS</a></td><td class="column-2">1.0.2</td><td class="column-3">1.0.2</td><td class="column-4">1.1</td><td class="column-5">1.1</td><td class="column-6">1.1</td>
</tr>
<tr class="row-7">
	<td class="column-1"><a href="http://en.wikipedia.org/wiki/JavaServer_Faces">JSF</a></td><td class="column-2">-</td><td class="column-3">-</td><td class="column-4">-</td><td class="column-5">1.2</td><td class="column-6">2.0 <a href="http://www.javabeat.net/tips/116-new-features-in-jsf-20.html">new</a></td>
</tr>
<tr class="row-8">
	<td class="column-1">J2EE Release Date</td><td class="column-2">Dec 12, 1999</td><td class="column-3">Sep 24, 2001</td><td class="column-4">Nov 11, 2003</td><td class="column-5">May 11 , 2006</td><td class="column-6">Dec 2009</td>
</tr>
<tr class="row-9">
	<td class="column-1"><a href="http://en.wikipedia.org/wiki/Enterprise_JavaBean">EJB</a></td><td class="column-2">1.1</td><td class="column-3">2.0</td><td class="column-4">2.1</td><td class="column-5">3.0</td><td class="column-6">3.0</td>
</tr>
<tr class="row-10">
	<td class="column-1">Extra Info</td><td class="column-2">-</td><td class="column-3">-</td><td class="column-4">-</td><td class="column-5">-</td><td class="column-6">JDK 5 and above required</td>
</tr>
</tbody>
</table>

]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/java/jsp/j2ee-servlet-jsp-jsf-and-jms-version-table/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1976</post-id>	</item>
		<item>
		<title>Create Servlet using Annotation &#8211; Servlet 3.0</title>
		<link>https://www.jitendrazaa.com/blog/java/servlet/create-servlet-using-annotation/</link>
					<comments>https://www.jitendrazaa.com/blog/java/servlet/create-servlet-using-annotation/#respond</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Wed, 13 Apr 2011 09:53:55 +0000</pubDate>
				<category><![CDATA[Servlet]]></category>
		<category><![CDATA[J2EE]]></category>
		<guid isPermaLink="false">http://JitendraZaa.com/blog/?p=1959</guid>

					<description><![CDATA[Create the Servlet without any deployment descriptor. Using Annotations]]></description>
										<content:encoded><![CDATA[<p><a title="Version table of J2EE" href="https://jitendrazaa.com/blog/java/servlet/j2ee-servlet-jsp-jsf-and-jms-version-table/" target="_blank">Before reading this article, i will suggest readers to go through the version table of J2EE.</a></p>
<p>One of the most appraised feature of Servlet 3.0 is the support of the annotations to create the Servlet. Before  this the Deployment Descriptor (web.xml) was used to create the servlet mapping , defining init parameters etc. All the settings can be now achieved without any entry in web.xml.</p>
<p><span style="text-decoration: underline;"><strong>Prerequisite:</strong></span></p>
<ol>
<li>JDK 5.0 and above.</li>
<li>Tomcat 7</li>
<li>Eclipse 3.6 &#8211; Helios (as of now, it only supports Tomcat 7)</li>
</ol>
<p>We will start with creating the Servlet class :<span id="more-1959"></span></p>
<pre class="brush: java; highlight: [13,14,15,16,17,18,19,20,21]; title: ; notranslate">
package com.g2.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(
		name=&quot;TestServlet&quot;,
		urlPatterns={&quot;/test&quot;,&quot;/testServlet&quot;},
		initParams={
				@WebInitParam(name=&quot;db&quot;,value=&quot;Oracle&quot;),
				@WebInitParam(name=&quot;jdk&quot;,value=&quot;6&quot;),
				@WebInitParam(name=&quot;tomcat&quot;,value=&quot;7&quot;)
				}
			)
public class ServletUsingAnnotation extends HttpServlet{

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		PrintWriter pw = res.getWriter();

		res.setContentType(&quot;text/html&quot;);
		pw.write(&quot;Running Servlet using Annotation..&quot;);
		pw.write(&quot;&lt;br /&gt;&quot;);
		pw.write(&quot;-- Init parameters --&quot;);
		pw.write(&quot;&lt;br /&gt;&quot;);
		pw.write(&quot;db : &quot;+this.getInitParameter(&quot;db&quot;));
		pw.write(&quot;&lt;br /&gt;&quot;);
		pw.write(&quot;jdk : &quot;+this.getInitParameter(&quot;jdk&quot;));
		pw.write(&quot;&lt;br /&gt;&quot;);
		pw.write(&quot;tomcat : &quot;+this.getInitParameter(&quot;tomcat&quot;));
	}
}
</pre>
<p>As you can see in above code, we have used annotation &#8220;<strong>@WebServlet</strong>&#8221; to declare this class as servlet. to pass the init parameter to servlet, we have used &#8220;<strong>@WebInitParam</strong>&#8220;. Before Servlet 2.5 we were not able to specify multiple url for the same servlet. As you can see in above code, we have given two URLs for the same servlet.</p>
<p>Create &#8220;index.jsp&#8221; page :</p>
<pre class="brush: xml; title: ; notranslate">
&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=ISO-8859-1&quot;
    pageEncoding=&quot;ISO-8859-1&quot;%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859-1&quot;&gt;
&lt;title&gt;Creating servlet using Annotation&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;a href=&quot;../test&quot;&gt;Servlet using Annotation&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>After clicking on hyperlink on page &#8220;index.jsp&#8221; Servlet will open with following output.</p>
<blockquote><p>Running Servlet using Annotation..<br />
&#8212; Init parameters &#8212;<br />
db : Oracle<br />
jdk : 6<br />
tomcat : 7</p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/java/servlet/create-servlet-using-annotation/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1959</post-id>	</item>
		<item>
		<title>Step by Step DWR Application &#8211; Simple AJAX in JAVA</title>
		<link>https://www.jitendrazaa.com/blog/java/jsp/step-by-step-dwr-application-simple-ajax-in-java/</link>
					<comments>https://www.jitendrazaa.com/blog/java/jsp/step-by-step-dwr-application-simple-ajax-in-java/#comments</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Tue, 15 Mar 2011 11:37:30 +0000</pubDate>
				<category><![CDATA[JSP]]></category>
		<category><![CDATA[Servlet]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[DWR]]></category>
		<guid isPermaLink="false">http://JitendraZaa.com/blog/?p=1664</guid>

					<description><![CDATA[Step by Step DWR Application - Simple AJAX in JAVA]]></description>
										<content:encoded><![CDATA[<p>In this article, i am going to explain the step by step approach to create the <strong>DWR (Direct Web Remoting)</strong> application in JAVA.</p>
<p><strong>DWR consists of two main parts:</strong></p>
<ul>
<li>A Java Servlet running on the server that processes requests and sends responses back to the browser.</li>
<li>JavaScript running in the browser that sends requests and can dynamically update the webpage.</li>
</ul>
<figure id="attachment_1665" aria-describedby="caption-attachment-1665" style="width: 439px" class="wp-caption aligncenter"><a href="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/03/How-DWR-works-in-Java.jpg?ssl=1"><img data-recalc-dims="1" decoding="async" class="size-full wp-image-1665 " title="How DWR works in Java" src="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/03/How-DWR-works-in-Java.jpg?resize=439%2C175&#038;ssl=1" alt="How DWR works in Java" width="439" height="175" /></a><figcaption id="caption-attachment-1665" class="wp-caption-text">How DWR works in Java</figcaption></figure>
<p><span id="more-1664"></span></p>
<p style="text-align: left;"><span style="text-decoration: underline;"><strong>JavaScript file that must be included in your application for DWR:</strong></span></p>
<p style="text-align: left;">1. Auto generated Javascript of your equivalent class. in this case, my java class name is &#8220;Message&#8221;, so the javascript complete path is &#8220;<strong>/&lt;App NAME&gt;/dwr/interface/&lt;YOUR CLASS NAME&gt;.js</strong>&#8220;.</p>
<p style="text-align: left;">in my application the path is &#8220;/SimpleDWR/dwr/interface/Message.js&#8221;</p>
<p style="text-align: left;">2. DWR engine file : <strong>&#8220;/&lt;APP NAME&gt;/dwr/engine.js&#8221;</strong>. This is responsible for marshaling of Java objects/Values between Client and Server.</p>
<p style="text-align: left;">3. This is not necessary, but it provides nice set of utility methods to work with HTML code :</p>
<p style="text-align: left;">&#8220;<strong>/&lt;APP NAME&gt;/dwr/util.js</strong>&#8220;</p>
<p>Lets start with our project:</p>
<p><strong>Step 1:</strong> Create a Dynamic Website.<br />
<strong>Step 2: </strong>Download and add the dwr jar files in the &#8220;web-inf/ lib&#8221; folder.<br />
Download location : <a title="Download link" href="http://directwebremoting.org/dwr/downloads/index.html" target="_blank">http://directwebremoting.org/dwr/downloads/index.html</a><br />
<strong>Step 3:</strong> add following code in web.xml.</p>
<pre class="brush: xml; title: ; notranslate">
  &lt;servlet&gt;
  &lt;servlet-name&gt; dwr-invoker &lt;/servlet-name&gt;
  &lt;display-name&gt; DWR Servlet &lt;/display-name&gt;
  &lt;servlet-class&gt; org.directwebremoting.servlet.DwrServlet &lt;/servlet-class&gt;
  &lt;init-param&gt;
     &lt;param-name&gt; debug &lt;/param-name&gt;
     &lt;param-value&gt; true &lt;/param-value&gt;
  &lt;/init-param&gt;
&lt;/servlet&gt;

&lt;servlet-mapping&gt;
  &lt;servlet-name&gt; dwr-invoker &lt;/servlet-name&gt;
  &lt;url-pattern&gt; /dwr/* &lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;

</pre>
<p><strong>Step 4:</strong> create a dwr.xml alongside the web.xml and add below code:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;!DOCTYPE dwr PUBLIC
   &quot;-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN&quot;
   &quot;http://www.getahead.ltd.uk/dwr/dwr10.dtd&quot;&gt;
&lt;dwr&gt;
&lt;allow&gt;
   &lt;create creator=&quot;new&quot; javascript=&quot;Message&quot;&gt;
   &lt;param name=&quot;class&quot; value=&quot;com.G2.POJO.Message&quot;/&gt;
   &lt;/create&gt;
&lt;/allow&gt;
&lt;/dwr&gt;
</pre>
<p>As you can see in above code, i have added the creator attribute which will tell the dwr engine to create the javascript object named &#8220;Message&#8221;, using which we can call the server side methods.</p>
<p><strong>Step 5: </strong>create Java class, which will be called by the DWR javascript:</p>
<pre class="brush: java; title: ; notranslate">
package com.G2.POJO;

public class Message {
	public String getMessage() {
		return &quot;Hello DWR from Server&quot;;
	}
}
</pre>
<p><strong>Step 6:</strong> create index.html with below code:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859-1&quot;&gt;
&lt;title&gt;DWR Demo&lt;/title&gt;
&lt;script type='text/javascript' src='/SimpleDWR/dwr/interface/Message.js'&gt;&lt;/script&gt;
&lt;script type='text/javascript' src='/SimpleDWR/dwr/engine.js'&gt;&lt;/script&gt;
&lt;script type='text/javascript' src='/SimpleDWR/dwr/util.js'&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;input value=&quot;click me!!!&quot; type=&quot;button&quot; onclick=&quot;update();&quot; /&gt;
&lt;div style=&quot;background-color: #ffeaa7;font-weight: bold;width: 300px;&quot; id=&quot;divResponse&quot;&gt;
Message From Server&lt;/div&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
function update()
{
    Message.getMessage(function(data) {
		dwr.util.setValue(&quot;divResponse&quot;, data);
	  });
}
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Start the application and Test by writing below URL:</p>
<blockquote><p>http://localhost:8080/[YOUR-WEBAPP]/dwr/</p></blockquote>
<p>You will see a page containing the information about class added in step 5.<br />
Now start the application normally, and your program will run showing that how easy is AJAX with DWR.</p>
<figure id="attachment_1666" aria-describedby="caption-attachment-1666" style="width: 318px" class="wp-caption aligncenter"><a href="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/03/Simple-AJAX-in-JAVA-using-DWR.jpg?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" class="size-full wp-image-1666" title="Simple AJAX in JAVA using DWR" src="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/03/Simple-AJAX-in-JAVA-using-DWR.jpg?resize=318%2C269&#038;ssl=1" alt="Simple AJAX in JAVA using DWR" width="318" height="269" /></a><figcaption id="caption-attachment-1666" class="wp-caption-text">Simple AJAX in JAVA using DWR</figcaption></figure>
<p>Clicking on button, it will display the response coming from the java code.</p>
<p>You can <a title="Download WAR file of DWR" href="http://directwebremoting.org/dwr/downloads/index.html" target="_blank">download the war file of the examples</a> from here and explore the DWR in depth.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/java/jsp/step-by-step-dwr-application-simple-ajax-in-java/feed/</wfw:commentRss>
			<slash:comments>20</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1664</post-id>	</item>
		<item>
		<title>Servlet, Hibernate, jQuery and Ajax based google like chat</title>
		<link>https://www.jitendrazaa.com/blog/java/jsp/servlet-hibernate-jquery-and-ajax-based-google-like-chat/</link>
					<comments>https://www.jitendrazaa.com/blog/java/jsp/servlet-hibernate-jquery-and-ajax-based-google-like-chat/#comments</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Mon, 14 Mar 2011 11:35:32 +0000</pubDate>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[Servlet]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Query Browser]]></category>
		<guid isPermaLink="false">http://JitendraZaa.com/blog/?p=1643</guid>

					<description><![CDATA[Servlet, Hibernate, jQuery and Ajax based google like chat with source code]]></description>
										<content:encoded><![CDATA[<p>Hi, In this article, my aim is to create an application which uses the concept of <strong>Hibernate in Servlet with Ajax support of Jquery</strong>.<br />
Below figure can give you the idea of final look and feel of the complete application:</p>
<figure id="attachment_1646" aria-describedby="caption-attachment-1646" style="width: 440px" class="wp-caption aligncenter"><a href="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/03/Servlet-Hibernate-jQuery-and-Ajax-based-google-like-chat.jpg?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" class="size-full wp-image-1646 " title="Servlet, Hibernate, jQuery and Ajax based google like chat" src="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/03/Servlet-Hibernate-jQuery-and-Ajax-based-google-like-chat.jpg?resize=440%2C293&#038;ssl=1" alt="Servlet, Hibernate, jQuery and Ajax based google like chat" width="440" height="293" /></a><figcaption id="caption-attachment-1646" class="wp-caption-text">Servlet, Hibernate, jQuery and Ajax based google like chat</figcaption></figure>
<p><strong><span id="more-1643"></span>DataBase:</strong><br />
Here I am using the MySQL Database for saving the messages entered by the users:<br />
Copy below code to create the table in database named &#8220;test&#8221;.</p>
<pre class="brush: sql; title: ; notranslate">
DROP TABLE IF EXISTS `test`.`chatmessage`;
CREATE TABLE  `test`.`chatmessage` (
`Id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`Message` varchar(4000) NOT NULL,
`userName` varchar(100) NOT NULL,
`MsgTime` varchar(45) NOT NULL,
`colorSelected` varchar(45) NOT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=400 DEFAULT CHARSET=latin1;
</pre>
<p><strong>Hibernate:</strong><br />
Now, start with the hibernate part of the code.<br />
Create the <strong>POJO (Plain Old Java Object)</strong>, which mapps the table &#8220;chatmessage&#8221; to the object in our application.</p>
<pre class="brush: java; title: ; notranslate">
package com.G2.pojo;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ChatMessage {
	private String Message;
	private String userName;
	private String MsgTime;
	private Long Id;
	private String colorSelected;

	public String getColorSelected() {
		return colorSelected;
	}

	public void setColorSelected(String colorSelected) {
		this.colorSelected = colorSelected;
	}

	public String getMessage() {
		return Message;
	}

	public void setMessage(String message) {
		Message = message;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Long getId() {
		return Id;
	}

	public void setId(Long id) {
		Id = id;
	}

	public String getMsgTime() {
		DateFormat dateFormat = new SimpleDateFormat(&quot;yyyy/MM/dd HH:mm:ss&quot;);
		Date date = new Date();
		return dateFormat.format(date);
	}

	public void setMsgTime(String msgTime) {
		MsgTime = msgTime;
	}
}
</pre>
<p>After creating the POJO class, create the hibernate configuration file, from which the application will come to know that how to map the table&#8217;s column from database to the class members, database name and so on. The hibernate configuration file name must be &#8220;<strong>hibernate.cfg.xml</strong>&#8220; and it should reside at the root of source code.<br />
Code of hibernate.cfg.xml:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;?xml version='1.0' encoding='utf-8'?&gt;
&lt;!DOCTYPE hibernate-configuration PUBLIC
&quot;-//Hibernate/Hibernate Configuration DTD//EN&quot;
&quot;http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd&quot;&gt;

&lt;hibernate-configuration&gt;
&lt;session-factory name=&quot;java:hibernate/SessionFactory&quot;&gt;
      &lt;property name=&quot;hibernate.connection.driver_class&quot;&gt; com.mysql.jdbc.Driver&lt;/property&gt;
      &lt;property name=&quot;hibernate.connection.url&quot;&gt; jdbc:mysql://localhost/test&lt;/property&gt;
      &lt;property name=&quot;hibernate.connection.username&quot;&gt; username&lt;/property&gt;
      &lt;property name=&quot;hibernate.connection.password&quot;&gt; pwd&lt;/property&gt;
      &lt;property name=&quot;hibernate.connection.pool_size&quot;&gt; 10&lt;/property&gt;
      &lt;property name=&quot;show_sql&quot;&gt; true&lt;/property&gt;
      &lt;property name=&quot;dialect&quot;&gt; org.hibernate.dialect.MySQLDialect&lt;/property&gt;
      &lt;property name=&quot;hibernate.hbm2ddl.auto&quot;&gt; update&lt;/property&gt;
      &lt;!-- Mapping files --&gt;
      &lt;mapping resource=&quot;ChatMessage.hbm.xml&quot;/&gt;
&lt;/session-factory&gt;
&lt;/hibernate-configuration&gt;
</pre>
<p>To keep the code simple, normally I add different configurations for different classes in hibernate.<br />
What is the need of dialect in hibernate:<br />
Databases implement subtle differences in the SQL they use. Things such as data types for example vary across databases Or database specific functionality &#8211; selecting the top n rows is different depending on the database. The dialect abstracts this so you don&#8217;t have to worry about it. <strong>In short the dialect property internally creates the highly optimized query for underlying database.</strong><br />
Here also, as you can see, I have included file ChatMessage.hbm.xml file.<br />
Code of ChatMessage.hbm.xml:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;!DOCTYPE hibernate-mapping PUBLIC
&quot;-//Hibernate/Hibernate Mapping DTD 3.0//EN&quot;
&quot;http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd&quot;&gt;

&lt;hibernate-mapping default-lazy=&quot;false&quot;&gt;
	&lt;class name=&quot;com.G2.pojo.ChatMessage&quot; table=&quot;chatmessage&quot;&gt;
		&lt;id name=&quot;id&quot; type=&quot;long&quot; column=&quot;Id&quot;&gt;
			&lt;generator class=&quot;identity&quot; /&gt;
		&lt;/id&gt;
		&lt;property name=&quot;Message&quot;&gt;
			&lt;column name=&quot;Message&quot; /&gt;
		&lt;/property&gt;
		&lt;property name=&quot;userName&quot;&gt;
			&lt;column name=&quot;userName&quot; /&gt;
		&lt;/property&gt;
		&lt;property name=&quot;MsgTime&quot;&gt;
			&lt;column name=&quot;MsgTime&quot; /&gt;
		&lt;/property&gt;
		&lt;property name=&quot;colorSelected&quot;&gt;
			&lt;column name=&quot;colorSelected&quot; /&gt;
		&lt;/property&gt;
	&lt;/class&gt;
&lt;/hibernate-mapping&gt;
</pre>
<p><strong>UI Part of the application:</strong><br />
For the UI, I have written lots of css classes, this is not possible to write complete code here. Please check the code from download link provided at the bottom of the article.<br />
UI of the chat window is created to give the feel of the Gmail chat application as shown in below image:</p>
<figure id="attachment_1648" aria-describedby="caption-attachment-1648" style="width: 353px" class="wp-caption aligncenter"><a href="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/03/Gmail-like-chat-window-with-emoticons.jpg?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" class="size-full wp-image-1648" title="Gmail like chat window with emoticons" src="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/03/Gmail-like-chat-window-with-emoticons.jpg?resize=353%2C310&#038;ssl=1" alt="Gmail like chat window with emoticons" width="353" height="310" /></a><figcaption id="caption-attachment-1648" class="wp-caption-text">Gmail like chat window with emoticons</figcaption></figure>
<p>Here, find the javascript code, which is responsible to send the AJAX request to the servlet, and getting back the response code:</p>
<pre class="brush: jscript; title: ; notranslate">
&lt;script type=&quot;text/javascript&quot;&gt;
	var uEnteredName = prompt(&quot;Please Enter Your Name&quot;);
	$(&quot;#chat_Header&quot;).html(uEnteredName);
	$(&quot;#msg&quot;).focus();

	function openEmot()
	{
		var $ele = $(&quot;#emoticons&quot;);
		var visibility = $ele.css('display');

		if(visibility == 'none')
		{
			$ele.show();
		}
		else{
			$ele.hide();
		}
	}

	function smileyCode(iconCode)
	{
		var $msgEle = $(&quot;#msg&quot;);
		$msgEle.val($msgEle.val() + iconCode);
		var $ele = $(&quot;#emoticons&quot;);
		$ele.hide();

		$msgEle.focus();
	}

	function saveChats() {
		var uName = $(&quot;#chat_Header&quot;).html();
		if (uName == '') {
			alert('Please enter your name ');
			return false;
		}
		var msg = $(&quot;#msg&quot;).val();
		//var oldMsg = $(&quot;#chat-area&quot;).html();
		var colorCode = $('input&#x5B;name=nameColor]:checked', '#send-message-area')
				.val();

		$.ajax( {
					type : &quot;POST&quot;,
					data : &quot;uName=&quot; + uName + &quot;&amp;msg=&quot; + msg + &quot;&amp;colorCode=&quot;
							+ colorCode,
					url : &quot;Chatprocess.do&quot;,
					error : function(xhr, ajaxOptions, thrownError) {
						alert(xhr.status);
						alert(thrownError);
					},
					success : function(data) {
						$(&quot;#chat-area&quot;).html(data);
						$(&quot;#ChatAtBigScreen&quot;).html(data);
						document .getElementById('chat-area').scrollTop = document .getElementById('chat-area').scrollHeight;
						document .getElementById('ChatAtBigScreen').scrollTop = document .getElementById('ChatAtBigScreen').scrollHeight;
					}
				});
		return false;
	}
	$('#msg').keyup(function(e) {

		if (e.keyCode == 13) {
			saveChats();
			$(&quot;#msg&quot;).val('');
		}
	});
&lt;/script&gt;
</pre>
<p>Utility class, responsible to save the chat in the database using hibernate:</p>
<pre class="brush: java; title: ; notranslate">
package com.G2.Model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.G2.pojo.ChatMessage;

public class DBManager {
      // Read Hibernate.cfg.xml
	static Configuration cf = new Configuration().configure();
	static SessionFactory factory = cf.buildSessionFactory();

	public static void saveChat(ChatMessage chat) {

		Session session = null;
		try {

			session = factory.openSession();
			Transaction tx = session.beginTransaction();

			session.save(chat);
			tx.commit();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) {
				// I faced problem here, if below line is not written then data automatically gets deleted after insertion
				session.flush();
				session.close();
			}
		}
	}

	public static List&lt;ChatMessage&gt; getMessages() {
		List&lt;ChatMessage&gt; MessageList = new ArrayList&lt;ChatMessage&gt;();
		Session session = null;
		try {
			session = factory.openSession();
			String SQL_QUERY = &quot;from ChatMessage c&quot;;
			Query query = session.createQuery(SQL_QUERY);
			Iterator&lt;ChatMessage&gt; it = query.iterate();
			while (it.hasNext()) {
				ChatMessage c = it.next();
				MessageList.add(c);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}

		return MessageList;
	}
}
</pre>
<p>As shown in above code, to work with hibernate, our application will need to read the configuration file of hibernate by below line of code:</p>
<pre class="brush: java; title: ; notranslate">
static Configuration cf = new Configuration().configure();
</pre>
<p>Now, we will need to create the session object from the SessionFactory class:</p>
<pre class="brush: java; title: ; notranslate">
static SessionFactory factory = cf.buildSessionFactory();
Session session = factory.openSession();
</pre>
<p>Create the object of transaction :</p>
<pre class="brush: java; title: ; notranslate">
Transaction tx = session.beginTransaction();
</pre>
<p>To save the object in the database use <strong>save()</strong> method of the transaction.<br />
To get the list of chat messages from the database :</p>
<pre class="brush: java; title: ; notranslate">
String SQL_QUERY = &quot;from ChatMessage c&quot;;
Query query = session.createQuery(SQL_QUERY);
Iterator&lt;ChatMessage&gt; it = query.iterate();
</pre>
<p>Here object of <strong>org.hibernate.Query</strong> is created and using method iterate(), it gives all the records from the database as a object. (That is what <strong>ORM – Object Relation Mapping</strong>)</p>
<p><strong>Servlet Code:</strong></p>
<pre class="brush: java; title: ; notranslate">
package com.G2.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.G2.Model.DBManager;
import com.G2.pojo.ChatMessage;

public class ChatProcess extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String uName = req.getParameter(&quot;uName&quot;);
		String msg = req.getParameter(&quot;msg&quot;);
		String colorCode = req.getParameter(&quot;colorCode&quot;);

		ChatMessage chat = new ChatMessage();
		chat.setMessage(msg);
		chat.setUserName(uName);
		chat.setColorSelected(colorCode);

		DBManager.saveChat(chat);
		PrintWriter out = resp.getWriter();
		List&lt;ChatMessage&gt; msgList = DBManager.getMessages();

		StringBuilder sb = new StringBuilder();

		for (ChatMessage chatMsg : msgList) {
			sb.append(&quot;&lt;span style='background:#&quot;
					+ chatMsg.getColorSelected() + &quot;'&gt;&quot; + chatMsg.getUserName()
					+ &quot;&lt;/span&gt;&quot; + chatMsg.getMessage() + &quot;&lt;br/&gt;&lt;br/&gt;&quot;);
		}

		out.print(replaceEmoticons(sb.toString()));
	}

	private String replaceEmoticons(String msg) {
		String imgTag = &quot;&lt;img src=&quot;../images/smiley/{PH}.gif&quot;&gt;&quot;;
		String placeHolder = &quot;{PH}&quot;;

		SmileyCodes smileyCode = new SmileyCodes();
		HashMap&lt;String, String&gt; codeMap = smileyCode.getSmileyMap();

		for (Object key: codeMap.keySet()) {
			String val = codeMap.get(key);
			if(msg.contains(key.toString()))
			{
				msg = msg.replace(key.toString(), imgTag.replace(placeHolder,val));
			}
		}

		return msg;
	}

}
</pre>
<p>This servlet class is called by the ajax code, and from here the message is saved using DBManager class.<br />
Servlet mapping in <strong>web.config</strong></p>
<pre class="brush: xml; title: ; notranslate">
	&lt;servlet&gt;
		&lt;servlet-name&gt;Chatprocess&lt;/servlet-name&gt;
		&lt;servlet-class&gt;com.G2.servlets.ChatProcess&lt;/servlet-class&gt;
	&lt;/servlet&gt;

	&lt;servlet-mapping&gt;
		&lt;servlet-name&gt;Chatprocess&lt;/servlet-name&gt;
		&lt;url-pattern&gt;/Pages/Chatprocess.do&lt;/url-pattern&gt;
	&lt;/servlet-mapping&gt;
</pre>
<p><strong> Emoticon /Smiley part – XML Module: </strong></p>
<p>As you can see in the servlet code,</p>
<pre class="brush: java; title: ; notranslate">
SmileyCodes smileyCode = new SmileyCodes();
</pre>
<p>SmileyCodes class is responsible to render the symbol as a smiley. For the performance purpose, the smiley code is saved in xml file with corresponding image file name. Below class reads the xml and creates the HashMap (Only once).</p>
<pre class="brush: java; title: ; notranslate">
package com.G2.servlets;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class SmileyCodes {

	private static HashMap&lt;String, String&gt; smileyCodes = null;

	public static void main(String args&#x5B;]) {
		new SmileyCodes().replaceCodeBySmiley();
	}

	public HashMap&lt;String, String&gt; getSmileyMap() {
		if (smileyCodes == null) {
			replaceCodeBySmiley();
		}
		return smileyCodes;

	}

	private void replaceCodeBySmiley() {

		try {

			if (smileyCodes == null) {
				SmileyCodes testMain = new SmileyCodes();
				InputStream st = testMain.getClass().getResourceAsStream(&quot;/SmileyCode.xml&quot;);
				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
				DocumentBuilder builder;

				smileyCodes = new HashMap&lt;String, String&gt;();
				builder = factory.newDocumentBuilder();
				Document doc = builder.parse(st);

				// Get Root Node and its child
				Node root = doc.getDocumentElement();
				NodeList childNodes = root.getChildNodes();

				for (int i = 0; i &lt; childNodes.getLength(); i++) {
					replaceCodeBySmiley(childNodes.item(i));
				}
			}

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private void replaceCodeBySmiley(Node node) {

		if (node.getNodeType() == Node.ELEMENT_NODE) {
			Element e = (Element) node;
			String&#x5B;] keyVal = e.getTextContent().trim().split(&quot;#&quot;);
			smileyCodes.put(keyVal&#x5B;0].trim(), keyVal&#x5B;1].trim());
		}
	}

	private String nodeType(short type) {
		switch (type) {
		case Node.ELEMENT_NODE:
			return &quot;Element&quot;;
		case Node.DOCUMENT_TYPE_NODE:
			return &quot;Document type&quot;;
		case Node.ENTITY_NODE:
			return &quot;Entity&quot;;
		case Node.ENTITY_REFERENCE_NODE:
			return &quot;Entity reference&quot;;
		case Node.NOTATION_NODE:
			return &quot;Notation&quot;;
		case Node.TEXT_NODE:
			return &quot;Text&quot;;
		case Node.COMMENT_NODE:
			return &quot;Comment&quot;;
		case Node.CDATA_SECTION_NODE:
			return &quot;CDATA Section&quot;;
		case Node.ATTRIBUTE_NODE:
			return &quot;Attribute&quot;;
		case Node.PROCESSING_INSTRUCTION_NODE:
			return &quot;Attribute&quot;;
		}
		return &quot;Unidentified&quot;;
	}

}
</pre>
<p><a href="https://jitendrazaa.com/blog/wp-content/uploads/2011/03/JQuery-Chat.zip">Download Complete code &#8211; JQuery Chat in Hibernate</a> (Change extension from zip to rar )</p>
<p>Download 2 &#8211; <a href="https://jitendrazaa.com/blog/wp-content/uploads/2011/03/JQueryChat.zip">JQueryChat</a></p>
<p><strong>Note:</strong></p>
<p>Following jar files will be needed:</p>
<blockquote><p>antlr-2.7.6.jar<br />
commons-collections-3.1.jar<br />
dom4j-1.6.1.jar<br />
hibernate-jpa-2.0-api-1.0.0.Final.jar<br />
hibernate-testing.jar<br />
hibernate3.jar<br />
javassist-3.12.0.GA.jar<br />
jta-1.1.jar<br />
mysql-connector-java-3.1.14-bin.jar<br />
slf4j-api-1.6.1.jar</p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/java/jsp/servlet-hibernate-jquery-and-ajax-based-google-like-chat/feed/</wfw:commentRss>
			<slash:comments>14</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1643</post-id>	</item>
		<item>
		<title>Read File Present inside Java and J2EE Project</title>
		<link>https://www.jitendrazaa.com/blog/java/servlet/read-file-present-inside-java-and-j2ee-project/</link>
					<comments>https://www.jitendrazaa.com/blog/java/servlet/read-file-present-inside-java-and-j2ee-project/#respond</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Thu, 24 Feb 2011 17:54:37 +0000</pubDate>
				<category><![CDATA[Servlet]]></category>
		<category><![CDATA[JAVA]]></category>
		<guid isPermaLink="false">http://JitendraZaa.com/blog/?p=1586</guid>

					<description><![CDATA[How to Read File Present inside Java and J2EE Project]]></description>
										<content:encoded><![CDATA[<figure id="attachment_1587" aria-describedby="caption-attachment-1587" style="width: 249px" class="wp-caption aligncenter"><a href="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/02/Read-File-Present-inside-Java-and-J2EE-Project.jpg?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" class="size-full wp-image-1587" title="Read File Present inside Java and J2EE Project" src="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/02/Read-File-Present-inside-Java-and-J2EE-Project.jpg?resize=249%2C361&#038;ssl=1" alt="Read File Present inside Java and J2EE Project" width="249" height="361" /></a><figcaption id="caption-attachment-1587" class="wp-caption-text">Read File Present inside Java and J2EE Project</figcaption></figure>
<p>Consider above project hierarchy, we want to read the file &#8220;hibernate.cfg.xml&#8221; in &#8220;TestMain.java&#8221;. If we try to get the absolute path using &#8220;<strong>(new File(&#8220;&#8221;)).getAbsolutePath()</strong>&#8220; then it will give the path of eclipse in case we are using eclipse or tomcat from where the application is running. This problem occurs mainly in file upload functionality.<br />
Below code can be used to solve this type of problem.<span id="more-1586"></span></p>
<pre class="brush: java; title: ; notranslate">
package com.G2.servlets;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class TestMain {
	/**
	 * @param args
	 */
	public static void main(String&#x5B;] args) throws Exception {
		TestMain testMain = new TestMain();
		InputStream st = testMain.getClass().getResourceAsStream(&quot;/hibernate.cfg.xml&quot;);
		BufferedReader in = new BufferedReader(new InputStreamReader(st));
		String line;
		while((line = in.readLine()) != null)
		{
			System.out.println(line);
		}
	}
}
</pre>
<p>Now consider below situation:</p>
<figure id="attachment_1588" aria-describedby="caption-attachment-1588" style="width: 310px" class="wp-caption aligncenter"><a href="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/02/Read-File-Present-inside-J2EE-Project.jpg?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" class="size-full wp-image-1588" title="Read File Present inside J2EE Project" src="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/02/Read-File-Present-inside-J2EE-Project.jpg?resize=310%2C383&#038;ssl=1" alt="Read File Present inside J2EE Project" width="310" height="383" /></a><figcaption id="caption-attachment-1588" class="wp-caption-text">Read File Present inside J2EE Project</figcaption></figure>
<p>You need to read the file present in folder &#8220;WebContent&#8221;. To read the file, we can use here <strong>Servlet Context</strong>.</p>
<pre class="brush: java; title: ; notranslate">
InputStream st= getServletContext().getResourceAsStream(&quot;/css/style.css&quot;);
		BufferedReader in = new BufferedReader(new InputStreamReader(st));
		String line;
		while((line = in.readLine()) != null)
		{
			System.out.println(line);
		}

</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/java/servlet/read-file-present-inside-java-and-j2ee-project/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1586</post-id>	</item>
		<item>
		<title>Difference in SendRedirect() and RequestDispatcher() in Servlet</title>
		<link>https://www.jitendrazaa.com/blog/java/servlet/difference-in-sendredirect-and-requestdispatcher-in-servlet/</link>
					<comments>https://www.jitendrazaa.com/blog/java/servlet/difference-in-sendredirect-and-requestdispatcher-in-servlet/#comments</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Sat, 12 Feb 2011 19:17:14 +0000</pubDate>
				<category><![CDATA[Servlet]]></category>
		<category><![CDATA[J2EE]]></category>
		<guid isPermaLink="false">http://JitendraZaa.com/blog/?p=1501</guid>

					<description><![CDATA[difference in SendRedirect() and RequestDispatcher() in Servlet]]></description>
										<content:encoded><![CDATA[<p><strong>SendRedirect</strong></p>
<ul>
<li>This is the method of object <strong>HttpServlerResponse</strong>.</li>
<li>Request is redirected to client (Browser), and it will process the new URL.</li>
<li>End User can see on which page, url is redirected.</li>
<li>In Nutshell, <strong>Processing done at client side</strong>.</li>
</ul>
<p><strong>RequestDispatcher</strong></p>
<ul>
<li>This object can be accessed from <strong>HttpServletRequest</strong>.</li>
<li>Servlet will internally forward the request to another servlet or jsp page.</li>
<li>End user don&#8217;t know that which page is processed internally.</li>
<li>In Nutshell,<strong> Processing done at server side.</strong></li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/java/servlet/difference-in-sendredirect-and-requestdispatcher-in-servlet/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1501</post-id>	</item>
		<item>
		<title>Life Cycle of Servlet</title>
		<link>https://www.jitendrazaa.com/blog/java/servlet/life-cycle-of-servlet/</link>
					<comments>https://www.jitendrazaa.com/blog/java/servlet/life-cycle-of-servlet/#comments</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Sat, 12 Feb 2011 18:29:39 +0000</pubDate>
				<category><![CDATA[Servlet]]></category>
		<category><![CDATA[J2EE]]></category>
		<guid isPermaLink="false">http://JitendraZaa.com/blog/?p=1494</guid>

					<description><![CDATA[Explain Life Cycle of Servlet]]></description>
										<content:encoded><![CDATA[<p>Servlet is used in J2EE to create the dynamic web pages under Java Environment.  Like Applets, Servlet also have init() and destroy() methods. In this article i will explain the Life cycle of servlet.</p>
<p><strong>Step 1 </strong>: After Compilation of the Servlet, the class file is loaded by the loader.</p>
<p><strong>Step 2 </strong>: Then the Container instantiates the Servlet class by calling default Constructor. <a href="https://jitendrazaa.com/blog/java/servlet/how-container-handles-the-servlet-request/" target="_blank">Read this article to know more that how container works for Servlet</a>.</p>
<figure id="attachment_1496" aria-describedby="caption-attachment-1496" style="width: 452px" class="wp-caption aligncenter"><a href="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/02/Servlet-Life-Cycle.jpg?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" class="size-full wp-image-1496  " title="Servlet Life Cycle" src="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/02/Servlet-Life-Cycle.jpg?resize=452%2C305&#038;ssl=1" alt="Servlet Life Cycle" width="452" height="305" /></a><figcaption id="caption-attachment-1496" class="wp-caption-text">Servlet Life Cycle</figcaption></figure>
<p><strong><span id="more-1494"></span>Step 3</strong>: At Step 2, the Servlet object is just normal java object. <strong>It is the init() method which is just called after the instantiation of object and gives the Servletness to object. Like applets, this method is called only once in life cycle. </strong><a href="https://jitendrazaa.com/blog/java/example-to-override-the-init-method-of-the-servlet/" target="_blank">It is recommended not to override the init() method but if you want to override then read this article.</a></p>
<p><strong>Step 4 : </strong>Then <strong>Service()</strong> method is called and this is the place <strong>where servlet spends most of its life. </strong>Each request here comes as a separate thread.  This method then internally calls the doGet() or doPost() method depending upon the type of request comes.</p>
<p><strong>Step 5 :</strong> <strong>destroy()</strong> method is called just before destroying the Servlet. This method should be used if any clean code needs to be run before destroying the servlet.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/java/servlet/life-cycle-of-servlet/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1494</post-id>	</item>
		<item>
		<title>How container handles the Servlet request</title>
		<link>https://www.jitendrazaa.com/blog/java/servlet/how-container-handles-the-servlet-request/</link>
					<comments>https://www.jitendrazaa.com/blog/java/servlet/how-container-handles-the-servlet-request/#comments</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Sat, 12 Feb 2011 12:32:45 +0000</pubDate>
				<category><![CDATA[Servlet]]></category>
		<category><![CDATA[J2EE]]></category>
		<guid isPermaLink="false">http://JitendraZaa.com/blog/?p=1482</guid>

					<description><![CDATA[How container handles the Servlet request or How the apache Tomcat works]]></description>
										<content:encoded><![CDATA[<p>1. User clicks a link that has a URL of Servlet.</p>
<figure id="attachment_1485" aria-describedby="caption-attachment-1485" style="width: 495px" class="wp-caption aligncenter"><a href="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/02/Client-Browse-Servlet-URL.jpg?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" class="size-full wp-image-1485 " title="Client Browse Servlet URL" src="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/02/Client-Browse-Servlet-URL.jpg?resize=495%2C189&#038;ssl=1" alt="Client Browse Servlet URL" width="495" height="189" /></a><figcaption id="caption-attachment-1485" class="wp-caption-text">Client Browse Servlet URL</figcaption></figure>
<p><span id="more-1482"></span>2. Container (Apache Tomcat is one of the example) sees that the request is for servlet , so create two objects :<br />
<strong>HttpServletRequest<br />
HttpServletResponse</strong></p>
<figure id="attachment_1486" aria-describedby="caption-attachment-1486" style="width: 463px" class="wp-caption aligncenter"><a href="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/02/HttpServletRequest-and-HttpServletResponse.jpg?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" class="size-full wp-image-1486" title="HttpServletRequest and HttpServletResponse" src="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/02/HttpServletRequest-and-HttpServletResponse.jpg?resize=463%2C179&#038;ssl=1" alt="HttpServletRequest and HttpServletResponse" width="463" height="179" /></a><figcaption id="caption-attachment-1486" class="wp-caption-text">HttpServletRequest and HttpServletResponse</figcaption></figure>
<p>3. Container finds correct servlet on the basis of URL passed with the help <strong>deployment descriptor</strong> (web.xml) file. Creates / Allocate thread for that request and pass request and response object to servle thread.</p>
<figure id="attachment_1487" aria-describedby="caption-attachment-1487" style="width: 437px" class="wp-caption aligncenter"><a href="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/02/Create-Thread-for-Servlet.jpg?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" class="size-full wp-image-1487" title="Create Thread for Servlet" src="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/02/Create-Thread-for-Servlet.jpg?resize=437%2C180&#038;ssl=1" alt="Create Thread for Servlet" width="437" height="180" /></a><figcaption id="caption-attachment-1487" class="wp-caption-text">Create Thread for Servlet</figcaption></figure>
<p>4. Container calls the servlets <strong>service()</strong> method, on the type of request, service calls <strong>doGet() or doPost()</strong> methods.</p>
<figure id="attachment_1488" aria-describedby="caption-attachment-1488" style="width: 462px" class="wp-caption aligncenter"><a href="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/02/Service-method-of-servlet.jpg?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" class="size-full wp-image-1488" title="Service method of servlet" src="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/02/Service-method-of-servlet.jpg?resize=462%2C277&#038;ssl=1" alt="Service method of servlet" width="462" height="277" /></a><figcaption id="caption-attachment-1488" class="wp-caption-text">Service method of servlet</figcaption></figure>
<p>5. Lets assume that service calls the doPost() method. doPost() method<strong> generates dynamic page</strong> and add the page in response object.</p>
<figure id="attachment_1489" aria-describedby="caption-attachment-1489" style="width: 469px" class="wp-caption aligncenter"><a href="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/02/Servlet-doPost-response-object.jpg?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" class="size-full wp-image-1489" title="Servlet - doPost  - response object" src="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/02/Servlet-doPost-response-object.jpg?resize=469%2C223&#038;ssl=1" alt="Servlet - doPost  - response object" width="469" height="223" /></a><figcaption id="caption-attachment-1489" class="wp-caption-text">Servlet - doPost - response object</figcaption></figure>
<p>6. Thread completes, container <strong>converts the response object into HttpResponse</strong> object and <strong>destroys the response and request object</strong>.</p>
<figure id="attachment_1490" aria-describedby="caption-attachment-1490" style="width: 475px" class="wp-caption aligncenter"><a href="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/02/destroy-response-and-request-object.jpg?ssl=1"><img data-recalc-dims="1" loading="lazy" decoding="async" class="size-full wp-image-1490 " title="destroy response and request object" src="https://i0.wp.com/jitendrazaa.com/blog/wp-content/uploads/2011/02/destroy-response-and-request-object.jpg?resize=475%2C169&#038;ssl=1" alt="destroy response and request object" width="475" height="169" /></a><figcaption id="caption-attachment-1490" class="wp-caption-text">destroy response and request object</figcaption></figure>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/java/servlet/how-container-handles-the-servlet-request/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1482</post-id>	</item>
		<item>
		<title>Example to Override the init() method of the servlet</title>
		<link>https://www.jitendrazaa.com/blog/java/servlet/example-to-override-the-init-method-of-the-servlet/</link>
					<comments>https://www.jitendrazaa.com/blog/java/servlet/example-to-override-the-init-method-of-the-servlet/#comments</comments>
		
		<dc:creator><![CDATA[Jitendra]]></dc:creator>
		<pubDate>Thu, 10 Feb 2011 20:28:00 +0000</pubDate>
				<category><![CDATA[Servlet]]></category>
		<category><![CDATA[J2EE]]></category>
		<guid isPermaLink="false">http://JitendraZaa.com/blog/?p=1479</guid>

					<description><![CDATA[Example to Override the init() method of the servlet]]></description>
										<content:encoded><![CDATA[<p>It is not recommended to override the init() method of the servlet.</p>
<p>Here is an example to override init() method of servlet:</p>
<pre class="brush: java; title: ; notranslate">
    public class BookDBServlet ... {

        private BookstoreDB books;

        public void init(ServletConfig config) throws ServletException {

            // Store the ServletConfig object and log the initialization
            super.init(config);

            // Load the database to prepare for requests
            books = new BookstoreDB();
        }
        ...
    }
</pre>
<p>you must write  <strong>super.init(config) </strong>code and after that program specific logic.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.jitendrazaa.com/blog/java/servlet/example-to-override-the-init-method-of-the-servlet/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1479</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Minified using Disk

Served from: www.jitendrazaa.com @ 2026-05-09 19:39:54 by W3 Total Cache
-->