<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;CU4BQ3Y6cSp7ImA9WhBQEUo.&quot;"><id>tag:blogger.com,1999:blog-9029038390867514964</id><updated>2013-03-13T04:25:52.819-07:00</updated><category term="apache" /><category term="linux" /><category term="redhat" /><category term="hibernate" /><category term="java" /><category term="cluster" /><category term="processor" /><category term="DWR" /><category term="spring security" /><category term="spring mvc" /><category term="tomcat" /><category term="hibernate spring mvc DWR security" /><category term="Terracotta" /><category term="mod_jk" /><category term="cpu" /><title>Java Obsession</title><subtitle type="html" /><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://javadrama.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://javadrama.blogspot.com/" /><author><name>moutaz salem</name><uri>https://plus.google.com/106063653009079870460</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-VvhNV9YhJS8/AAAAAAAAAAI/AAAAAAAAAPI/hBEdI1Nhcis/s512-c/photo.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>8</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/JavaObsession" /><feedburner:info uri="javaobsession" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;DkYDQXo9fip7ImA9WhNTFE0.&quot;"><id>tag:blogger.com,1999:blog-9029038390867514964.post-8485656759618379015</id><published>2012-10-16T09:15:00.000-07:00</published><updated>2012-10-16T09:16:10.466-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-10-16T09:16:10.466-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="mod_jk" /><category scheme="http://www.blogger.com/atom/ns#" term="cluster" /><category scheme="http://www.blogger.com/atom/ns#" term="Terracotta" /><category scheme="http://www.blogger.com/atom/ns#" term="apache" /><category scheme="http://www.blogger.com/atom/ns#" term="tomcat" /><title>Terracotta and Tomcat Clustering - Page 3</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Ok, our tutorial will end by adding Apache web server to load balance the traffic between both Tomcats.&lt;br /&gt;
&lt;br /&gt;
&lt;h4 style="text-align: left;"&gt;
install Apache Web Server&amp;nbsp;&lt;/h4&gt;
&lt;div&gt;
just run the msi installer or install it from an RPM or from the OS repositories, i will refer to the installation directory as ${APACHE_HOME}.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
point your browser to &lt;b&gt;localhost &lt;/b&gt;and make sure you are welcomed with the most famous message&lt;/div&gt;
&lt;h2 style="text-align: left;"&gt;
&lt;span style="font-size: x-large;"&gt;It Works!&lt;/span&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;span style="font-size: x-large;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;h4 style="text-align: left;"&gt;
install mod_jk&lt;/h4&gt;
&lt;div&gt;
extract the file&amp;nbsp;&lt;b&gt;tomcat-connectors-x.x.xx-windows-arch-httpd-2.2.x.zip&lt;/b&gt;, copy mod_jk.so to ${APACHE_HOME}\modules&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style="text-align: left;"&gt;
Configure Apache Web Server&lt;/h3&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h4 style="text-align: left;"&gt;
httpd.conf&lt;/h4&gt;
&lt;div&gt;
edit the file ${APACHE_HOME}\conf\httpd.conf to add the following settings:
&lt;br /&gt;
&lt;pre class="brush: html"&gt;LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkShmFile logs/mod_jk.shm
JkLogFile logs/mod_jk.log
JkLogLevel info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkMount /examples loadbalancer
JkMount /examples/* loadbalancer
JkMount /status status
JkMount /status/* status&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
here we are loading the mod_jk module, the mod_jk will load cluster members (Tomcats) from a file named workers.properties under&amp;nbsp;${APACHE_HOME}\conf directory.&lt;br /&gt;
also, any requests coming to apache at /examples will be forwarded to one of our Tomcats.&lt;br /&gt;
while /status will direct us to the mod_jk control panel.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h4 style="text-align: left;"&gt;
workers.properties&lt;/h4&gt;
&lt;div&gt;
create a new file under the directory&amp;nbsp;${APACHE_HOME}\conf named &lt;b&gt;workers.properties&lt;/b&gt;&amp;nbsp;with the following content:
&lt;br /&gt;
&lt;pre class="brush: html"&gt;# Define list of workers that will be used
worker.list=loadbalancer,status
# Define tomcat1
worker.tomcat1.port=8009
worker.tomcat1.host=127.0.0.1
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
# Define tomcat2
worker.tomcat2.port=8010
worker.tomcat2.host=127.0.0.1
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1
# Load-balancing behaviour
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=tomcat1,tomcat2
worker.loadbalancer.sticky_session=0
# Status worker for managing load balancer
worker.status.type=status
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
note that the worker names must match the jvmRoute configurations on the Tomcats.
also, you can enable sticky sessions or disable to fit your need, however, i have turned it off here.
&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;
restart apache web server, if everything went as it should, navigating to http://localhost/examples would result in this page.

&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-s4cT0IFw5eA/UH2GLqhY92I/AAAAAAAAAOM/_LJPy9fkyJg/s1600/examples.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-s4cT0IFw5eA/UH2GLqhY92I/AAAAAAAAAOM/_LJPy9fkyJg/s1600/examples.JPG" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
if you have&amp;nbsp;debugging installed on your browser, like firebug, you can check your JSESSIONID value, you will find it&amp;nbsp;post fixed with the jvmRoute value of the tomcat server who served the response.&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
if you hit the refresh button you can see that you are moving for every hit from one Tomcat to the other.&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h4 style="text-align: left;"&gt;
Testing Sessions Failover&lt;/h4&gt;
&lt;div style="text-align: left;"&gt;
point your browser to&amp;nbsp;http://localhost/examples/servlets/servlet/SessionExample add a couple of values in the session.&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
now it is up to you, bring any Tomcat down (not both, of course) or any Terracotta (not both, of course), play around, your minimal setup is one Tomcat and &amp;nbsp;one Terracotta server.&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
your values in the session should never disappear!!!&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
Have Fun.&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/JavaObsession/~4/_wiETcnMR2Y" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javadrama.blogspot.com/feeds/8485656759618379015/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=9029038390867514964&amp;postID=8485656759618379015" title="6 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/9029038390867514964/posts/default/8485656759618379015?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/9029038390867514964/posts/default/8485656759618379015?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/JavaObsession/~3/_wiETcnMR2Y/terracotta-and-tomcat-clustering-page-3.html" title="Terracotta and Tomcat Clustering - Page 3" /><author><name>moutaz salem</name><uri>https://plus.google.com/106063653009079870460</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-VvhNV9YhJS8/AAAAAAAAAAI/AAAAAAAAAPI/hBEdI1Nhcis/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-s4cT0IFw5eA/UH2GLqhY92I/AAAAAAAAAOM/_LJPy9fkyJg/s72-c/examples.JPG" height="72" width="72" /><thr:total>6</thr:total><feedburner:origLink>http://javadrama.blogspot.com/2012/10/terracotta-and-tomcat-clustering-page-3.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkUER3gyfCp7ImA9WhNTFE0.&quot;"><id>tag:blogger.com,1999:blog-9029038390867514964.post-2106223455570433093</id><published>2012-10-16T07:57:00.003-07:00</published><updated>2012-10-16T09:16:46.694-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-10-16T09:16:46.694-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="mod_jk" /><category scheme="http://www.blogger.com/atom/ns#" term="cluster" /><category scheme="http://www.blogger.com/atom/ns#" term="Terracotta" /><category scheme="http://www.blogger.com/atom/ns#" term="apache" /><category scheme="http://www.blogger.com/atom/ns#" term="tomcat" /><title>Terracotta and Tomcat Clustering - Page 2</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Now, download&amp;nbsp;apache-tomcat-6.0.35 and extract it twice to two directories of your choice, i will refer to them as ${CATALINA_HOME_NODE1} and&amp;nbsp;${CATALINA_HOME_NODE2}.&lt;br /&gt;
&lt;br /&gt;
&lt;h4 style="text-align: left;"&gt;
Copy Terracotta Libraries&lt;/h4&gt;
&lt;div&gt;
we need two jar files to be copied to &lt;b&gt;both &lt;/b&gt;Tomcats &lt;b&gt;lib &lt;/b&gt;directory.&lt;/div&gt;
&lt;div&gt;
${TC_HOME}\common\terracotta-toolkit-1.6-runtime-5.0.0.jar&lt;/div&gt;
&lt;div&gt;
${TC_HOME}\sessions\terracotta-session-1.3.0.jar&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h4 style="text-align: left;"&gt;
Copy Terracotta Config File&lt;/h4&gt;
&lt;div&gt;
we need to copy the same Terracotta configuration file to the &lt;b&gt;conf&lt;/b&gt; directory of &lt;b&gt;both&lt;/b&gt; Tomcats&lt;/div&gt;
&lt;div&gt;
${TC_HOME}\config\tc-config.xml&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
one small change to do, the client log location, both clients (Tomcat servers) can't be configured with the same client log location.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
edit&amp;nbsp;${CATALINA_HOME_NODE2}\conf\tc-config.xml&lt;/div&gt;
&lt;div&gt;
change log location under the clients tag
&lt;br /&gt;
&lt;pre class="brush: xml"&gt;&amp;lt;logs&amp;gt;%(user.home)/terracotta/client-logs&amp;lt;/logs&amp;gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
to be
&lt;br /&gt;
&lt;pre class="brush: xml"&gt;&amp;lt;logs&amp;gt;%(user.home)/terracotta/client-logs2&amp;lt;/logs&amp;gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h4 style="text-align: left;"&gt;
Terracotta Valve&lt;/h4&gt;
&lt;/div&gt;
&lt;div&gt;
Tomcat connects to Terracotta by intercepting requests via a value, now let's hook that valve.&lt;/div&gt;
&lt;div&gt;
under the &lt;b&gt;conf&lt;/b&gt; directory of both Tomcats, edit the file context.xml to look like this:
&lt;br /&gt;
&lt;pre class="brush: xml"&gt;&amp;lt;Context&amp;gt;

 &amp;lt;!-- Default set of monitored resources --&amp;gt;
 &amp;lt;WatchedResource&amp;gt;WEB-INF/web.xml&amp;lt;/WatchedResource&amp;gt;

 &amp;lt;!-- Uncomment this to disable session persistence across Tomcat restarts --&amp;gt;
    &amp;lt;Manager pathname="" /&amp;gt;
 
 &amp;lt;!-- Hook the Terracotta Valve and configure it using the tc-config.xml file --&amp;gt;
 &amp;lt;Valve className="org.terracotta.session.TerracottaTomcat60xSessionValve" tcConfigUrl="../conf/tc-config.xml"/&amp;gt;

&amp;lt;/Context&amp;gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
note that we have&amp;nbsp;disabled session persistence across Tomcat restarts because that is a task for Terracotta now.&lt;br /&gt;
&lt;br /&gt;
&lt;h3 style="text-align: left;"&gt;
Tomcat HA Configurations&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h4 style="text-align: left;"&gt;
&lt;/h4&gt;
&lt;h4 style="text-align: left;"&gt;
jvmRoute&lt;/h4&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
we need to define a unique jvmRoute for each Tomcat server.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
edit ${CATALINA_HOME_NODE1}\conf\server.xml, change the &lt;b&gt;Engine &lt;/b&gt;tag to look like this:
&lt;br /&gt;
&lt;pre class="brush: xml"&gt;&amp;lt;Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1"&amp;gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
edit&amp;nbsp;${CATALINA_HOME_NODE2}\conf\server.xml, change the&amp;nbsp;&lt;b&gt;Engine&amp;nbsp;&lt;/b&gt;tag to look like this:
&lt;br /&gt;
&lt;pre class="brush: xml"&gt;&amp;lt;Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2"&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;h4 style="text-align: left;"&gt;
Make sure you don't have port conflicts&lt;/h4&gt;
as i am running both Tomcats on the same box, i had to change the ports for the second Tomcat.&lt;br /&gt;
in&amp;nbsp;${CATALINA_HOME_NODE2}\conf\server.xml, i have changed&lt;br /&gt;
shutdown port: 8005 to 8006&lt;br /&gt;
http port: 8080 to 8081&lt;br /&gt;
ajp port: 8009 to 8010&lt;br /&gt;
&lt;br /&gt;
&lt;h4 style="text-align: left;"&gt;
Run both Tomcats&lt;/h4&gt;
&lt;/div&gt;
&lt;div&gt;
open two more terminals and navigate to&amp;nbsp;${CATALINA_HOME_NODE1}\bin on one and&amp;nbsp;${CATALINA_HOME_NODE2}\bin on the other, on both terminals execute:
&lt;br /&gt;
&lt;pre class="brush: html"&gt;startup.bat
&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
if everything went as it should, you should see something like that on both Tomcat terminals
&lt;br /&gt;
&lt;pre class="brush: html"&gt;Terracotta 3.7.0, as of 20120717-133013 (Revision unknown-20453 by cruise@rh5vmo113.terracotta.lan from 3.7.0)
Successfully loaded base configuration from file at 'H:\terracotta\apache-tomcat-6.0.35\bin\.\..\conf\tc-config.xml'.
Successfully loaded base configuration from file at 'H:\terracotta\apache-tomcat-6.0.35\temp\tc-config8509448930662041153.xml'.
Log file: 'C:\terracotta\client-logs\terracotta-client.log'.
Connection successfully established to server at 127.0.0.1:9510
&lt;/pre&gt;
&lt;br /&gt;
&lt;h4 style="text-align: left;"&gt;
Test it&lt;/h4&gt;
Tomcat comes by default with a web application called &lt;b&gt;examples&lt;/b&gt;,&amp;nbsp;we will use it till the end of this tutorial to test our setup&lt;br /&gt;
&lt;br /&gt;
point your browser to&amp;nbsp;http://localhost:8080/examples/&amp;nbsp;and&amp;nbsp;http://localhost:8081/examples/ and make sure both servers are responding.&lt;br /&gt;
&lt;br /&gt;
open your Terracotta Development Console and make sure that both clients (the tomcat server) are present as below.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-fND5b69bODE/UH11dSKOO_I/AAAAAAAAAN8/F6WUD-Mu85g/s1600/ter.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="165" src="http://3.bp.blogspot.com/-fND5b69bODE/UH11dSKOO_I/AAAAAAAAAN8/F6WUD-Mu85g/s320/ter.JPG" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
also note that the sessions button is now active.&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
&lt;a href="http://javadrama.blogspot.com/2012/10/terracotta-and-tomcat-clustering-page-3.html"&gt;Next - Load balancing with Apache Web Server&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/JavaObsession/~4/xLmQKL1gdPw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javadrama.blogspot.com/feeds/2106223455570433093/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=9029038390867514964&amp;postID=2106223455570433093" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/9029038390867514964/posts/default/2106223455570433093?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/9029038390867514964/posts/default/2106223455570433093?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/JavaObsession/~3/xLmQKL1gdPw/terracotta-and-tomcat-clustering-page-2.html" title="Terracotta and Tomcat Clustering - Page 2" /><author><name>moutaz salem</name><uri>https://plus.google.com/106063653009079870460</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-VvhNV9YhJS8/AAAAAAAAAAI/AAAAAAAAAPI/hBEdI1Nhcis/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-fND5b69bODE/UH11dSKOO_I/AAAAAAAAAN8/F6WUD-Mu85g/s72-c/ter.JPG" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://javadrama.blogspot.com/2012/10/terracotta-and-tomcat-clustering-page-2.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Ck8HSX05eSp7ImA9WhNTGE8.&quot;"><id>tag:blogger.com,1999:blog-9029038390867514964.post-5607013592985415205</id><published>2012-10-16T05:27:00.000-07:00</published><updated>2012-10-21T05:00:38.321-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-10-21T05:00:38.321-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="mod_jk" /><category scheme="http://www.blogger.com/atom/ns#" term="cluster" /><category scheme="http://www.blogger.com/atom/ns#" term="Terracotta" /><category scheme="http://www.blogger.com/atom/ns#" term="apache" /><category scheme="http://www.blogger.com/atom/ns#" term="tomcat" /><title>Terracotta and Tomcat Clustering - Page 1</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;div style="text-align: left;" trbidi="on"&gt;
&lt;div style="text-align: left;"&gt;
In this tutorial we will learn a lot of stuff :)&lt;/div&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;How to install Terracotta in an Active/Passive HA (High Availability) model.&lt;/li&gt;
&lt;li&gt;How to configure Tomcat 6.0 to store/retrieve sessions on Terracotta.&lt;/li&gt;
&lt;li&gt;How to install and configure Apache Web Server configure it to relay requests to Tomcat.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 style="text-align: left;"&gt;
Why?&lt;/h3&gt;
&lt;div&gt;
i can do session replication using Tomcat built-in clustering module, so why do i use Terracotta?&lt;/div&gt;
&lt;div&gt;
well, the Tomcat clustering works just perfect with major four&amp;nbsp;concerns:&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;Clients' sessions data are part of the Tomcat JVM instance.&lt;/li&gt;
&lt;li&gt;There is extra work for Tomcat to make sure that the sessions and its data are replicated among the cluster&lt;/li&gt;
&lt;li&gt;not suitable for larger cluster because of using&amp;nbsp;multicasting, imagine the network traffic generated by eight tomcat nodes replicating their sessions.&lt;/li&gt;
&lt;li&gt;You can't store an object which is not serializable.&lt;/li&gt;
&lt;/ol&gt;
So, moving this responsibility to Terracotta would help&amp;nbsp;eliminating those four concerns.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style="text-align: left;"&gt;
Our Final&amp;nbsp;architecture&amp;nbsp;&lt;/h3&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-d2FZmV3CGS4/UH1h4v4si6I/AAAAAAAAANs/EuHgy0p9GSw/s1600/Terracotta.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="304" src="http://1.bp.blogspot.com/-d2FZmV3CGS4/UH1h4v4si6I/AAAAAAAAANs/EuHgy0p9GSw/s320/Terracotta.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
it is really simple:&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;
&lt;/div&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;Both Tomcats store/retrieve sessions on the active Terracotta server.&lt;/li&gt;
&lt;li&gt;Apache web server forwards requests to both Tomcats.&lt;/li&gt;
&lt;li&gt;If the active Terracotta fails the passive Terracotta will be the active.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 style="text-align: left;"&gt;
Let's Digg in&lt;/h3&gt;
&lt;div&gt;
&lt;ol style="text-align: left;"&gt;
&lt;/ol&gt;
&lt;div style="text-align: left;"&gt;
First things first! we need:&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;&lt;a href="http://terracotta.org/downloads/open-source/destination?name=terracotta-3.7.0-installer.jar&amp;amp;bucket=tcdistributions&amp;amp;file=terracotta-3.7.0-installer.jar"&gt;terracotta-3.7.0-installer.jar&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://tomcat.apache.org/download-60.cgi"&gt;apache-tomcat-6.0.35&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://tomcat.apache.org/download-connectors.cgi"&gt;mod_jk.so&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://httpd.apache.org/download.cgi#apache22"&gt;httpd-2.2.22&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.oracle.com/technetwork/java/javase/downloads/jdk6u35-downloads-1836443.html"&gt;JDK 1.6&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
My five servers built on a single windows 7 box as a POC, you should have no troubles with running the same example on Linux, only minor chages are needed.&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;h4 style="text-align: left;"&gt;
Install and Configure Terracotta&lt;/h4&gt;
&lt;div&gt;
open a terminal and change the directory to the place where you downloaded terracotta-3.7.0-installer.jar&lt;/div&gt;
&lt;div&gt;
&lt;pre class="brush: html"&gt;java -jar terracotta-3.7.0-installer.jar
&lt;/pre&gt;
&lt;/div&gt;
you will see a setup wizard, it is straight forward, install Terracotta to the directory of your choice, from now on i will refer to that directory as ${TC_HOME}&lt;br /&gt;
now, go to ${TC_HOME} and create a new directory called 'config', this directory will contain our Terracotta configuration file that will set up the Terracotta cluster.&lt;br /&gt;
create a new file inside the &lt;b&gt;config&lt;/b&gt; directory called &lt;b&gt;tc-config.xml &lt;/b&gt;with the following content:&lt;/div&gt;
&lt;/div&gt;
&lt;pre class="brush: xml" style="text-align: left;"&gt;&amp;lt;tc-config xmlns="http://www.terracotta.org/config"&amp;gt;
 &amp;lt;servers secure="false" xmlns:tc="http://www.terracotta.org/config" xmlns:con="http://www.terracotta.org/config" xmlns=""&amp;gt;
  &amp;lt;server bind="0.0.0.0" host="127.0.0.1" name="node1"&amp;gt;
   &amp;lt;data&amp;gt;C:\terracotta\server-data&amp;lt;/data&amp;gt;
   &amp;lt;logs&amp;gt;C:\terracotta\server-logs&amp;lt;/logs&amp;gt;
   &amp;lt;statistics&amp;gt;C:\terracotta\server-statistics&amp;lt;/statistics&amp;gt;
   &amp;lt;dso-port bind="0.0.0.0"&amp;gt;9510&amp;lt;/dso-port&amp;gt;
   &amp;lt;jmx-port bind="0.0.0.0"&amp;gt;9520&amp;lt;/jmx-port&amp;gt;
   &amp;lt;l2-group-port bind="0.0.0.0"&amp;gt;9530&amp;lt;/l2-group-port&amp;gt;
   &amp;lt;data-backup&amp;gt;C:\terracotta\data-backup&amp;lt;/data-backup&amp;gt;
   &amp;lt;index&amp;gt;C:\terracotta\server-data\index&amp;lt;/index&amp;gt;
   &amp;lt;dso&amp;gt;
    &amp;lt;client-reconnect-window&amp;gt;120&amp;lt;/client-reconnect-window&amp;gt;
    &amp;lt;persistence&amp;gt;
     &amp;lt;mode&amp;gt;temporary-swap-only&amp;lt;/mode&amp;gt;
    &amp;lt;/persistence&amp;gt;
    &amp;lt;garbage-collection&amp;gt;
     &amp;lt;enabled&amp;gt;true&amp;lt;/enabled&amp;gt;
     &amp;lt;verbose&amp;gt;false&amp;lt;/verbose&amp;gt;
     &amp;lt;interval&amp;gt;3600&amp;lt;/interval&amp;gt;
    &amp;lt;/garbage-collection&amp;gt;
   &amp;lt;/dso&amp;gt;
  &amp;lt;/server&amp;gt;
  &amp;lt;server bind="0.0.0.0" host="127.0.0.1" name="node2"&amp;gt;
   &amp;lt;data&amp;gt;C:\terracotta\server-data2&amp;lt;/data&amp;gt;
   &amp;lt;logs&amp;gt;C:\terracotta\server-logs2&amp;lt;/logs&amp;gt;
   &amp;lt;statistics&amp;gt;C:\terracotta\server-statistics2&amp;lt;/statistics&amp;gt;
   &amp;lt;dso-port bind="0.0.0.0"&amp;gt;9511&amp;lt;/dso-port&amp;gt;
   &amp;lt;jmx-port bind="0.0.0.0"&amp;gt;9521&amp;lt;/jmx-port&amp;gt;
   &amp;lt;l2-group-port bind="0.0.0.0"&amp;gt;9531&amp;lt;/l2-group-port&amp;gt;
   &amp;lt;data-backup&amp;gt;C:\terracotta\data-backup2&amp;lt;/data-backup&amp;gt;
   &amp;lt;index&amp;gt;C:\terracotta\server-data\index2&amp;lt;/index&amp;gt;
   &amp;lt;dso&amp;gt;
    &amp;lt;client-reconnect-window&amp;gt;120&amp;lt;/client-reconnect-window&amp;gt;
    &amp;lt;persistence&amp;gt;
     &amp;lt;mode&amp;gt;temporary-swap-only&amp;lt;/mode&amp;gt;
    &amp;lt;/persistence&amp;gt;
    &amp;lt;garbage-collection&amp;gt;
     &amp;lt;enabled&amp;gt;true&amp;lt;/enabled&amp;gt;
     &amp;lt;verbose&amp;gt;false&amp;lt;/verbose&amp;gt;
     &amp;lt;interval&amp;gt;3600&amp;lt;/interval&amp;gt;
    &amp;lt;/garbage-collection&amp;gt;
   &amp;lt;/dso&amp;gt;
  &amp;lt;/server&amp;gt;
  &amp;lt;mirror-groups&amp;gt;
   &amp;lt;mirror-group&amp;gt;
    &amp;lt;members&amp;gt;
     &amp;lt;member&amp;gt;node1&amp;lt;/member&amp;gt;
     &amp;lt;member&amp;gt;node2&amp;lt;/member&amp;gt;
    &amp;lt;/members&amp;gt;
   &amp;lt;/mirror-group&amp;gt;
  &amp;lt;/mirror-groups&amp;gt;
  &amp;lt;ha&amp;gt;
   &amp;lt;mode&amp;gt;networked-active-passive&amp;lt;/mode&amp;gt;
   &amp;lt;networked-active-passive&amp;gt;
    &amp;lt;election-time&amp;gt;5&amp;lt;/election-time&amp;gt;
   &amp;lt;/networked-active-passive&amp;gt;
  &amp;lt;/ha&amp;gt;
  &amp;lt;update-check&amp;gt;
   &amp;lt;enabled&amp;gt;true&amp;lt;/enabled&amp;gt;
   &amp;lt;period-days&amp;gt;7&amp;lt;/period-days&amp;gt;
  &amp;lt;/update-check&amp;gt;
 &amp;lt;/servers&amp;gt;
 &amp;lt;system xmlns:tc="http://www.terracotta.org/config" xmlns:con="http://www.terracotta.org/config" xmlns=""&amp;gt;
  &amp;lt;configuration-model&amp;gt;production&amp;lt;/configuration-model&amp;gt;
 &amp;lt;/system&amp;gt;

 &amp;lt;clients xmlns:tc="http://www.terracotta.org/config" xmlns:con="http://www.terracotta.org/config" xmlns=""&amp;gt;
  &amp;lt;logs&amp;gt;%(user.home)/terracotta/client-logs&amp;lt;/logs&amp;gt;
  &amp;lt;modules&amp;gt;
   &amp;lt;module name="terracotta-toolkit-1.6" group-id="org.terracotta.toolkit"/&amp;gt;
  &amp;lt;/modules&amp;gt;
 &amp;lt;/clients&amp;gt;
&amp;lt;/tc-config&amp;gt;&lt;/pre&gt;
&lt;div style="text-align: left;"&gt;
the important thing to note here is the servers node&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;/div&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;for each Terracotta server that will run in the cluster you need to define a server node.&lt;/li&gt;
&lt;li&gt;the server tag has three attributes:&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;bind: the default bind address which Terracotta listen to.&lt;/li&gt;
&lt;li&gt;host: the IP address that will be used to connect to the Terracotta server.&lt;/li&gt;
&lt;li&gt;name: the name of this node&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;data: is where this server stores its data (make it unique for each server if you are running the cluster on one box)&lt;/li&gt;
&lt;li&gt;logs: is where the server store its logs&amp;nbsp;(make it unique for each server if you are running the cluster on one box)&lt;/li&gt;
&lt;li&gt;statistics:&amp;nbsp;&amp;nbsp;is where the server store its statistics&amp;nbsp;(make it unique for each server if you are running the cluster on one box)&lt;/li&gt;
&lt;li&gt;port configurations: JMX, DSO and group ports&amp;nbsp;(make it unique for each server if you are running the cluster on one box)&lt;/li&gt;
&lt;li&gt;data-backup:&amp;nbsp;is where the server store its data backups&amp;nbsp;&amp;nbsp;(make it unique for each server if you are running the cluster on one box)&lt;/li&gt;
&lt;li&gt;index: is&amp;nbsp;where the server store its index files&amp;nbsp;&amp;nbsp;(make it unique for each server if you are running the cluster on one box)&lt;/li&gt;
&lt;li&gt;dso: is the Distributed Shared Objects specific options.&lt;/li&gt;
&lt;/ol&gt;
we have now defined two servers, we need to&amp;nbsp;define how they would work.&lt;br /&gt;
&lt;div&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;mirror-group: a tag to define Terracotta groups&amp;nbsp;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;members: a tag to add a server to a group using its name&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;ha: is a tag to define the High&amp;nbsp;Availability options of the group&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;mode: networked-active-passive it means that the&amp;nbsp;communications between the servers will relay on networking.&lt;/li&gt;
&lt;li&gt;election-time: the Terracotta server would wait for that time to decide if it should start as an Active or passive.&lt;/li&gt;
&lt;/ul&gt;
&lt;/ol&gt;
let's start out cluster an make sure everything is OK.&lt;br /&gt;
&lt;div style="text-align: left;"&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;open three consoles and navigate to ${TC_HOME}\bin on the three of them&lt;/li&gt;
&lt;li&gt;Start node1
&lt;pre class="brush: html"&gt;start-tc-server.bat -f ..\config\tc-config.xml -n node1
&lt;/pre&gt;
you should see
&lt;pre class="brush: html"&gt;Becoming State[ ACTIVE-COORDINATOR ]
Terracotta Server instance has started up as ACTIVE node on 0.0.0.0:9510 successfully, and is now ready for work.
&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Start node2
&lt;pre class="brush: html"&gt;start-tc-server.bat -f ..\config\tc-config.xml -n node2
&lt;/pre&gt;
you should see
&lt;pre class="brush: html"&gt;NodeID[127.0.0.1:9510] joined the cluster
Moved to State[ PASSIVE-UNINITIALIZED ]
Moved to State[ PASSIVE-STANDBY ]
&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;on the third console start the Terracotta Development Console
&lt;pre class="brush: html"&gt;dev-console.bat
&lt;/pre&gt;
connect to either 127.0.0.1:9520 or 127.0.0.1:9521, you should see this screen
&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;ol style="text-align: left;"&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-QSpd1bbGuXE/UH1Qu_gHHfI/AAAAAAAAANc/N9RTpKagaOg/s1600/tera.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="165" src="http://4.bp.blogspot.com/-QSpd1bbGuXE/UH1Qu_gHHfI/AAAAAAAAANc/N9RTpKagaOg/s320/tera.jpg" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
as per the screen shot, node1 is active and node2 is passive, play around by taking node1 down and see if node2 becomes the active and then Vice Versa.&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;a href="http://javadrama.blogspot.com/2012/10/terracotta-and-tomcat-clustering-page-2.html"&gt;Next - Configure Tomcat to use Terracotta&lt;/a&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/JavaObsession/~4/EcdxIv3x1LE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javadrama.blogspot.com/feeds/5607013592985415205/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=9029038390867514964&amp;postID=5607013592985415205" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/9029038390867514964/posts/default/5607013592985415205?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/9029038390867514964/posts/default/5607013592985415205?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/JavaObsession/~3/EcdxIv3x1LE/terracotta-and-tomcat-clustering-page-1.html" title="Terracotta and Tomcat Clustering - Page 1" /><author><name>moutaz salem</name><uri>https://plus.google.com/106063653009079870460</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-VvhNV9YhJS8/AAAAAAAAAAI/AAAAAAAAAPI/hBEdI1Nhcis/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-d2FZmV3CGS4/UH1h4v4si6I/AAAAAAAAANs/EuHgy0p9GSw/s72-c/Terracotta.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://javadrama.blogspot.com/2012/10/terracotta-and-tomcat-clustering-page-1.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DE4DSH48fyp7ImA9WhJaF00.&quot;"><id>tag:blogger.com,1999:blog-9029038390867514964.post-4727797414121831261</id><published>2012-10-08T07:15:00.001-07:00</published><updated>2012-10-08T07:36:19.077-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-10-08T07:36:19.077-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="hibernate" /><category scheme="http://www.blogger.com/atom/ns#" term="DWR" /><category scheme="http://www.blogger.com/atom/ns#" term="spring mvc" /><category scheme="http://www.blogger.com/atom/ns#" term="hibernate spring mvc DWR security" /><category scheme="http://www.blogger.com/atom/ns#" term="spring security" /><title>Spring MVC, Security + Hibernate + DWR - Page 4</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;strike&gt;&lt;/strike&gt;&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;br /&gt;
&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;h2 style="text-align: left;"&gt;
&lt;span style="font-size: x-large;"&gt;
Spring MVC, Security + Hibernate + DWR, Let's PLay&lt;/span&gt;&lt;/h2&gt;
&lt;div&gt;
OK, let's secure it ;)&lt;/div&gt;
&lt;br/&gt;



&lt;h3 style="text-align: left;"&gt;
The Spring security filter and Spring configuration:
&lt;/h3&gt;
in you web.xml edit the line to add a new file called spring-security.xml
&lt;pre class="brush: xml"&gt;
&amp;lt;param-value&gt;/WEB-INF/applicationContext.xml,/WEB-INF/dwr-context.xml,/WEB-INF/hibernate-context.xml,/WEB-INF/spring-security.xml&amp;lt;/param-value&gt;
&lt;/pre&gt;

&lt;pre class="brush: xml"&gt;
&amp;lt;filter&gt;
    &amp;lt;filter-name&gt;springSecurityFilterChain&amp;lt;/filter-name&gt;
    &amp;lt;filter-class&gt;
            org.springframework.web.filter.DelegatingFilterProxy
    &amp;lt;/filter-class&gt;
&amp;lt;/filter&gt;
&amp;lt;filter-mapping&gt;
	&amp;lt;filter-name&gt;springSecurityFilterChain&amp;lt;/filter-name&gt;
    &amp;lt;url-pattern&gt;/*&amp;lt;/url-pattern&gt;
&amp;lt;/filter-mapping&gt;
&lt;/pre&gt;

&lt;h3 style="text-align: left;"&gt;
Filter Order
&lt;/h3&gt;
as we are now having two filters (springSecurityFilterChain and OpenSessionInViewFilter) we need to set the filter order as, 1-security 2-Hibernate.&lt;br/&gt;
make sure you filter order look like this
&lt;pre class="brush: xml"&gt;
&amp;lt;filter-mapping&gt;
	&amp;lt;filter-name&gt;springSecurityFilterChain&amp;lt;/filter-name&gt;
	&amp;lt;url-pattern&gt;/*&amp;lt;/url-pattern&gt;
&amp;lt;/filter-mapping&gt;
&amp;lt;filter-mapping&gt;
	&amp;lt;filter-name&gt;hibernateFilter&amp;lt;/filter-name&gt;
	&amp;lt;url-pattern&gt;/*&amp;lt;/url-pattern&gt;
&amp;lt;/filter-mapping&gt;
&lt;/pre&gt;

&lt;h3 style="text-align: left;"&gt;
spring-security.xml
&lt;/h3&gt;
this file will configure spring security, we will use form authentication, the users will be configured using this file too.
&lt;pre class="brush: xml"&gt;
&amp;lt;beans:beans xmlns="http://www.springframework.org/schema/security"
	xmlns:beans="http://www.springframework.org/schema/beans" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/security
	http://www.springframework.org/schema/security/spring-security-3.0.3.xsd"&gt;
 
	&amp;lt;http auto-config="true"&gt;
		&amp;lt;intercept-url pattern="/*" access="ROLE_USER" /&gt;
	&amp;lt;/http&gt;
 
	&amp;lt;authentication-manager&gt;
	  &amp;lt;authentication-provider&gt;
	    &amp;lt;user-service&gt;
		&amp;lt;user name="mtz" password="123" authorities="ROLE_USER" /&gt;
	    &amp;lt;/user-service&gt;
	  &amp;lt;/authentication-provider&gt;
	&amp;lt;/authentication-manager&gt;
 
&amp;lt;/beans:beans&gt;
&lt;/pre&gt;

the idea here is that we are securing every URL, with:&lt;br/&gt;
username:mtz&lt;br/&gt;
password:123&lt;br/&gt;

&lt;h4 style="text-align: left;"&gt;
Test it
&lt;/h4&gt;
deploy the project and you should see the following page

&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-6uBhfaHO8oo/UHLjptBRLdI/AAAAAAAAAM8/dShDCQbb7lQ/s1600/springMVC3.PNG" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="173" style="border:2px; border-style:solid" width="316" src="http://4.bp.blogspot.com/-6uBhfaHO8oo/UHLjptBRLdI/AAAAAAAAAM8/dShDCQbb7lQ/s400/springMVC3.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;

and you are done, thanks :)&lt;br/&gt;

&lt;a href="http://dl.dropbox.com/u/28629918/SpringHibernateDWR.zip"&gt;download the full example&lt;/a&gt;

&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/JavaObsession/~4/fgIz1M_0ULs" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javadrama.blogspot.com/feeds/4727797414121831261/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=9029038390867514964&amp;postID=4727797414121831261" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/9029038390867514964/posts/default/4727797414121831261?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/9029038390867514964/posts/default/4727797414121831261?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/JavaObsession/~3/fgIz1M_0ULs/spring-mvc-security-hibernate-dwr-page-4.html" title="Spring MVC, Security + Hibernate + DWR - Page 4" /><author><name>moutaz salem</name><uri>https://plus.google.com/106063653009079870460</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-VvhNV9YhJS8/AAAAAAAAAAI/AAAAAAAAAPI/hBEdI1Nhcis/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-6uBhfaHO8oo/UHLjptBRLdI/AAAAAAAAAM8/dShDCQbb7lQ/s72-c/springMVC3.PNG" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://javadrama.blogspot.com/2012/10/spring-mvc-security-hibernate-dwr-page-4.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUUCQHc8eyp7ImA9WhJaF00.&quot;"><id>tag:blogger.com,1999:blog-9029038390867514964.post-3759747543222456984</id><published>2012-10-08T05:26:00.000-07:00</published><updated>2012-10-08T07:41:01.973-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-10-08T07:41:01.973-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="hibernate" /><category scheme="http://www.blogger.com/atom/ns#" term="DWR" /><category scheme="http://www.blogger.com/atom/ns#" term="spring mvc" /><category scheme="http://www.blogger.com/atom/ns#" term="hibernate spring mvc DWR security" /><category scheme="http://www.blogger.com/atom/ns#" term="spring security" /><title>Spring MVC, Security + Hibernate + DWR - Page 3</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;strike&gt;&lt;/strike&gt;&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;br /&gt;
&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;h2 style="text-align: left;"&gt;
&lt;span style="font-size: x-large;"&gt;
Spring MVC, Security + Hibernate + DWR, Let's PLay&lt;/span&gt;&lt;/h2&gt;
&lt;div&gt;
OK, time for DWR ;)&lt;/div&gt;
&lt;br/&gt;

&lt;h3 style="text-align: left;"&gt;
the DWR servlet and Spring configuration:
&lt;/h3&gt;
in you web.xml edit the line to add a new file called dwr-context.xml
&lt;pre class="brush: xml"&gt;
&amp;lt;param-value&gt;/WEB-INF/applicationContext.xml,/WEB-INF/hibernate-context.xml,/WEB-INF/dwr-context.xml&amp;lt;/param-value&gt;
&lt;/pre&gt;

and add the DWR servlet and associate it with /dwr/* requests.
&lt;pre class="brush: xml"&gt;
&amp;lt;servlet&gt;
	&amp;lt;servlet-name&gt;dwr&amp;lt;/servlet-name&gt;
	&amp;lt;servlet-class&gt;org.directwebremoting.spring.DwrSpringServlet&amp;lt;/servlet-class&gt;
	&amp;lt;init-param&gt;
		&amp;lt;param-name&gt;debug&amp;lt;/param-name&gt;
		&amp;lt;param-value&gt;true&amp;lt;/param-value&gt;
	&amp;lt;/init-param&gt;
&amp;lt;/servlet&gt;
&amp;lt;servlet-mapping&gt;
	&amp;lt;servlet-name&gt;dwr&amp;lt;/servlet-name&gt;
	&amp;lt;url-pattern&gt;/dwr/*&amp;lt;/url-pattern&gt;
&amp;lt;/servlet-mapping&gt;
&lt;/pre&gt;

&lt;h3 style="text-align: left;"&gt;
dwr-context.xml:
&lt;/h3&gt;
under /WEB-INF create a new file called dwr-context.xml, the file will configure the integration points between Spring and DWR.
&lt;pre class="brush: xml"&gt;
&amp;lt;?xml version="1.0" encoding="UTF-8"?&gt;
&amp;lt;beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"&gt;

       &amp;lt;dwr:configuration /&gt;
       &amp;lt;dwr:annotation-config id="dwrAnnotationConfig"/&gt;
       &amp;lt;dwr:annotation-scan base-package="com.mtz.spring.dto"/&gt;

&amp;lt;/beans&gt;
&lt;/pre&gt;

this file defines three configurations:&lt;br/&gt;
1- a default dwr configuration: the default configurations is fine as we are relying mostly on annotatios.&lt;br/&gt;
2- annotation config: DWR will scan spring beans for certain annotations.&lt;br/&gt;
3- annotation scan: DWR will scan the class path for certain annotations.&lt;br/&gt;
&lt;br/&gt;&lt;br/&gt;
simply, if you have service class and want this service to be exposed to dwr you have two options, either, create a spring bean or let dwr scan its path, both ways, those classes had to have certain annotations.&lt;br/&gt;&lt;br/&gt;

&lt;h3 style="text-align: left;"&gt;
the User class
&lt;/h3&gt;
as the User is under the package &lt;b&gt;com.mtz.spring.dto&lt;/b&gt; and no Spring beans were (and mostly will never) created from that class, we need DWR to scan the classes under this package.&lt;br/&gt;

the class has been mentioned before, what needs to be explained&lt;br/&gt;
&lt;b&gt;@DataTransferObject&lt;/b&gt;: DWR will marshal and unmarshal this object using a certain converter, in our example it is &lt;b&gt;type="hibernate3", converter=H3BeanConverter.class&lt;/b&gt;, also for each property you need on the javascript object you have to expose it using the annotation &lt;b&gt;@RemoteProperty&lt;/b&gt;&lt;br/&gt;&lt;br/&gt;
&lt;h3 style="text-align: left;"&gt;
DAO and Service
&lt;/h3&gt;

in the package &lt;b&gt;com.mtz.spring.dao&lt;/b&gt;
&lt;h4 style="text-align: left;"&gt;
create the interface UserDAO
&lt;/h4&gt;
&lt;pre class="brush: java"&gt;
package com.mtz.spring.dao;

import com.mtz.spring.dto.User;
import java.util.List;

/**
 *
 * @author salemmo
 */
public interface UserDAO {
    public User getUser(int id);
    public User addUser(User user);
    public User removeUser(User user);
    public List&amp;lt;User&gt; getAllUsers();
}

&lt;/pre&gt;

in the package &lt;b&gt;com.mtz.spring.dao&lt;/b&gt;
&lt;h4 style="text-align: left;"&gt;
create the class UserDAOImpl
&lt;/h4&gt;
&lt;pre class="brush: java"&gt;
package com.mtz.spring.dao;

import com.mtz.spring.dto.User;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author salemmo
 */
public class UserDAOImpl implements UserDAO{

    public User getUser(int id) {
        Session session = sessionFactory.getCurrentSession();
        User u = (User) session.get(User.class, id);
        return u;
    }

    public User addUser(User user) {
        Session session = sessionFactory.getCurrentSession();
        session.saveOrUpdate(user);
        return user;
    }

    public User removeUser(User user) {
        Session session = sessionFactory.getCurrentSession();
        session.delete(user);
        return user;
    }

    public List&amp;lt;User&gt; getAllUsers() {
        Session session = sessionFactory.getCurrentSession();
        return session.createQuery("from User").list();
    }
    
    private SessionFactory sessionFactory;
    /**
     * @return the sessionFactory
     */
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    /**
     * @param sessionFactory the sessionFactory to set
     */
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
}
&lt;/pre&gt;

in the package &lt;b&gt;com.mtz.spring.service&lt;/b&gt;
&lt;h4 style="text-align: left;"&gt;
create the interface UserService
&lt;/h4&gt;
&lt;pre class="brush: java"&gt;
package com.mtz.spring.service;

import com.mtz.spring.dto.User;
import java.util.List;

/**
 *
 * @author salemmo
 */
public interface UserService {
    public User getUser(int id);
    public User addUser(User user);
    public User removeUser(User user);
    public List&amp;lt;User&gt; getAllUsers();
}
&lt;/pre&gt;

in the package &lt;b&gt;com.mtz.spring.service&lt;/b&gt;
&lt;h4 style="text-align: left;"&gt;
create the class UserServiceImpl
&lt;/h4&gt;
&lt;pre class="brush: java"&gt;
package com.mtz.spring.service;

import com.mtz.spring.dao.UserDAO;
import com.mtz.spring.dto.User;
import java.util.List;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author salemmo
 */
@RemoteProxy
public class UserServiceImpl implements UserService{

    
    @RemoteMethod
    @Transactional
    public User getUser(int id) {
        return getUserDAO().getUser(id);
    }

    @Transactional
    @RemoteMethod
    public User addUser(User user) {
        return getUserDAO().addUser(user);
    }
    
    @Transactional
    @RemoteMethod
    public User removeUser(User user) {
        return getUserDAO().removeUser(user);
    }

    @Transactional
    @RemoteMethod
    public List&amp;lt;User&gt; getAllUsers() {
        return getUserDAO().getAllUsers();
    }
    
    private UserDAO userDAO;

    /**
     * @return the userDAO
     */
    public UserDAO getUserDAO() {
        return userDAO;
    }

    /**
     * @param userDAO the userDAO to set
     */
    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }
    
}

&lt;/pre&gt;

the important things to know about the UserServiceImpl are the annotations.&lt;br/&gt;
&lt;b&gt;@RemoteProxy:&lt;/b&gt; tells DWR that you want this service to be exposed.&lt;br/&gt;
&lt;b&gt;@RemoteMethod:&lt;/b&gt; tells DWR that you want this method to be exposed.&lt;br/&gt;
&lt;b&gt;@Transactional:&lt;/b&gt; tells hibernate that you want this method to run in transaction.&lt;br/&gt;&lt;br/&gt;

&lt;h3 style="text-align: left;"&gt;
Wrap it up in the applicationContext.xml
&lt;/h3&gt;
edit your applicationContext.xml to add the service bean
&lt;pre class="brush: xml"&gt;
&amp;lt;bean class="com.mtz.spring.dao.UserDAOImpl" id="userDAO"&gt;
	&amp;lt;property name="sessionFactory" ref="sessionFactory"/&gt;
&amp;lt;/bean&gt;

&amp;lt;bean class="com.mtz.spring.service.UserServiceImpl" id="userService"&gt;
	&amp;lt;property name="userDAO" ref="userDAO"/&gt;
&amp;lt;/bean&gt;
&lt;/pre&gt;

&lt;h3 style="text-align: left;"&gt;
edit hello.jsp
&lt;/h3&gt;
edit /WEB-INF/jsp/hello.jsp to make use of DWR
&lt;pre class="brush: html"&gt;
&amp;lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
&amp;lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd"&gt;

	&amp;lt;%
		String contextPath=request.getContextPath();
	%&gt;
	
&amp;lt;html&gt;
    &amp;lt;head&gt;
        &amp;lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
        &amp;lt;script type='text/javascript' src='&amp;lt;%=contextPath%&gt;/dwr/engine.js'&gt;&amp;lt;/script&gt;
        &amp;lt;script type='text/javascript' src='&amp;lt;%=contextPath%&gt;/dwr/interface/UserServiceImpl.js'&gt;&amp;lt;/script&gt;
        &amp;lt;script type='text/javascript' src='&amp;lt;%=contextPath%&gt;/dwr/util.js'&gt;&amp;lt;/script&gt;
        &amp;lt;title&gt;Welcome to Spring Web MVC project&amp;lt;/title&gt;
        &amp;lt;script type="text/javascript"&gt;
            function goGetit(){
                UserServiceImpl.getUser(document.getElementById("userid").value, {
                    callback:function(user) { 
                        document.getElementById("userage").value=user.age;
                        document.getElementById("username").value=user.name;
                    }
                });
            }
            
            function goSaveit(){
                var user= {
                    id: document.getElementById("userid").value,
                    age:document.getElementById("userage").value,
                    name:document.getElementById("username").value
                };
                UserServiceImpl.addUser(user, {
                    callback:function(user) { 
                        var useraction = (document.getElementById("userid").value=="")? "added":"altered";
                        document.getElementById("message").innerHTML = "User "+useraction+" with the ID: "+user.id;
                        getAllUsers();
                    }
                });
            }
            
            function goRemoveit(){
                var user= {
                    id: document.getElementById("userid").value
                };
                UserServiceImpl.removeUser(user, {
                    callback:function(user) { 
                        document.getElementById("userid").value="";
                        document.getElementById("userage").value="";
                        document.getElementById("username").value="";
                        document.getElementById("message").innerHTML = "User removed with the ID: "+user.id;
                        getAllUsers();
                    }
                });
            }
            
            function getAllUsers(){
                
                UserServiceImpl.getAllUsers({
                    callback:function(users) {
                        var cellFunctions = [
                            function(user) { return user.id; },
                            function(user) { return user.age; },
                            function(user) { return user.name; }
                        ];
                        dwr.util.removeAllRows("allusers");
                        dwr.util.addRows( "allusers", users, cellFunctions,{ escapeHtml:false });
                    }
                });
            }
        &amp;lt;/script&gt;
    &amp;lt;/head&gt;

    &amp;lt;body onload="getAllUsers()"&gt;
        &amp;lt;H2&gt;Hello! This is the default welcome page for a Spring Web MVC project.&amp;lt;/H2&gt;
        &amp;lt;h3&gt;${message}&amp;lt;h3/&gt;
            &amp;lt;input type="text" id="userid"&gt;&amp;lt;input type="button" value="Get User By ID" onclick="goGetit();"/&gt;
            &amp;lt;hr width="70%"/&gt;
            &amp;lt;div id="message"&gt; &amp;lt;/div&gt;
            &amp;lt;table&gt;
                &amp;lt;tr&gt;
                    &amp;lt;td&gt;Age:&amp;lt;/td&gt;
                    &amp;lt;td&gt;&amp;lt;input type="text" id="userage"&gt;&amp;lt;/td&gt;
                &amp;lt;/tr&gt;
                &amp;lt;tr&gt;
                    &amp;lt;td&gt;Name:&amp;lt;/td&gt;
                    &amp;lt;td&gt;&amp;lt;input type="text" id="username"&gt;&amp;lt;/td&gt;
                &amp;lt;/tr&gt;
                &amp;lt;tr&gt;
                    &amp;lt;td&gt;&amp;lt;input type="button" value="Add/Edit User" onclick="goSaveit();"/&gt;&amp;lt;/td&gt;
                    &amp;lt;td&gt;&amp;lt;input type="button" value="Remove User" onclick="goRemoveit();"/&gt;&amp;lt;/td&gt;
                &amp;lt;/tr&gt;
            &amp;lt;/table&gt;
            &amp;lt;hr width="70%"/&gt;
            &amp;lt;table border="1"&gt;
                &amp;lt;thead&gt;
                    &amp;lt;tr&gt;
                        &amp;lt;th&gt;ID&amp;lt;/th&gt;
                        &amp;lt;th&gt;Age&amp;lt;/th&gt;
                        &amp;lt;th&gt;Name&amp;lt;/th&gt;
                    &amp;lt;/tr&gt;
                &amp;lt;/thead&gt;
                &amp;lt;tbody id="allusers"&gt; &amp;lt;/tbody&gt;
            &amp;lt;/table&gt;
    &amp;lt;/body&gt;
&amp;lt;/html&gt;

&lt;/pre&gt;

&lt;h4 style="text-align: left;"&gt;
Test it
&lt;/h4&gt;
deploy the project and you should see the following page

&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-JxxgwdhtZ68/UHLZCh71BtI/AAAAAAAAAMk/aG9462xllFo/s1600/springMVC2.PNG" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="140" style="border:2px; border-style:solid" width="400" src="http://4.bp.blogspot.com/-JxxgwdhtZ68/UHLZCh71BtI/AAAAAAAAAMk/aG9462xllFo/s400/springMVC2.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;




&lt;br/&gt;&lt;br/&gt;
&lt;a href="http://javadrama.blogspot.co.uk/2012/10/spring-mvc-security-hibernate-dwr-page-4.html"&gt;Next - Configuring Spring Security&lt;/a&gt;













&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/JavaObsession/~4/2URLkvbUIXU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javadrama.blogspot.com/feeds/3759747543222456984/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=9029038390867514964&amp;postID=3759747543222456984" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/9029038390867514964/posts/default/3759747543222456984?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/9029038390867514964/posts/default/3759747543222456984?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/JavaObsession/~3/2URLkvbUIXU/spring-mvc-security-hibernate-dwr-page-3.html" title="Spring MVC, Security + Hibernate + DWR - Page 3" /><author><name>moutaz salem</name><uri>https://plus.google.com/106063653009079870460</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-VvhNV9YhJS8/AAAAAAAAAAI/AAAAAAAAAPI/hBEdI1Nhcis/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-JxxgwdhtZ68/UHLZCh71BtI/AAAAAAAAAMk/aG9462xllFo/s72-c/springMVC2.PNG" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://javadrama.blogspot.com/2012/10/spring-mvc-security-hibernate-dwr-page-3.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUADQ3Y-fip7ImA9WhJaF0o.&quot;"><id>tag:blogger.com,1999:blog-9029038390867514964.post-3694849973297266207</id><published>2012-10-07T08:34:00.001-07:00</published><updated>2012-10-09T02:09:32.856-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-10-09T02:09:32.856-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="hibernate" /><category scheme="http://www.blogger.com/atom/ns#" term="DWR" /><category scheme="http://www.blogger.com/atom/ns#" term="spring mvc" /><category scheme="http://www.blogger.com/atom/ns#" term="hibernate spring mvc DWR security" /><category scheme="http://www.blogger.com/atom/ns#" term="spring security" /><title>Spring MVC, Security + Hibernate + DWR - Page 2</title><content type="html">&lt;strike&gt;&lt;/strike&gt;&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;br /&gt;
&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;h2 style="text-align: left;"&gt;
&lt;span style="font-size: x-large;"&gt;
Spring MVC, Security + Hibernate + DWR, Let's PLay&lt;/span&gt;&lt;/h2&gt;
&lt;div&gt;
OK, our Spring MVC should be working by now, let's add Hibernate ;)&lt;/div&gt;
&lt;br/&gt;
create a table with the following specifications
&lt;h3 style="text-align: left;"&gt;
MySQL table SQL script:
&lt;/h3&gt;
&lt;pre class="brush: sql"&gt;

CREATE TABLE `user_` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `age` int(11) DEFAULT NULL,
  `name` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

&lt;/pre&gt;

now, configure your spring context to load a new file called hibernate-context.xml.
edit your web.xml to add the mentioned file
&lt;pre class="brush: xml"&gt;
&amp;lt;param-value&gt;/WEB-INF/applicationContext.xml,/WEB-INF/hibernate-context.xml&amp;lt;/param-value&gt;
&lt;/pre&gt;

&lt;h3 style="text-align: left;"&gt;
hibernate-context.xml:
&lt;/h3&gt;
create a new file called hibernate-context.xml under /WEB-INF with the following content
&lt;pre class="brush: xml"&gt;
&amp;lt;?xml version="1.0" encoding="UTF-8"?&gt;
&amp;lt;beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"&gt;

	&amp;lt;bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
		p:location="/WEB-INF/jdbc.properties" /&gt;

	&amp;lt;bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource"
		p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
		p:username="${jdbc.username}" p:password="${jdbc.password}" /&gt;

	&amp;lt;bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;

		&amp;lt;property name="dataSource"&gt;
			&amp;lt;ref bean="dataSource" /&gt;
		&amp;lt;/property&gt;

		&amp;lt;property name="hibernateProperties"&gt;
			&amp;lt;props&gt;
				&amp;lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&amp;lt;/prop&gt;
				&amp;lt;prop key="hibernate.show_sql"&gt;true&amp;lt;/prop&gt;
			&amp;lt;/props&gt;
		&amp;lt;/property&gt;

		&amp;lt;property name="annotatedClasses"&gt;
			&amp;lt;list&gt;
				&amp;lt;value&gt;com.mtz.spring.dto.User&amp;lt;/value&gt;
			&amp;lt;/list&gt;
		&amp;lt;/property&gt;

	&amp;lt;/bean&gt;

	&amp;lt;bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager"
		p:sessionFactory-ref="sessionFactory" /&gt;

	&amp;lt;tx:annotation-driven /&gt;

&amp;lt;/beans&gt;
&lt;/pre&gt;

basically, this file defines the following:&lt;br/&gt;

&lt;b&gt;propertyConfigurer&lt;/b&gt;: spring will look for a properties file called jdbc.properties to replace the place holders mentioned here which are related to the database connection arguments.&lt;br/&gt;
&lt;b&gt;dataSource&lt;/b&gt;: the datasource object will be used by the Hibernate session factory to connect to the database.&lt;br/&gt;
&lt;b&gt;sessionFactory:&lt;/b&gt; the session factory that will be used by Hibernate, and it defines an attribute called &lt;b&gt;annotatedClasses&lt;/b&gt; which are Hibernate mapping objects&lt;br/&gt;
&lt;b&gt;transactionManager:&lt;/b&gt; we define this bean so we can be able to do database queries in transactions.
&lt;b&gt;Transaction Annotations&lt;/b&gt;: we will configure transactions via annotations.&lt;br/&gt;
alternatively, you can configure the session factory to scan the class path
&lt;pre class="brush: xml"&gt;
&amp;lt;property name="packagesToScan" value="com.mtz.spring.dto"/&gt;
&lt;/pre&gt;
&lt;h3 style="text-align: left;"&gt;
jdbc.properties:
&lt;/h3&gt;
under /WEB-INF create a new file named jdbc.properties with the following content, please edit for your test
&lt;pre class="brush: html"&gt;
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=root
&lt;/pre&gt;

&lt;h3 style="text-align: left;"&gt;
the User class
&lt;/h3&gt;
under the package &lt;b&gt;com.mtz.spring.dto&lt;/b&gt; create a file named User.java
&lt;pre class="brush: java"&gt;
package com.mtz.spring.dto;

import java.io.Serializable;
import javax.persistence.*;
import org.directwebremoting.annotations.DataTransferObject;
import org.directwebremoting.annotations.RemoteProperty;
import org.directwebremoting.hibernate.H3BeanConverter;
/**
 *
 * @author salemmo
 */
@DataTransferObject(type="hibernate3", converter=H3BeanConverter.class)
@Entity
@Table(name="user_", catalog="spring")
public class User implements Serializable{
    @RemoteProperty
    private String name;
    @RemoteProperty
    private int age;
    @RemoteProperty
    private int id;

    /**
     * @return the name
     */
    @Column(name="name")
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the age
     */
    @Column(name="age")
    public int getAge() {
        return age;
    }

    /**
     * @param age the age to set
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * @return the id
     */
    @Id
    @GeneratedValue(strategy=javax.persistence.GenerationType.IDENTITY)
    @Column(name="id")
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }
}
&lt;/pre&gt;
here we define the mapping between this class and the database table using the annotations @Entity, @Table @Column and ID specific annotations (don't give so much attention to the @DataTransferObject and @RemoteProperty as they will be used by DWR later on)

&lt;h3 style="text-align: left;"&gt;
OpenSssionInView Filter
&lt;/h3&gt;
we don't want to open session manually and maybe forget to close them, can we do it automatically? also, i have a lazy initialized property can't use it in my JSPs because the object sent to the view was de-attached from the Hibernate context.

Well, Hibernate has a Filter called OpenSssionInView Filter that will solve both problems, and indeed, Spring has wrapped this filter. to enable this filter edit your web.xml to add
&lt;pre class="brush: xml"&gt;
&amp;lt;filter&gt;
	&amp;lt;filter-name&gt;hibernateFilter&amp;lt;/filter-name&gt;
	&amp;lt;filter-class&gt;
		org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
	&amp;lt;/filter-class&gt;
	&amp;lt;init-param&gt;
		&amp;lt;param-name&gt;sessionFactoryBeanName&amp;lt;/param-name&gt;
		&amp;lt;param-value&gt;sessionFactory&amp;lt;/param-value&gt;
	&amp;lt;/init-param&gt;
&amp;lt;/filter&gt;

&amp;lt;filter-mapping&gt;
	&amp;lt;filter-name&gt;hibernateFilter&amp;lt;/filter-name&gt;
	&amp;lt;url-pattern&gt;/*&amp;lt;/url-pattern&gt;
&amp;lt;/filter-mapping&gt;
&lt;/pre&gt;
of course the filter needs one session factory to create sessions.&lt;br/&gt;

&lt;h4 style="text-align: left;"&gt;
Test it
&lt;/h4&gt;
no changes are expected, still we see our page, however, make sure that you did it right by not seeing any exceptions in the logs.


&lt;br/&gt;&lt;br/&gt;
&lt;a href="http://javadrama.blogspot.co.uk/2012/10/spring-mvc-security-hibernate-dwr-page-3.html"&gt;Next - Configuring DWR&lt;/a&gt;


&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/JavaObsession/~4/fAVpXyWWVyU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javadrama.blogspot.com/feeds/3694849973297266207/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=9029038390867514964&amp;postID=3694849973297266207" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/9029038390867514964/posts/default/3694849973297266207?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/9029038390867514964/posts/default/3694849973297266207?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/JavaObsession/~3/fAVpXyWWVyU/spring-mvc-security-hibernate-dwr-page-2.html" title="Spring MVC, Security + Hibernate + DWR - Page 2" /><author><name>moutaz salem</name><uri>https://plus.google.com/106063653009079870460</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-VvhNV9YhJS8/AAAAAAAAAAI/AAAAAAAAAPI/hBEdI1Nhcis/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javadrama.blogspot.com/2012/10/spring-mvc-security-hibernate-dwr-page-2.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUUHSHo_eyp7ImA9WhJaF0o.&quot;"><id>tag:blogger.com,1999:blog-9029038390867514964.post-1618428756142535515</id><published>2012-10-07T04:18:00.000-07:00</published><updated>2012-10-09T02:00:39.443-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-10-09T02:00:39.443-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="hibernate" /><category scheme="http://www.blogger.com/atom/ns#" term="DWR" /><category scheme="http://www.blogger.com/atom/ns#" term="spring mvc" /><category scheme="http://www.blogger.com/atom/ns#" term="hibernate spring mvc DWR security" /><category scheme="http://www.blogger.com/atom/ns#" term="spring security" /><title>Spring MVC, Security + Hibernate + DWR - Page 1</title><content type="html">&lt;strike&gt;&lt;/strike&gt;&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;br /&gt;
&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;h2 style="text-align: left;"&gt;
&lt;span style="font-size: x-large;"&gt;
Spring MVC, Security + Hibernate + DWR, Let's PLay&lt;/span&gt;&lt;/h2&gt;
&lt;div&gt;
OK, that is a lot, but it was fun getting those guys working together in harmony ;)&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Why?&lt;/div&gt;
&lt;div&gt;
Spring has proven to be a solid playground and a unique platform, here how the parts will act:&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;&lt;a href="http://www.springsource.org/" target="_blank"&gt;Spring Framework&lt;/a&gt;: will act as a platform and a glue.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html" target="_blank"&gt;Spring MVC&lt;/a&gt;: will act as the web framework.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://static.springsource.org/spring-security/site/" target="_blank"&gt;Spring Security&lt;/a&gt;: we want to secure our site, indeed.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.hibernate.org/" target="_blank"&gt;Hibernate&lt;/a&gt;: will act as an ORM.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://directwebremoting.org/dwr/index.html" target="_blank"&gt;DWR&lt;/a&gt;: will act as a AJAX framework.&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote class="tr_bq"&gt;
So, first things first, &lt;a href="http://maven.apache.org/" target="_blank"&gt;Maven&lt;/a&gt;, all the artifacts(shiny name for a jar/war files) that are required in in this example relies on Maven, all but dwr, by the time of writing this blog, "At the time of our last development build (3.0 RC2) we did not have a process in place to upload artifacts to Maven Central", the dwr site says.&lt;/blockquote&gt;
This issue is easy to fix, we will install dwr.jar manually to our local repository, even better, if you are using &lt;a href="http://archiva.apache.org/" target="_blank"&gt;archiva&lt;/a&gt;; upload the artifact.&lt;br /&gt;
&lt;div style="text-align: left;"&gt;
&lt;pre class="brush: html"&gt;mvn install:install-file -Dfile=path/to/dwr.jar -DgroupId=org.directwebremoting -DartifactId=dwr -Dversion=3.0RC2 -Dpackaging=jar&lt;/pre&gt;
&lt;/div&gt;
By now we can go ahead and create a new project using maven archetype.&lt;br /&gt;
&lt;pre class="brush: html"&gt;mvn archetype:generate&lt;/pre&gt;
we will start with a plain java web archetype, choose maven-archetype-webapp or number 224 as per my Maven, i ended up with those parameters&lt;/div&gt;
&lt;/div&gt;
&lt;pre class="brush: html" style="text-align: left;"&gt;groupId: com.mtz.spring
artifactId: SpringHibernateDWR
version: 1.0.0
package: com.mtz.spring
&lt;/pre&gt;
&lt;h3 style="text-align: left;"&gt;
Enable Java web 2.5 specifications:
&lt;/h3&gt;
edit /WEB-INF/web.xml
&lt;pre class="brush: xml"&gt;
&amp;lt;!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" &gt;

&amp;lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"&gt;
&lt;/pre&gt;
&lt;h3 style="text-align: left;"&gt;
Maven Dependencies:
&lt;/h3&gt;
edit the pom.xml to include the following dependencies
&lt;br /&gt;
&lt;pre class="brush: xml"&gt;
&amp;lt;dependency&gt;
 &amp;lt;groupId&gt;junit&amp;lt;/groupId&gt;
 &amp;lt;artifactId&gt;junit&amp;lt;/artifactId&gt;
 &amp;lt;version&gt;3.8.1&amp;lt;/version&gt;
 &amp;lt;scope&gt;test&amp;lt;/scope&gt;
&amp;lt;/dependency&gt;

&amp;lt;dependency&gt;
 &amp;lt;groupId&gt;log4j&amp;lt;/groupId&gt;
 &amp;lt;artifactId&gt;log4j&amp;lt;/artifactId&gt;
 &amp;lt;version&gt;1.2.17&amp;lt;/version&gt;
&amp;lt;/dependency&gt;

&amp;lt;!--Spring--&gt;
&amp;lt;dependency&gt;
 &amp;lt;groupId&gt;org.springframework&amp;lt;/groupId&gt;
 &amp;lt;artifactId&gt;spring-orm&amp;lt;/artifactId&gt;
 &amp;lt;version&gt;3.0.6.RELEASE&amp;lt;/version&gt;
&amp;lt;/dependency&gt;
&amp;lt;dependency&gt;
 &amp;lt;groupId&gt;org.springframework&amp;lt;/groupId&gt;
 &amp;lt;artifactId&gt;spring-web&amp;lt;/artifactId&gt;
 &amp;lt;version&gt;3.0.6.RELEASE&amp;lt;/version&gt;
&amp;lt;/dependency&gt;
&amp;lt;dependency&gt;
 &amp;lt;groupId&gt;org.springframework&amp;lt;/groupId&gt;
 &amp;lt;artifactId&gt;spring-webmvc&amp;lt;/artifactId&gt;
 &amp;lt;version&gt;3.0.6.RELEASE&amp;lt;/version&gt;
&amp;lt;/dependency&gt;
&amp;lt;!--End Spring--&gt;

&amp;lt;!--Spring Security--&gt;
&amp;lt;dependency&gt;
 &amp;lt;groupId&gt;org.springframework.security&amp;lt;/groupId&gt;
 &amp;lt;artifactId&gt;spring-security-core&amp;lt;/artifactId&gt;
 &amp;lt;version&gt;3.0.7.RELEASE&amp;lt;/version&gt;
&amp;lt;/dependency&gt; 
&amp;lt;dependency&gt;
 &amp;lt;groupId&gt;org.springframework.security&amp;lt;/groupId&gt;
 &amp;lt;artifactId&gt;spring-security-web&amp;lt;/artifactId&gt;
 &amp;lt;version&gt;3.0.7.RELEASE&amp;lt;/version&gt;
&amp;lt;/dependency&gt; 
&amp;lt;dependency&gt;
 &amp;lt;groupId&gt;org.springframework.security&amp;lt;/groupId&gt;
 &amp;lt;artifactId&gt;spring-security-config&amp;lt;/artifactId&gt;
 &amp;lt;version&gt;3.0.7.RELEASE&amp;lt;/version&gt;
&amp;lt;/dependency&gt;
&amp;lt;!--End Spring Security--&gt;

&amp;lt;!-- MySQL database driver --&gt;
&amp;lt;dependency&gt;
 &amp;lt;groupId&gt;mysql&amp;lt;/groupId&gt;
 &amp;lt;artifactId&gt;mysql-connector-java&amp;lt;/artifactId&gt;
 &amp;lt;version&gt;5.1.9&amp;lt;/version&gt;
&amp;lt;/dependency&gt;
&amp;lt;!-- End MySQL--&gt;

&amp;lt;!--DWR--&gt;
&amp;lt;dependency&gt;
 &amp;lt;groupId&gt;org.directwebremoting&amp;lt;/groupId&gt;
 &amp;lt;artifactId&gt;dwr&amp;lt;/artifactId&gt;
 &amp;lt;version&gt;3.0RC2&amp;lt;/version&gt;
 &amp;lt;type&gt;jar&amp;lt;/type&gt;
&amp;lt;/dependency&gt;
&amp;lt;!--End DWR--&gt;

&amp;lt;!-- Hibernate framework --&gt;
&amp;lt;dependency&gt;
 &amp;lt;groupId&gt;org.hibernate&amp;lt;/groupId&gt;
 &amp;lt;artifactId&gt;hibernate-core&amp;lt;/artifactId&gt;
 &amp;lt;version&gt;3.6.10.Final&amp;lt;/version&gt;
&amp;lt;/dependency&gt;
&amp;lt;dependency&gt;
 &amp;lt;groupId&gt;org.hibernate&amp;lt;/groupId&gt;
 &amp;lt;artifactId&gt;hibernate-ehcache&amp;lt;/artifactId&gt;
 &amp;lt;version&gt;3.6.10.Final&amp;lt;/version&gt;
&amp;lt;/dependency&gt;
&amp;lt;dependency&gt;
 &amp;lt;groupId&gt;javassist&amp;lt;/groupId&gt;
 &amp;lt;artifactId&gt;javassist&amp;lt;/artifactId&gt;
 &amp;lt;version&gt;3.12.1.GA&amp;lt;/version&gt;
&amp;lt;/dependency&gt;
&amp;lt;!-- Hibernate library dependecy end --&gt;

&amp;lt;!-- apache commons --&gt;
&amp;lt;dependency&gt;
 &amp;lt;groupId&gt;commons-logging&amp;lt;/groupId&gt;
 &amp;lt;artifactId&gt;commons-logging&amp;lt;/artifactId&gt;
 &amp;lt;version&gt;1.1.1&amp;lt;/version&gt;
&amp;lt;/dependency&gt;
&amp;lt;!-- end apache commons --&gt;
&lt;/pre&gt;
under the resources directory create two files.
&lt;br /&gt;
log4j.properties
&lt;br /&gt;
&lt;pre class="brush: html"&gt;# Root logger option
log4j.rootLogger=DEBUG, stdout
 
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
&lt;/pre&gt;
logging.properties
&lt;br /&gt;
&lt;pre class="brush: html"&gt;org.apache.catalina.core.ContainerBase.[Catalina].level = DEBUG
org.apache.catalina.core.ContainerBase.[Catalina].handlers = java.util.logging.ConsoleHandler
&lt;/pre&gt;
i wanted to see a lot of loggings, so i set both to DEBUG&lt;br /&gt;
now build your project with maven and make sure everything is OK.
&lt;br /&gt;
&lt;pre class="brush: html"&gt;mvn clean package
&lt;/pre&gt;
&lt;h3 style="text-align: left;"&gt;
Spring MVC:
&lt;/h3&gt;
in your web.xml add
&lt;br /&gt;
&lt;pre class="brush: xml"&gt;
  &amp;lt;context-param&gt;
 &amp;lt;param-name&gt;contextConfigLocation&amp;lt;/param-name&gt;
 &amp;lt;param-value&gt;/WEB-INF/applicationContext.xml&amp;lt;/param-value&gt;
&amp;lt;/context-param&gt;
&amp;lt;listener&gt;
 &amp;lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&amp;lt;/listener-class&gt;
&amp;lt;/listener&gt;
&amp;lt;servlet&gt;
 &amp;lt;servlet-name&gt;spring&amp;lt;/servlet-name&gt;
 &amp;lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&amp;lt;/servlet-class&gt;
 &amp;lt;init-param&gt;
  &amp;lt;param-name&gt;contextConfigLocation&amp;lt;/param-name&gt;
  &amp;lt;param-value&gt;/WEB-INF/spring-mvc.xml&amp;lt;/param-value&gt;
 &amp;lt;/init-param&gt;
 &amp;lt;load-on-startup&gt;2&amp;lt;/load-on-startup&gt;
&amp;lt;/servlet&gt;
&amp;lt;servlet-mapping&gt;
 &amp;lt;servlet-name&gt;spring&amp;lt;/servlet-name&gt;
 &amp;lt;url-pattern&gt;*.html&amp;lt;/url-pattern&gt;
&amp;lt;/servlet-mapping&gt;
&amp;lt;welcome-file-list&gt;
	&amp;lt;welcome-file&gt;redirect.jsp&amp;lt;/welcome-file&gt;
&amp;lt;/welcome-file-list&gt;
&lt;/pre&gt;
this is basically,&lt;br /&gt;
1- initializing the the Spring context using ContextLoaderListener which will look for a file named applicationContext.xml under WEB-INF.

&lt;br/&gt;2- loads the Spring MVC dispatcher servlet, the dispatcher will look for a file named spring-mvc.xml under WEB-INF and it also associate *.html requests to it.

&lt;br/&gt;3- the container will use redirect.jsp as the default landing page.
&lt;br/&gt;&lt;br/&gt;
&lt;h4 style="text-align: left;"&gt;
applicationContext.xml
&lt;/h4&gt;
we will just create a bean-less file that will be used later on, create the file under /WEB-INF
&lt;pre class="brush: xml"&gt;
&amp;lt;?xml version="1.0" encoding="UTF-8"?&gt;
&amp;lt;beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"&gt;


&amp;lt;/beans&gt;
&lt;/pre&gt;

&lt;h4 style="text-align: left;"&gt;
spring-mvc.xml
&lt;/h4&gt;
create a file with that name under /WEB-INF with the following content
&lt;pre class="brush: xml"&gt;
&amp;lt;?xml version="1.0" encoding="UTF-8"?&gt;
&amp;lt;beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"&gt;

    &amp;lt;context:component-scan base-package="com.mtz.spring.mvc.controller"/&gt;

    &amp;lt;bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" /&gt;

&amp;lt;/beans&gt;


&lt;/beans&gt;
&lt;/pre&gt;
this file configures Spring MVC to &lt;br/&gt;
1-Look for classes annotated with @controller in any class under com.mtz.spring.mvc.controller package.&lt;br/&gt;
2-add a basic view resolver that will map strings returned from the controller to a jsp file.
&lt;br/&gt;&lt;br/&gt;
&lt;h4 style="text-align: left;"&gt;
HelloController.java
&lt;/h4&gt;
under the package com.mtz.spring.mvc.controller create a new class called HelloController with the following content
&lt;pre class="brush: java"&gt;
package com.mtz.spring.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
@Controller
@RequestMapping("/welcome")
public class HelloController {
 
 @RequestMapping(method = RequestMethod.GET)
 public String printWelcome(ModelMap model) {
 
  model.addAttribute("message", "Spring + Hibernate + DWR, Hello World");
  return "hello";
 
 }
 
}
&lt;/pre&gt;
here, Spring MVC will bind /welcome.html requests to be handled by this controller, the controller will return its view name which is "hello" then the view resolver will map it to /WEB-INF/jsp/hello.jsp
&lt;br/&gt;&lt;br/&gt;
&lt;h4 style="text-align: left;"&gt;
hello.jsp
&lt;/h4&gt;
under /WEB-INF create a directory named jsp and then create a jsp file called hello.jsp with the following content
&lt;pre class="brush: html"&gt;
&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
&lt;H2&gt;Hello! This is the default welcome page for a Spring Web MVC project.&lt;/H2&gt;
&lt;h3&gt;Message: ${message}&lt;/h3&gt;
&lt;/pre&gt;
this jsp will print the message that was sent from the controller.
&lt;br/&gt;&lt;br/&gt;
&lt;h4 style="text-align: left;"&gt;
redirect.jsp
&lt;/h4&gt;
create a new jsp under webapp directory with the following content
&lt;pre class="brush: html"&gt;
&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
&lt;% response.sendRedirect("welcome.html"); %&gt;
&lt;/pre&gt;
the redirect.jsp will redirect the requests from / to /welcome.html which will trigger the Spring MVC controller to work.
&lt;br/&gt;&lt;br/&gt;
&lt;h4 style="text-align: left;"&gt;
Test it
&lt;/h4&gt;
now package your project and deploy it, navigate to http://localhost:8080/SpringHibernateDWR/, if everything is correct, you should see this page.
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-0wMy_BAzXJ8/UHGd4JL2rZI/AAAAAAAAAMI/vZ0O2bu49UE/s1600/springMVC.PNG" imageanchor="1" style="margin-left:1em; margin-right:1em; border:5px"&gt;&lt;img border="0" height="43" width="400" style="border:2px; border-style:solid" src="http://2.bp.blogspot.com/-0wMy_BAzXJ8/UHGd4JL2rZI/AAAAAAAAAMI/vZ0O2bu49UE/s400/springMVC.PNG" /&gt;&lt;/a&gt;&lt;/div&gt;

&lt;br/&gt;&lt;br/&gt;
&lt;a href="http://javadrama.blogspot.co.uk/2012/10/spring-mvc-security-hibernate-dwr-page-2.html"&gt;Next - Configuring Hibernate&lt;/a&gt;

&lt;!--http://dl.dropbox.com/u/28629918/SpringHibernateDWR.zip--&gt;

&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/JavaObsession/~4/-bChzjUNlY4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javadrama.blogspot.com/feeds/1618428756142535515/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=9029038390867514964&amp;postID=1618428756142535515" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/9029038390867514964/posts/default/1618428756142535515?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/9029038390867514964/posts/default/1618428756142535515?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/JavaObsession/~3/-bChzjUNlY4/spring-mvc-security-hibernate-dwr-page-1.html" title="Spring MVC, Security + Hibernate + DWR - Page 1" /><author><name>moutaz salem</name><uri>https://plus.google.com/106063653009079870460</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-VvhNV9YhJS8/AAAAAAAAAAI/AAAAAAAAAPI/hBEdI1Nhcis/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-0wMy_BAzXJ8/UHGd4JL2rZI/AAAAAAAAAMI/vZ0O2bu49UE/s72-c/springMVC.PNG" height="72" width="72" /><thr:total>2</thr:total><feedburner:origLink>http://javadrama.blogspot.com/2012/10/spring-mvc-security-hibernate-dwr-page-1.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0cFQHwycSp7ImA9WhVTEU0.&quot;"><id>tag:blogger.com,1999:blog-9029038390867514964.post-3231410167019254298</id><published>2012-02-22T01:10:00.000-08:00</published><updated>2012-02-24T09:36:51.299-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-02-24T09:36:51.299-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="redhat" /><category scheme="http://www.blogger.com/atom/ns#" term="cpu" /><category scheme="http://www.blogger.com/atom/ns#" term="java" /><category scheme="http://www.blogger.com/atom/ns#" term="linux" /><category scheme="http://www.blogger.com/atom/ns#" term="processor" /><title>Figure out why is JAVA eating CPU?</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;span style="font-size: large;"&gt;I&lt;/span&gt;n a production environment profiling is not an option, we have seen several times that our CPU has reached almost 100%, we are running tomcat on Redhat 5.5, so what is happening?&lt;br /&gt;
&lt;br /&gt;
Fortunately, java comes with some great debugging tools,&amp;nbsp;cooperating those tools with Linux built-in tools will let you know what is&amp;nbsp;happening.&lt;br /&gt;
&lt;br /&gt;
Here is what i am going to explain:&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;An introduction about java threads and its relation to what-so-called Linux LWP&lt;/li&gt;
&lt;li&gt;A step-by-step&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
1- An introduction&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
as you may know, any java program starts when the JVM calls the main method, this creates a thread called the main thread and any thread you create using java code will be&amp;nbsp;derived from the main thread (&lt;span style="color: #38761d;"&gt;out-of-focus: it is always a good&amp;nbsp;practice to give your threads a name, it is very&amp;nbsp;useful in debugging, however, it is not&amp;nbsp;required here&lt;/span&gt;) the same exact&amp;nbsp;behavior&amp;nbsp;occurs on the Linux level, the main thread for java means a process for the OS, and every thread you create using java the OS will create a Light-weight-process or &lt;b&gt;&lt;a href="http://en.wikipedia.org/wiki/Light-weight_process" target="_blank"&gt;LWP&lt;/a&gt;&lt;/b&gt;.&lt;/div&gt;
&lt;div&gt;
to cut it short, Java main thread = Linux process and Java thread&amp;nbsp;= Linux LWP.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
the idea here:&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Ask Linux which LWP is eating the CPU.&lt;/li&gt;
&lt;li&gt;Ask Java for a Thread Dump.&lt;/li&gt;
&lt;li&gt;Map this LWP to a Java thread.&lt;/li&gt;
&lt;li&gt;Get the part of code causing the issue.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
2- A Step-By-Step:&lt;/div&gt;
&lt;div&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;Get the PID: the very first step is to know what is the Java process ID, we will use Linux tools&amp;nbsp;&lt;span style="color: #38761d;"&gt;&lt;a href="http://linux.about.com/od/commands/l/blcmdl1_ps.htm" target="_blank"&gt;ps&lt;/a&gt; and &lt;a href="http://linux.about.com/od/commands/l/blcmdl1_grep.htm" target="_blank"&gt;grep&lt;/a&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;code style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt; ps -A|grep java&lt;/code&gt;&lt;br /&gt; if you are running&amp;nbsp;multiple java processes we can execute
&lt;br /&gt;
&lt;code style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt; ps -ef|grep java&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;the next step is to get CPU usage per each LWP related to the main process, again we will use&amp;nbsp;&lt;span style="color: #38761d;"&gt;
&lt;a href="http://linux.about.com/od/commands/l/blcmdl1_ps.htm" target="_blank"&gt;ps&lt;/a&gt;&amp;nbsp;&amp;nbsp;and&amp;nbsp;&lt;/span&gt;
&lt;a href="http://linux.about.com/od/commands/l/blcmdl1_grep.htm" target="_blank"&gt;grep&lt;/a&gt;&lt;br /&gt;&lt;code style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;ps -eLo
pid,lwp,nlwp,ruser,pcpu,stime,etime,args|grep {pid} &amp;gt; lwpthread.txt&lt;/code&gt;&lt;br /&gt;the file &lt;b&gt;lwpthread.txt&lt;/b&gt; will contain some thing similar:&lt;br /&gt;
&lt;table style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;th style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;PID&lt;/th&gt;
&lt;th style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;NLWP&lt;/th&gt;
&lt;th style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;SZ&lt;/th&gt;
&lt;th style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;RUSER&lt;/th&gt;
&lt;th style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;%CPU&lt;/th&gt;
&lt;th style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;STIME&lt;/th&gt;
&lt;th style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;ELAPSED&lt;/th&gt;
&lt;th style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;COMMAND&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;8234&lt;/td&gt;
&lt;td style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;8234&lt;/td&gt;
&lt;td style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;1110&lt;/td&gt;
&lt;td style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;admin&lt;/td&gt;
&lt;td style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;0.3&lt;/td&gt;
&lt;td style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;08:11&lt;/td&gt;
&lt;td style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;30:15&lt;/td&gt;
&lt;td style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;/usr/java/jdk1.6.0_24/bin/java&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;8234&lt;/td&gt;
&lt;td style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;8245&lt;/td&gt;
&lt;td style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;1110&amp;nbsp;&lt;/td&gt;
&lt;td style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;admin&lt;/td&gt;
&lt;td style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;99.0&lt;/td&gt;
&lt;td style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;08:45&lt;/td&gt;
&lt;td style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;10:15&lt;/td&gt;
&lt;td style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt;/usr/java/jdk1.6.0_24/bin/java&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
as you can see, we have an LWP(mapped to a java thread) eating the CPU, get the NLWP, that will be our lead in the next step.
&lt;br /&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;the next step is to generate a java thread dump, there are two main ways to create a thread dump, the first one is to use the JDK tool &lt;a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/share/jstack.html" target="_blank"&gt;jstack&lt;/a&gt; and pass the PID to it, the second way is to send a &lt;b&gt;kill &lt;/b&gt;signal to the JVM, however in the first way you will have control over where you want to save the thread dump, while on the second way you will have the thread dump written on the java process standard output stream.&lt;br /&gt;&amp;nbsp;as said, we are using tomcat, so the thread dump will be in &lt;b&gt;catalina.out&lt;/b&gt;
&lt;br /&gt;
&lt;code style="border-color: #C4C4C4; border-style: solid; border-width: 1px;"&gt; kill -3 {pid}&lt;/code&gt;
&lt;br /&gt;
the thread dump will be printed to the file with full stack trace. the file will contain the thread you are after, but first convert your &lt;b&gt;LWP &lt;/b&gt;id from &lt;b&gt;DEC &lt;/b&gt;to &lt;b&gt;HEX &lt;/b&gt;so &lt;b&gt;8245&amp;nbsp;&lt;/b&gt;would be&amp;nbsp;&lt;b&gt;2035&lt;/b&gt;, now open the thread dump with text editor and search for&amp;nbsp;&lt;b&gt;2035&lt;/b&gt;, you will find something similar:&lt;br /&gt;
&lt;code&gt;"TP-Processor234786" daemon prio=10 tid=0x00002aaad8024800 &lt;span style="background-color: yellow;"&gt;nid=0x2035&lt;/span&gt; runnable [0x00002aaadef29000]
   &lt;br /&gt;java.lang.Thread.State: RUNNABLE&lt;br /&gt;
 at &lt;span style="background-color: yellow;"&gt;java.util.HashMap.get(HashMap.java:303)&lt;/span&gt;&lt;br /&gt;
        at ......
&lt;/code&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;/ul&gt;
and you are done!
&lt;/div&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/JavaObsession/~4/ss8K-4DYWks" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javadrama.blogspot.com/feeds/3231410167019254298/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=9029038390867514964&amp;postID=3231410167019254298" title="15 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/9029038390867514964/posts/default/3231410167019254298?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/9029038390867514964/posts/default/3231410167019254298?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/JavaObsession/~3/ss8K-4DYWks/why-is-java-eating-my-cpu.html" title="Figure out why is JAVA eating CPU?" /><author><name>moutaz salem</name><uri>https://plus.google.com/106063653009079870460</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh5.googleusercontent.com/-VvhNV9YhJS8/AAAAAAAAAAI/AAAAAAAAAPI/hBEdI1Nhcis/s512-c/photo.jpg" /></author><thr:total>15</thr:total><feedburner:origLink>http://javadrama.blogspot.com/2012/02/why-is-java-eating-my-cpu.html</feedburner:origLink></entry></feed>
