<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' 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'><id>tag:blogger.com,1999:blog-184335780399009477</id><updated>2025-07-03T20:53:32.357-04:00</updated><category term="Servlets"/><category term="Hello World Example"/><category term="Struts 2"/><category term="JSP"/><category term="Struts"/><category term="Servlet API"/><category term="Hibernate"/><category term="JSTL"/><category term="Spring Framework 2.0"/><category term="JSP Standard Tag Library"/><category term="Servlet API Listeners"/><category term="Design Patterns"/><category term="EJB3"/><category term="JDBC"/><category term="Collections Framework"/><category term="Javascript"/><category term="Ajax"/><category term="Apache Axis"/><category term="Cookies"/><category term="Inheritance Mapping"/><category term="JSF"/><category term="Java Server Faces"/><category term="Struts 1.x"/><category term="Apache Axis2"/><category term="Arrays"/><category term="Exception Handling"/><category term="Inner Classes"/><category term="JAXP"/><category term="JMS"/><category term="Java Message Service"/><category term="Session Beans"/><category term="Threads"/><category term="Web Services"/><category term="Access Modifiers"/><category term="DOM Manipulation"/><category term="DOM Parser"/><category term="EL"/><category term="Expression Language"/><category term="Java Threads"/><category term="Struts Validator"/><category term="Validation"/><category term="XML"/><category term="static keyword"/><category term="Attributes"/><category term="Connection Pooling"/><category term="Constructors"/><category term="Custom Interceptor"/><category term="DispatchAction"/><category term="Entity Beans"/><category term="Enumerated data types"/><category term="Error Handling"/><category term="Filter"/><category term="Garbage Collection"/><category term="Generics"/><category term="Generics with Wildcards"/><category term="HashMap"/><category term="Interface"/><category term="Islands of Isolation"/><category term="JDOM"/><category term="JSON"/><category term="JSP Custom Tags"/><category term="List"/><category term="MDB"/><category term="Map"/><category term="Message Driven Bean"/><category term="Multi-dimensional Arrays"/><category term="Object Reference Variable"/><category term="Overloading"/><category term="Overriding"/><category term="Polymorphism"/><category term="Queue"/><category term="Redirection"/><category term="Response headers"/><category term="SAX Parser"/><category term="Serialization"/><category term="ServletContext"/><category term="Session Management"/><category term="Spring AOP"/><category term="Synchronization"/><category term="Transaction Management"/><category term="TreeSet"/><category term="Unicode"/><category term="Upload"/><category term="Validation Framework"/><category term="Validator Framework"/><category term="Var-Args"/><category term="Variable Argument Parameter"/><category term="Variable Scope"/><category term="Variables"/><category term="WSDL2Java"/><category term="XML Schema"/><category term="XSD"/><category term="abstract keyword"/><category term="addHeader"/><category term="char"/><category term="custom sort order"/><category term="enum in Java"/><category term="final keyword"/><category term="finalize method"/><category term="forward"/><category term="getOutputStream"/><category term="include"/><category term="join method"/><category term="org.json"/><category term="sendRedirect"/><category term="setHeader"/><category term="struts.xml"/><category term="super keyword"/><category term="this keyword"/><title type='text'>Java Code Issues</title><subtitle type='html'>Code Examples and Resolutions to Issues with Java, J2EE, JEE, Servlets, Java Server Pages (JSP), Struts Framework 1.x, Struts Framework 2, Spring Framework, Hibernate Framework, JBoss, Tomcat, Java Server Faces (JSF) and other assorted tools and technologies</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>188</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-8509036548897553798</id><published>2012-01-15T15:56:00.000-05:00</published><updated>2012-01-15T15:56:05.076-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Apache Axis2"/><category scheme="http://www.blogger.com/atom/ns#" term="Hello World Example"/><category scheme="http://www.blogger.com/atom/ns#" term="Web Services"/><title type='text'>Deploying an Apache Axis2 Web Service - Hello World Example</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;1) Setup Apache Axis2 in the web application - follow the &lt;a href=&quot;http://www.javaissues.com/2012/01/setting-up-apache-axis2-in-web.html&quot; target=&quot;_blank&quot;&gt;steps here&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
2) Create a POJO - HelloWorldService.java - in the src folder of the web application&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;package info.icontraining.ws.axis2;

public class HelloWorld {
   public String sayHello(String name) {
      return &quot;Hello &quot; + name;
   }
}&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
3) In the WebContent/WEB-INF/services folder, create a new folder and name it &#39;HelloWorld&#39;.&lt;br /&gt;
In this folder create a sub-folder and name it &#39;META-INF&#39;.&lt;br /&gt;
In the META-INF folder create a new XML file - name it &#39;services.xml&#39;.&lt;br /&gt;
&lt;br /&gt;
4) Add the following configuration to the services.xml file created in the step above,&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;service&amp;gt;
   &amp;lt;parameter name=&quot;ServiceClass&quot; locked=&quot;false&quot;&amp;gt;info.icontraining.ws.axis2.HelloWorld&amp;lt;/parameter&amp;gt;
   &amp;lt;operation name=&quot;sayHello&quot;&amp;gt;
      &amp;lt;messageReceiver class=&quot;org.apache.axis2.rpc.receivers.RPCMessageReceiver&quot;/&amp;gt;
   &amp;lt;/operation&amp;gt; 
&amp;lt;/service&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
5) Now go to the build/classes folder of the web application. Copy the folder named &#39;info&#39; and paste it into the services/HelloWorld folder created in step 3.&lt;br /&gt;
&lt;br /&gt;
So, now in the services/HelloWorld folder, there are 2 folders - META-INF and info&lt;br /&gt;
The META-INF folder contains the services.xml file.&lt;br /&gt;
The info folder contains the following hierarchy of sub-folders - icontraining/ws/axis2/HelloWorldService.class&lt;br /&gt;
&lt;br /&gt;
6) Deploy the Web Application to the server, and access the following URL in the browser,&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;http://localhost:8080/WebAppName/services/listServices&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
The HelloWorld service will be present in the list of services.&lt;br /&gt;
&lt;br /&gt;
7) Invoke the Web Service with the following URL in the browser,&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;http://localhost:8080/WebAppName/services/HelloWorld/sayHello?name=dinesh&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/8509036548897553798/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2012/01/deploying-apache-axis2-web-service.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/8509036548897553798'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/8509036548897553798'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2012/01/deploying-apache-axis2-web-service.html' title='Deploying an Apache Axis2 Web Service - Hello World Example'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-6607625266638370099</id><published>2012-01-15T14:52:00.003-05:00</published><updated>2012-01-15T14:56:56.596-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Apache Axis2"/><category scheme="http://www.blogger.com/atom/ns#" term="Web Services"/><title type='text'>Setting up Apache Axis2 in a Web Application</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;1) Download the required resources for the Apache Axis2 installation from the link here. Unzip the contents of the downloaded zip file.&lt;br /&gt;
&lt;br /&gt;
2) Copy all the jars in the lib folder in the zip file into the WebContent/WEB-INF/lib folder of the web application.&lt;br /&gt;
&lt;br /&gt;
3) Copy the folder axis2-web (and its contents) into the WebContent folder of the web application&lt;br /&gt;
&lt;br /&gt;
4) Copy the conf, modules and services folders into the WebContent/WEB-INF folder of the web application&lt;br /&gt;
&lt;br /&gt;
5) The structure of the Web Application project after the above steps will look like this,&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
WebAppName
   |
   |--- WebContent
            |
            |--- axis2-web (and all contents of this folder)
            |
            |---WEB-INF
                   |
                   |--- conf (axis2.xml file in this folder)
                   |
                   |--- lib (all jars files in this folder)
                   |
                   |--- modules (*.mar files in this folder)
                   |
                   |--- services (and its contents in this folder)&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
6) Add the following configuration to the WebContent/WEB-INF/web.xml file of the web application and within its root &amp;lt;web-app&amp;gt; element&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;servlet&amp;gt;
   &amp;lt;servlet-name&amp;gt;AxisServlet&amp;lt;/servlet-name&amp;gt;
   &amp;lt;servlet-class&amp;gt;org.apache.axis2.transport.http.AxisServlet&amp;lt;/servlet-class&amp;gt;
   &amp;lt;load-on-startup&amp;gt;1&amp;lt;/load-on-startup&amp;gt;
&amp;lt;/servlet&amp;gt;
&amp;lt;servlet&amp;gt;
   &amp;lt;servlet-name&amp;gt;AxisAdminServlet&amp;lt;/servlet-name&amp;gt;
   &amp;lt;servlet-class&amp;gt;org.apache.axis2.webapp.AxisAdminServlet&amp;lt;/servlet-class&amp;gt;
&amp;lt;/servlet&amp;gt;
    
&amp;lt;servlet-mapping&amp;gt;
   &amp;lt;servlet-name&amp;gt;AxisServlet&amp;lt;/servlet-name&amp;gt;
   &amp;lt;url-pattern&amp;gt;/servlet/AxisServlet&amp;lt;/url-pattern&amp;gt;
&amp;lt;/servlet-mapping&amp;gt;

&amp;lt;servlet-mapping&amp;gt;
   &amp;lt;servlet-name&amp;gt;AxisServlet&amp;lt;/servlet-name&amp;gt;
   &amp;lt;url-pattern&amp;gt;*.jws&amp;lt;/url-pattern&amp;gt;
&amp;lt;/servlet-mapping&amp;gt;

&amp;lt;servlet-mapping&amp;gt;
   &amp;lt;servlet-name&amp;gt;AxisServlet&amp;lt;/servlet-name&amp;gt;
   &amp;lt;url-pattern&amp;gt;/services/*&amp;lt;/url-pattern&amp;gt;
&amp;lt;/servlet-mapping&amp;gt;

&amp;lt;servlet-mapping&amp;gt;
   &amp;lt;servlet-name&amp;gt;AxisAdminServlet&amp;lt;/servlet-name&amp;gt;
   &amp;lt;url-pattern&amp;gt;/axis2-admin/*&amp;lt;/url-pattern&amp;gt;
&amp;lt;/servlet-mapping&amp;gt;

&amp;lt;mime-mapping&amp;gt;
   &amp;lt;extension&amp;gt;inc&amp;lt;/extension&amp;gt;
   &amp;lt;mime-type&amp;gt;text/plain&amp;lt;/mime-type&amp;gt;
&amp;lt;/mime-mapping&amp;gt;

&amp;lt;welcome-file-list&amp;gt;
   &amp;lt;welcome-file&amp;gt;index.jsp&amp;lt;/welcome-file&amp;gt;
   &amp;lt;welcome-file&amp;gt;index.html&amp;lt;/welcome-file&amp;gt;
   &amp;lt;welcome-file&amp;gt;/axis2-web/index.jsp&amp;lt;/welcome-file&amp;gt;
&amp;lt;/welcome-file-list&amp;gt;

&amp;lt;error-page&amp;gt;
   &amp;lt;error-code&amp;gt;404&amp;lt;/error-code&amp;gt;
   &amp;lt;location&amp;gt;/axis2-web/Error/error404.jsp&amp;lt;/location&amp;gt;
&amp;lt;/error-page&amp;gt;

&amp;lt;error-page&amp;gt;
   &amp;lt;error-code&amp;gt;500&amp;lt;/error-code&amp;gt;
   &amp;lt;location&amp;gt;/axis2-web/Error/error500.jsp&amp;lt;/location&amp;gt;
&amp;lt;/error-page&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
7) Deploy the Web Application to the server. Test the installation was successful by accessing the following URL through the browser,&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;http://localhost:8080/WebAppName/axis2-web/index.jsp&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
8) Click on the links - Services, Validate and Administration - and check each of the links work.&lt;br /&gt;
&lt;br /&gt;
Note: Use admin/axis2 as username/password to log in as Administrator&lt;br /&gt;
&lt;br /&gt;
Note: If Validate link displays an error message for the Version service, resolve the error from &lt;a href=&quot;http://www.javaissues.com/2012/01/getversion-service-error-in-axis2.html&quot; target=&quot;_blank&quot;&gt;this post here&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
9) Invoke the Version Web Service with this URL in the browser:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;http://localhost:8080/WebAppName/services/Version/getVersionRequest&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/6607625266638370099/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2012/01/setting-up-apache-axis2-in-web.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/6607625266638370099'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/6607625266638370099'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2012/01/setting-up-apache-axis2-in-web.html' title='Setting up Apache Axis2 in a Web Application'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-5104742749569519081</id><published>2012-01-15T14:23:00.001-05:00</published><updated>2012-01-15T14:25:09.378-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Apache Axis2"/><category scheme="http://www.blogger.com/atom/ns#" term="Web Services"/><title type='text'>getVersion Service method error in Apache Axis2 - There was a problem in Axis2 version service , may be the service not available or some thing has gone wrong.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;b&gt;&lt;u&gt;Topic:&lt;/u&gt;&lt;/b&gt;&amp;nbsp;Apache Axis2&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Application Server:&lt;/u&gt;&lt;/b&gt; JBoss 4.2.2GA&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Exception:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Clicking on the Validate link during Apache Axis2 installation shows the following error message:&lt;br /&gt;
&lt;br /&gt;
There was a problem in Axis2 version service , may be the service not available or some thing has gone wrong. But this does not mean system is not working ! Try to upload some other service and check to see whether it is working.&lt;br /&gt;
&lt;br /&gt;
and shows the following stack trace on the server console log:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;14:20:11,065 INFO  [STDOUT] 14:20:11,065 ERROR [RPCMessageReceiver] Exception occurred while trying to invoke service method getVersion
org.apache.axis2.AxisFault: namespace mismatch require http://axisversion.sample found http://axisversion.sample/xsd
 at org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:190)
 at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:117)
 at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40)
 at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:110)
 at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:181)
 at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:172)
 at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:146)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
 at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
 at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
 at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
 at java.lang.Thread.run(Unknown Source)&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Resolution:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Open the axis2-web/HappyAxis.jsp and find the following line of code:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;OMNamespace omNs = fac.createOMNamespace(&quot;http://axisversion.sample/xsd&quot;, &quot;ns1&quot;);&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
Modify the above line to the following:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;OMNamespace omNs = fac.createOMNamespace(&quot;http://axisversion.sample&quot;, &quot;ns1&quot;);&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/5104742749569519081/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2012/01/getversion-service-error-in-axis2.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/5104742749569519081'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/5104742749569519081'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2012/01/getversion-service-error-in-axis2.html' title='getVersion Service method error in Apache Axis2 - There was a problem in Axis2 version service , may be the service not available or some thing has gone wrong.'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-8360378913081678196</id><published>2012-01-11T21:39:00.000-05:00</published><updated>2012-01-11T21:39:29.383-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Connection Pooling"/><category scheme="http://www.blogger.com/atom/ns#" term="JDBC"/><title type='text'>Database Connection Pool Code Example with Apache DBCP and Oracle 10g</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;1) Download the Apache Commons DBCP and Commons Pool jars from &lt;a href=&quot;http://184.168.209.81/book/commons_dbcp_pool_jars.zip&quot;&gt;the link here&lt;/a&gt; and add them to the classpath / build path of the Java Application / Project.&lt;br /&gt;
&lt;br /&gt;
2) Add the jboss-j2ee.jar file to the class path/build path of the Java Application/Project. The jboss-j2ee.jar file is present in the $JBOSS_HOME/server/default/lib folder of the JBoss Installation folder.&lt;br /&gt;
&lt;br /&gt;
3) Complete the JDBC example at &lt;a href=&quot;http://www.javaissues.com/2011/04/jdbc-code-example.html&quot; target=&quot;_blank&quot;&gt;the link here&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
4) Create a java class - ConnectionPoolExample.java - in the src folder of the Java Application / Project&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
package info.icontraining.jdbc;

import java.sql.*;
import javax.sql.*;
import org.apache.commons.pool.*;
import org.apache.commons.pool.impl.*;
import org.apache.commons.dbcp.*;

public class ConnectionPoolExample {
 
   private static GenericObjectPool genericPool = new GenericObjectPool();

   public static void main(String[] args) throws Exception {

      try {
         Class.forName(&quot;oracle.jdbc.OracleDriver&quot;);
      } catch (ClassNotFoundException e) {
         e.printStackTrace();
      }

      DataSource dataSource = setupDataSource(&quot;jdbc:oracle:thin:system/system@localhost:1521:xe&quot;);

      Connection conn = null;
      Statement stmt = null;
      ResultSet rs = null;

      try {
         conn = dataSource.getConnection();
         stmt = conn.createStatement();
         rs = stmt.executeQuery(&quot;SELECT * FROM DUAL&quot;);
            
         while(rs.next()) {
            for(int i=1; i&amp;lt;=rs.getMetaData().getColumnCount(); i++) {
               System.out.println(rs.getString(i));
            }
         }
            
         System.out.println(&quot;Active Connections: &quot; + genericPool.getNumActive() + &quot;, Idle Connections: &quot; + genericPool.getNumIdle());
            
      } catch(SQLException e) {
         e.printStackTrace();
      } finally {
         try { if (rs != null) rs.close(); } catch(Exception e) { }
         try { if (stmt != null) stmt.close(); } catch(Exception e) { }
         try { if (conn != null) conn.close(); } catch(Exception e) { }
      }
        
      System.out.println(&quot;Active Connections: &quot; + genericPool.getNumActive() + &quot;, Idle Connections: &quot; + genericPool.getNumIdle());
   }

   public static DataSource setupDataSource(String connectURI) throws Exception {
      ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,null);
      KeyedObjectPoolFactory kopf =new GenericKeyedObjectPoolFactory(null);

      PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory( connectionFactory, genericPool, kopf, null, false, true);

      for(int i = 0; i &amp;lt; 5; i++) {
         genericPool.addObject();
      }

      System.out.println(&quot;Active Connections: &quot; + genericPool.getNumActive() + &quot;, Idle Connections: &quot; + genericPool.getNumIdle());
        
      PoolingDataSource dataSource = new PoolingDataSource(genericPool);
      return dataSource;
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
5) Run the code as a standalone Java Application&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/8360378913081678196/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2012/01/database-connection-pool-code-example.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/8360378913081678196'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/8360378913081678196'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2012/01/database-connection-pool-code-example.html' title='Database Connection Pool Code Example with Apache DBCP and Oracle 10g'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-4551047943618282633</id><published>2012-01-01T13:20:00.001-05:00</published><updated>2012-01-01T13:21:35.998-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="JDBC"/><title type='text'>JDBC Code to read BLOB data type from database table</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;0) Complete the example to store an image (BLOB data) to a table at the &lt;a href=&quot;http://www.javaissues.com/2012/01/jdbc-code-to-insert-image-in-blob.html&quot; target=&quot;_blank&quot;&gt;link here&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
1) Create a Java class - JdbcBlobReader.java - in the src folder of the Java Project / application&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
package info.icontraining.jdbc;

import java.io.*;
import java.sql.*;

public class JdbcBlobReader {
 
   public static void main(String[] args) throws Exception, IOException, SQLException {
   
      Class.forName(&quot;oracle.jdbc.OracleDriver&quot;);
      Connection conn = DriverManager.getConnection(&quot;jdbc:oracle:thin:@localhost:1521:xe&quot;,&quot;system&quot;,&quot;system&quot;);

      PreparedStatement pstmt = conn.prepareStatement(&quot;Select name, image from MyImages&quot;);
      ResultSet rs = pstmt.executeQuery();
      
      while (rs.next()) {
         String name = rs.getString(1);
         String description = rs.getString(2);
         File image = new File(&quot;src/icon-training-downloaded.jpg&quot;);
         FileOutputStream fos = new FileOutputStream(image);

         byte[] buffer = new byte[1];
         InputStream is = rs.getBinaryStream(2);

         while (is.read(buffer) &amp;gt; 0) {
            fos.write(buffer);
         }

         fos.close();
      }

      conn.close();    
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
2) Run the code as a standalone Java Application&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/4551047943618282633/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2012/01/jdbc-code-to-read-blob-data-type-from.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/4551047943618282633'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/4551047943618282633'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2012/01/jdbc-code-to-read-blob-data-type-from.html' title='JDBC Code to read BLOB data type from database table'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-3195924958703720599</id><published>2012-01-01T08:41:00.000-05:00</published><updated>2012-01-01T08:41:27.074-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="JDBC"/><title type='text'>JDBC Code to insert an image in a BLOB column</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;0) Complete the first 5 steps in the JDBC code example at the &lt;a href=&quot;http://www.javaissues.com/2011/04/jdbc-code-example.html&quot; target=&quot;_blank&quot;&gt;link here&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
1) Create a new table in the database using the following CREATE TABLE command&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
create table MyImages (
   name VARCHAR(1000),
   image BLOB
);
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
2) Add the image file to the src folder of the Java Project/Application&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTOeYyvjgoNxJC0U90Vm1C4pNKVrezgzBt1Qpwdyiep7MrOLmNIy7BoDlXAuPDJK-HckmVmmS1l7NOAcTnH3mgOLivPV_eyNj8FdnchB7cch-BdC7Ny1ZalJrIgRTA5K4yigQsVowpMqc/s1600/icon-training.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;62&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTOeYyvjgoNxJC0U90Vm1C4pNKVrezgzBt1Qpwdyiep7MrOLmNIy7BoDlXAuPDJK-HckmVmmS1l7NOAcTnH3mgOLivPV_eyNj8FdnchB7cch-BdC7Ny1ZalJrIgRTA5K4yigQsVowpMqc/s320/icon-training.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
3) Create a Java class - JdbcBlobExample.java - to the src folder of the Java Project/Application&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
package info.icontraining.jdbc;

import java.io.*;
import java.sql.*;

public class JdbcBlobExample {
 
   public static void main(String[] args) throws Exception, IOException, SQLException {
   
      Class.forName(&quot;oracle.jdbc.OracleDriver&quot;);
      Connection conn = DriverManager.getConnection(&quot;jdbc:oracle:thin:@localhost:1521:xe&quot;,&quot;system&quot;,&quot;system&quot;);

      FileInputStream fis = null;
      PreparedStatement pstmt = null;
    
      try {
         conn.setAutoCommit(false);
      
         File file = new File(&quot;src/icon-training.jpg&quot;);
         fis = new FileInputStream(file);
      
         pstmt = conn.prepareStatement(&quot;insert into MyImages(name, image) values (?, ?)&quot;);
      
         pstmt.setString(1, &quot;Icon Training Logo&quot;);
         pstmt.setBinaryStream(2, fis, (int) file.length());
      
         pstmt.executeUpdate();
         conn.commit();
      
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         pstmt.close();
         fis.close();
      }
   }
}&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
4) Run the code as a standalone Java Application&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/3195924958703720599/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2012/01/jdbc-code-to-insert-image-in-blob.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/3195924958703720599'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/3195924958703720599'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2012/01/jdbc-code-to-insert-image-in-blob.html' title='JDBC Code to insert an image in a BLOB column'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTOeYyvjgoNxJC0U90Vm1C4pNKVrezgzBt1Qpwdyiep7MrOLmNIy7BoDlXAuPDJK-HckmVmmS1l7NOAcTnH3mgOLivPV_eyNj8FdnchB7cch-BdC7Ny1ZalJrIgRTA5K4yigQsVowpMqc/s72-c/icon-training.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-6913883801373140821</id><published>2012-01-01T08:15:00.001-05:00</published><updated>2012-01-01T08:16:48.969-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="JDBC"/><category scheme="http://www.blogger.com/atom/ns#" term="Transaction Management"/><title type='text'>JDBC Transaction Management Code Example</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;0) Complete the first 6 steps in the JDBC Code example at the &lt;a href=&quot;http://www.javaissues.com/2011/04/jdbc-code-example.html&quot; target=&quot;_blank&quot;&gt;link here&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
1) Create a Java class - JdbcTransactionMgmt.java - in the src folder of the Java Project /Application&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
package info.icontraining.jdbc;

import java.sql.*;

public class JdbcTransactionMgmt {

   public static void main(String[] args) throws SQLException, ClassNotFoundException {

      Class.forName(&quot;oracle.jdbc.OracleDriver&quot;);
      Connection conn = DriverManager.getConnection(&quot;jdbc:oracle:thin:@localhost:1521:xe&quot;,&quot;system&quot;,&quot;system&quot;);
      PreparedStatement pstmt = null;
  
      conn.setAutoCommit(false); // start of transaction
  
      try {
         pstmt = conn.prepareStatement(&quot;insert into jdbcdemo values (?, ?, ?, ?)&quot;);

         pstmt.setString(1, &quot;Ramesh&quot;);
         pstmt.setString(2, &quot;PC&quot;);
         pstmt.setInt(3, 32);

         Date d = new Date(0);
         pstmt.setDate(4, d.valueOf(&quot;1978-9-28&quot;));

         int rowsAffected = pstmt.executeUpdate();
         System.out.println(rowsAffected + &quot; rows affected.&quot;);

         // deliberately cause an exception to be thrown
         int i = 3/0;    // comment this line for commit to happen 

         pstmt.setString(1, &quot;Gunjan&quot;);
         pstmt.setString(2, &quot;C&quot;);
         pstmt.setInt(3, 26);
         pstmt.setDate(4, d.valueOf(&quot;1984-10-30&quot;));

         rowsAffected = pstmt.executeUpdate();
         System.out.println(rowsAffected + &quot; rows affected.&quot;);

         conn.commit();

         pstmt.close();

      } catch (Exception e) {
         conn.rollback();
         System.err.println(&quot;Caught Exception &amp;amp; transaction rollbacked&quot;);
      }

      conn.close(); 
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
2) Run the code as a standalone Java application.&lt;br /&gt;
Since the code throws an exception, which is caught, and the transaction is rolled-back, no changes reflect in the database table, even though one row was, apparently, successfully inserted.&lt;br /&gt;
&lt;br /&gt;
3) Comment the line that deliberately throws an exception and run the code as a standalone Java application.&lt;br /&gt;
Now, both rows are inserted because the transaction gets committed successfully.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/6913883801373140821/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2012/01/jdbc-transaction-management-code.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/6913883801373140821'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/6913883801373140821'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2012/01/jdbc-transaction-management-code.html' title='JDBC Transaction Management Code Example'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-4778894624080315244</id><published>2012-01-01T07:58:00.000-05:00</published><updated>2012-01-01T07:58:26.556-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="JDBC"/><title type='text'>JDBC Prepared Statement Code Example</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;0) Complete the first 6 steps in the JDBC Code Example at the &lt;a href=&quot;http://www.javaissues.com/2011/04/jdbc-code-example.html&quot; target=&quot;_blank&quot;&gt;link here&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
1) Create a java class - JdbcPSExample.java - to the src folder of the Java Project / Application&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
package info.icontraining.jdbc;

import java.sql.*;

public class JdbcPSExample {

   public static void main(String[] args) throws SQLException, ClassNotFoundException {
  
      Class.forName(&quot;oracle.jdbc.OracleDriver&quot;);
      Connection conn = DriverManager.getConnection(&quot;jdbc:oracle:thin:@localhost:1521:xe&quot;,&quot;system&quot;,&quot;system&quot;);
      PreparedStatement pstmt = conn.prepareStatement(&quot;insert into jdbcdemo values (?, ?, ?, ?)&quot;);
  
      pstmt.setString(1, &quot;Dinesh&quot;);
      pstmt.setString(2, &quot;PC&quot;);
      pstmt.setInt(3, 32);
  
      Date d = new Date(0);
      pstmt.setDate(4, d.valueOf(&quot;1978-9-28&quot;));

      int rowsAffected = pstmt.executeUpdate();
      System.out.println(rowsAffected + &quot; rows affected.&quot;);

      pstmt.setString(1, &quot;Tanvi&quot;);
      pstmt.setString(2, &quot;C&quot;);
      pstmt.setInt(3, 26);
      pstmt.setDate(4, d.valueOf(&quot;1984-10-30&quot;));
  
      rowsAffected = pstmt.executeUpdate();
      System.out.println(rowsAffected + &quot; rows affected.&quot;);

      pstmt.close();
      conn.close(); 
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
3) Run the Example as a standalone Java Application&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/4778894624080315244/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2012/01/jdbc-prepared-statement-code-example.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/4778894624080315244'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/4778894624080315244'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2012/01/jdbc-prepared-statement-code-example.html' title='JDBC Prepared Statement Code Example'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-2070249270406664659</id><published>2011-12-31T11:38:00.001-05:00</published><updated>2011-12-31T11:39:39.404-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Hello World Example"/><category scheme="http://www.blogger.com/atom/ns#" term="Hibernate"/><category scheme="http://www.blogger.com/atom/ns#" term="Spring Framework 2.0"/><title type='text'>Spring - Hibernate framework Integration - Hello World Example</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;0) To proceed with this example, complete the &lt;a href=&quot;http://www.javaissues.com/2011/03/spring-framework-20-hello-world-example.html&quot; target=&quot;_blank&quot;&gt;Spring Hello World Example&lt;/a&gt; and the &lt;a href=&quot;http://www.javaissues.com/2011/04/hibernate-30-hello-world-example.html&quot; target=&quot;_blank&quot;&gt;Hibernate Hello World Example&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
1) Put 2 jar files from the Apache Commons collection in the WebContent/WEB-INF/lib folder of the web application - download the jars &lt;a href=&quot;http://184.168.209.81/book/spring-hibernate-integration-jars.zip&quot; target=&quot;_blank&quot;&gt;from this link&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
2) Add the following elements to the Spring configuration file, applicationContext.xml&lt;br /&gt;
Integrating Spring with Hibernate results in the hibernate.cfg.xml becoming obsolete - the configuration in hibernate.cfg.xml is now put in the applicationContext.xml&lt;br /&gt;
&lt;br /&gt;
Configuration for DataSource (Database Connection properties)&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
&amp;lt;bean id=&quot;dataSource&quot; destroy-method=&quot;close&quot; class=&quot;org.apache.commons.dbcp.BasicDataSource&quot;&amp;gt;
   &amp;lt;property name=&quot;driverClassName&quot; value=&quot;oracle.jdbc.OracleDriver&quot; /&amp;gt;
   &amp;lt;property name=&quot;url&quot; value=&quot;jdbc:oracle:thin:@localhost:1521:xe&quot; /&amp;gt;
   &amp;lt;property name=&quot;username&quot; value=&quot;system&quot; /&amp;gt;
   &amp;lt;property name=&quot;password&quot; value=&quot;system&quot; /&amp;gt;
&amp;lt;/bean&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
Configuration for Hibernate (of hibernate.cfg.xml)&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
&amp;lt;bean id=&quot;sessionFactory&quot; class=&quot;org.springframework.orm.hibernate3.LocalSessionFactoryBean&quot;&amp;gt;
   &amp;lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;/&amp;gt;
   &amp;lt;property name=&quot;mappingResources&quot;&amp;gt;
      &amp;lt;list&amp;gt;
         &amp;lt;value&amp;gt;info/icontraining/hibernate/Message.hbm.xml&amp;lt;/value&amp;gt;
      &amp;lt;/list&amp;gt;
   &amp;lt;/property&amp;gt;
   &amp;lt;property name=&quot;hibernateProperties&quot;&amp;gt;
      &amp;lt;props&amp;gt;
         &amp;lt;prop key=&quot;hibernate.dialect&quot;&amp;gt;org.hibernate.dialect.Oracle9Dialect&amp;lt;/prop&amp;gt;
      &amp;lt;/props&amp;gt;
   &amp;lt;/property&amp;gt;
&amp;lt;/bean&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
Configuration for HibernateTemplate, DAOs, etc.&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
&amp;lt;bean id=&quot;hibernateTemplate&quot; class=&quot;org.springframework.orm.hibernate3.HibernateTemplate&quot;&amp;gt;
   &amp;lt;property name=&quot;sessionFactory&quot; ref=&quot;sessionFactory&quot; /&amp;gt;
&amp;lt;/bean&amp;gt;
 
&amp;lt;bean id=&quot;myDao&quot; class=&quot;info.icontraining.dao.MyDAO&quot;&amp;gt;
   &amp;lt;property name=&quot;hibernateTemplate&quot; ref=&quot;hibernateTemplate&quot; /&amp;gt;
&amp;lt;/bean&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
3) Create a MyDAO.java class in the src folder of the web application - this class does the actual persistence&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
package info.icontraining.dao;

import java.util.List;
import info.icontraining.hibernate.Message;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class MyDAO {

   private HibernateTemplate hibernateTemplate;
 
   public void setHibernateTemplate(HibernateTemplate template) {
      this.hibernateTemplate = template;
   }
 
   public void saveData(String str) {
      Message message1 = new Message(str);
      hibernateTemplate.save(message1);
   }

   public int retrieveData() {
      List&amp;lt;Message&amp;gt; messages = hibernateTemplate.find(&quot;from Message as m order by m.text asc&quot;);
      return messages.size();
   } 
}
&lt;/code&gt;&lt;/pre&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
4) Create a client JSP - springHibernate.jsp - in the WebContent folder of the web application&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
&amp;lt;%@ page  import=&quot;org.springframework.context.*,org.springframework.web.context.*,info.icontraining.spring.*,info.icontraining.dao.*&quot;%&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;body&amp;gt;

&amp;lt;% 
 ApplicationContext factory = 
  (ApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

 MyDAO mydao = (MyDAO)factory.getBean(&quot;myDao&quot;);
 mydao.saveData(&quot;Spring Hibernate&quot;);
 
 int numMessages = mydao.retrieveData();
%&amp;gt;
 
&amp;lt;%= numMessages + &quot; messages found.&quot; %&amp;gt;

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
5) Test the example with the following URL in the browser,&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
http://localhost:8080/WebAppName/springHibernate.jsp
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
Log into Oracle Database and check the contents of the appropriate table&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/2070249270406664659/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2011/12/spring-hibernate-framework-integration.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/2070249270406664659'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/2070249270406664659'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2011/12/spring-hibernate-framework-integration.html' title='Spring - Hibernate framework Integration - Hello World Example'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-2982143502449774186</id><published>2011-12-31T10:25:00.001-05:00</published><updated>2011-12-31T10:28:16.040-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Spring Framework 2.0"/><title type='text'>Spring Bean Scope Demo - Prototype &amp; Singleton</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;1) Create 2 beans - PrototypeBean.java &amp;amp; SingletonBean.java - in the src folder of the web application&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
package info.icontraining.spring;

public class PrototypeBean {
   private int i;
 
   public PrototypeBean() {
      i = 3;
   }
 
   public void changeValue() {
      i = 5;
   }
 
   public int getValue() {
      return i;
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;

package info.icontraining.spring;

public class SingletonBean {

   private int i;
 
   public SingletonBean() {
      i = 3;
   }
 
   public void changeValue() {
      i = 5;
   }
 
   public int getValue() {
      return i;
   } 
}
&lt;/code&gt;&lt;/pre&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;2) Configure both beans in the WebContent/WEB-INF/applicationContext.xml file - the PrototypeBean bean is configured with the scope attribute set to &#39;prototype&#39;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
&amp;lt;bean id=&quot;singleton&quot; class=&quot;info.icontraining.spring.SingletonBean&quot; /&amp;gt;
 
&amp;lt;bean id=&quot;prototype&quot; class=&quot;info.icontraining.spring.PrototypeBean&quot; scope=&quot;prototype&quot; /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
3) Create a client JSP - singletonPrototypeDemo.jsp - in the WebContent folder of the web application&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
&amp;lt;%@ page  import=&quot;org.springframework.context.*,org.springframework.web.context.*,info.icontraining.spring.*&quot;%&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;body&amp;gt;

 &amp;lt;% 
 ApplicationContext factory = 
  (ApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

 SingletonBean s1 = (SingletonBean)factory.getBean(&quot;singleton&quot;);
 s1.changeValue();
 
 SingletonBean s2 = (SingletonBean)factory.getBean(&quot;singleton&quot;); 
 %&amp;gt;
 
 &amp;lt;%= &quot;SingletonBean on Reference-1 after changing value = &quot; + s1.getValue() %&amp;gt;&amp;lt;br/&amp;gt;
 &amp;lt;%= &quot;SingletonBean on Reference-2 without changing value = &quot; + s2.getValue() %&amp;gt;

&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;

&amp;lt;% 
 PrototypeBean p1 = (PrototypeBean)factory.getBean(&quot;prototype&quot;);
 p1.changeValue();
 
 PrototypeBean p2 = (PrototypeBean)factory.getBean(&quot;prototype&quot;); 
 %&amp;gt;

 &amp;lt;%= &quot;PrototypeBean on Reference-1 after changing value = &quot; + p1.getValue() %&amp;gt;&amp;lt;br/&amp;gt;
 &amp;lt;%= &quot;PrototypeBean on Reference-2 without changing value = &quot; + p2.getValue() %&amp;gt;

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
4) Test the example with the following URL in the browser&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;http://localhost:8080/WebAppName/singletonPrototypeDemo.jsp&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/2982143502449774186/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2011/12/spring-bean-scope-demo-prototype.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/2982143502449774186'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/2982143502449774186'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2011/12/spring-bean-scope-demo-prototype.html' title='Spring Bean Scope Demo - Prototype &amp; Singleton'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-2160171048706380727</id><published>2011-12-31T09:21:00.001-05:00</published><updated>2011-12-31T09:22:00.758-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Spring Framework 2.0"/><title type='text'>Breaking a large applicationContext.xml configuration file into multiple files</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;0) Complete the Spring Framework Hello World example at &lt;a href=&quot;http://www.javaissues.com/2011/03/spring-framework-20-hello-world-example.html&quot; target=&quot;_blank&quot;&gt;this link&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
1) Create a new configuration file - applicationContext2.xml - in the WebContent/WEB-INF folder of the web application. Make sure that the root &amp;lt;beans&amp;gt; element is the same as in the applicationContext.xml&lt;br /&gt;
&lt;br /&gt;
Move the &amp;lt;bean&amp;gt; configuration for the greetingService bean to the new configuration file and delete it from the applicationContext.xml&lt;br /&gt;
&lt;br /&gt;
2) Modify the configuration in the web.xml by adding the new configuration file name, as follows,&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
&amp;lt;context-param&amp;gt;
     &amp;lt;param-name&amp;gt;contextConfigLocation&amp;lt;/param-name&amp;gt;
     &amp;lt;param-value&amp;gt;/WEB-INF/applicationContext.xml /WEB-INF/applicationContext2.xml&amp;lt;/param-value&amp;gt;
  &amp;lt;/context-param&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
3) Test the Hello World example as in the Hello World example&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/2160171048706380727/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2011/12/breaking-large-applicationcontextxml.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/2160171048706380727'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/2160171048706380727'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2011/12/breaking-large-applicationcontextxml.html' title='Breaking a large applicationContext.xml configuration file into multiple files'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-5944585351521009675</id><published>2011-12-31T09:10:00.001-05:00</published><updated>2011-12-31T09:10:46.544-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Spring Framework 2.0"/><title type='text'>Initializing, Destroying Spring Beans &amp; instantiating a Spring Bean through a factory method</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;1) Create an interface - InitDestroyDemo.java - in the src folder of the web application&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
package info.icontraining.spring;

public interface InitDestroyDemo {
   public void businessMethod();
}
&lt;/code&gt;&lt;/pre&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
2) Create a Spring Bean that implements the interface - InitDestroyDemoImpl.java - in the src folder of the web application&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
package info.icontraining.spring;

public class InitDestroyDemoImpl implements InitDestroyDemo {

   private InitDestroyDemoImpl() { }
 
   public void myInitMethod() {
      System.out.println(&quot;Inside Init Method of Spring Bean&quot;);
   }
 
   public void businessMethod() {
      System.out.println(&quot;Inside Business Method of Spring Bean&quot;);
   }
 
   public void myDestroyMethod() {
      System.out.println(&quot;Inside Destroy Method of Spring Bean&quot;);
   }
 
   public static InitDestroyDemo getInstance() {
      return new InitDestroyDemoImpl();
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
3) Configure the Spring Bean in the WebContent/WEB-INF/applicationContext.xml file&lt;br /&gt;
&lt;br /&gt;
The bean is configured with an initializing method using the init-method attribute, with a destroying method using the destroy-method attribute.&lt;br /&gt;
&lt;br /&gt;
The constructor of the bean is made private, and instantiation is instead done through a static factory method, getInstance() - this is configured using the factory-method attribute&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
&amp;lt;bean id=&quot;initDestroyDemo&quot; class=&quot;info.icontraining.spring.InitDestroyDemoImpl&quot; init-method=&quot;myInitMethod&quot; destroy-method=&quot;myDestroyMethod&quot; factory-method=&quot;getInstance&quot; /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
4) Create a client JSP - springInitDestroy.jsp - in the WebContent folder of the web application&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
&amp;lt;%@ page  import=&quot;org.springframework.context.*,org.springframework.web.context.*,info.icontraining.spring.*&quot;%&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;body&amp;gt;

View output on Server console

 &amp;lt;% 
 ApplicationContext factory = 
  (ApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

 InitDestroyDemo test = (InitDestroyDemo)factory.getBean(&quot;initDestroyDemo&quot;);
 test.businessMethod();
 
 %&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
5) Test the example with the following URL in the browser,&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
http://localhost:8080/WebAppName/springInitDestroy.jsp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/5944585351521009675/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2011/12/initializing-destroying-spring-beans.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/5944585351521009675'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/5944585351521009675'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2011/12/initializing-destroying-spring-beans.html' title='Initializing, Destroying Spring Beans &amp; instantiating a Spring Bean through a factory method'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-8778433857119789793</id><published>2011-11-27T09:39:00.000-05:00</published><updated>2011-11-27T09:39:47.780-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Arrays"/><title type='text'>Anonymous Arrays Code Example</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Anonymous Arrays in Java are those arrays that are constructed without specifying the size nor assigned a reference. Anonymous arrays are created on-the-fly when needed and cannot be re-accessed (since there is no reference available to the array object), therefore they become eligible for garbage-collection as soon as the code that uses it completes it.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;package info.icontraining.core;

public class AnonymousArrays {

   public static void main(String[] args) {
  
      int[] arr1 = {1, 2, 3, 4, 5};
  
      for(int i: arr1) {
         System.out.print(i);
      }
  
      for(int i: new int[]{1, 2, 3, 4, 5} ) {
         System.out.print(i);
      }  
   }
}&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/8778433857119789793/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2011/11/anonymous-arrays-code-example.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/8778433857119789793'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/8778433857119789793'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2011/11/anonymous-arrays-code-example.html' title='Anonymous Arrays Code Example'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-1927292082401397095</id><published>2011-11-26T16:07:00.001-05:00</published><updated>2011-11-26T16:08:36.579-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="char"/><category scheme="http://www.blogger.com/atom/ns#" term="Unicode"/><title type='text'>Writing Unicode characters in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Create the following class in the src folder of the Java Project in Eclipse&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;

package info.icontraining.core;

public class CharExample {

   public static void main(String args[]) {
 
      char c=&#39;\u0915&#39;;
      System.out.println(c);
   } 
}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;To enabled Unicode character display in the console of Eclipse, make the following 3 configurations in Eclipse:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;1) Open the &quot;Run&quot; menu &amp;gt; Choose &quot;Open Run Dialog ...&quot; &amp;gt; Choose your application and the particular class (in this case, CharExample) &amp;gt; Click on the &quot;Common&quot; tab &amp;gt; Choose &quot;Console Encoding&quot; as &quot;Other&quot; and &quot;UTF-8&quot; from the drop-down list&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;2) Next, go on the &quot;Arguments&quot; tab &amp;gt; Copy the string &quot;-Dfile.encoding=UTF-8&quot; in the VM arguments text area (without the double quotes). Click &quot;Apply&quot; and then &quot;Close&quot;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;3) Finally, go to the &quot;Window&quot; menu &amp;gt; Choose &quot;Preferences ...&quot; &amp;gt; Next, choose &quot;General&quot; &amp;gt; Then &quot;Workspace&quot; &amp;gt; In the &quot;Text File Encoding&quot; select &quot;Other&quot; and &quot;UTF-8&quot; from the drop-down list&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/1927292082401397095/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2011/11/writing-unicode-characters-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/1927292082401397095'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/1927292082401397095'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2011/11/writing-unicode-characters-in-java.html' title='Writing Unicode characters in Java'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-5713045369008947843</id><published>2011-10-09T14:09:00.002-04:00</published><updated>2011-10-09T14:12:29.591-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Access Modifiers"/><title type='text'>Access Modifiers for Class members in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;There are 3 access modifiers for class members: public, private, protected&lt;br /&gt;
and 4 access levels: public, private, protected, default (none of the modifiers present)&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;public access level&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
A public member (variable or method) can be accessed from another class regardless of the package in which the other class is present.&lt;br /&gt;
public members can be inherited by a subclass regardless of the package in which the subclass is present.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;package info.icontraining.p1;
public class Test {
   public int i;           // public member variable
}

package info.icontraining.p2;
import info.icontraining.p1.Test;
public class A {
   public void m1() {
      Test t = new Test();
      t.i = 3;       // public members can be accessed 
                     // by code in another class that is
   }                 // present in a different package 
}

package info.icontraining.p2;

import info.icontraining.p1.Test;
public class B extends Test {
   public void m1() {
      i = 3;         // public members can be inherited
   }                 // by another class that is present
}                    // in a different package
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;private access level&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
A private member cannot be accessed by code in any class except by code in the class in which the private member is present.&lt;br /&gt;
private members cannot be inherited by any subclass.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;package info.icontraining.p1;
public class Test {
   private int i;           // private member variable
   public void m1() {
      i = 3;           // private members can be accessed
   }                   // only by code within the class
}                      // in which it is declared

package info.icontraining.p1;
public class A {
   public void method1() {
      Test t = new Test();
      t.i = 3;           // this line will not compile
   }
}

package info.icontraining.p1;
public class B extends Test {
   public void method1() {
      i = 3;            // this line will not compile
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;default access level&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
A default member can be accessed from another class if both classes are present in the same package.&lt;br /&gt;
default members can be inherited by a subclass is both classes are present in the same package.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;package info.icontraining.p1;
public class Test {
   int i;              // default member variable
}

package info.icontraining.p1;
public class A {
   public void m1() {
      Test t = new Test();
      t.i = 3;         // can be accessed by a class
   }                   // in same package
}

package info.icontraining.p1;
public class B extends Test {
   public void m2 {
      i = 3;       // can be inherited by a class
   }               // in same package
}

package info.icontraining.p2;
import info.icontraining.p1.Test;
public class C {
   public void m1() {
      Test t = new Test();
      t.i = 3;         // will not compile, cannot be 
   }                   // accessed in another package
}

package info.icontraining.p2;
import info.icontraining.p1.Test;
public class D extends Test {
   public void m2 {
      i = 3;       // will not compile, cannot be
   }               // inherited in another package
}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;protected access level&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
A protected member can be accessed by another class in the same package only.&lt;br /&gt;
However, protected members can be inherited by a subclass present in a different package also.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
package info.icontraining.p1;
public class Test {
   protected int i;              // protected member variable
}

package info.icontraining.p1;
public class A {
   public void m1() {
      Test t = new Test();
      t.i = 3;         // can be accessed by a class
   }                   // in same package
}

package info.icontraining.p1;
public class B extends Test {
   public void m2 {
      i = 3;       // can be inherited by a class
   }               // in same package
}

package info.icontraining.p2;
import info.icontraining.p1.Test;
public class C {
   public void m1() {
      Test t = new Test();
      t.i = 3;         // will not compile, cannot be
   }                   // accessed in another package
}

package info.icontraining.p2;
import info.icontraining.p1.Test;
public class D extends Test {
   public void m2 {
      i = 3;       // can be inherited by a class
   }               // in another package
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/5713045369008947843/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2011/10/access-modifiers-for-class-members-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/5713045369008947843'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/5713045369008947843'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2011/10/access-modifiers-for-class-members-in.html' title='Access Modifiers for Class members in Java'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-2479271996289313500</id><published>2011-10-08T07:13:00.001-04:00</published><updated>2011-10-08T07:14:17.561-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Javascript"/><title type='text'>Timer Interval functions in Javascript</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;b&gt;&lt;u&gt;Code Example&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;

&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;

var timerId;

var setAlerts = function() {
   timerId = setInterval(&quot;myfunction();&quot;,3000);
}

var removeAlerts = function() {
   clearInterval(timerId);
}

function myfunction() {
   alert(&quot;Hi&quot;);
}

window.onload = function() {
   document.getElementById(&quot;setButton&quot;).onclick=setAlerts;
   document.getElementById(&quot;clearButton&quot;).onclick=removeAlerts;
}

&amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;

&amp;lt;button id=&quot;setButton&quot;&amp;gt;Set Interval&amp;lt;/button&amp;gt;&amp;lt;br/&amp;gt;
&amp;lt;button id=&quot;clearButton&quot;&amp;gt;Clear Interval&amp;lt;/button&amp;gt;&amp;lt;br/&amp;gt;

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/2479271996289313500/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2011/10/timer-interval-functions-in-javascript.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/2479271996289313500'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/2479271996289313500'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2011/10/timer-interval-functions-in-javascript.html' title='Timer Interval functions in Javascript'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-6927101203911313532</id><published>2011-10-08T07:01:00.002-04:00</published><updated>2011-10-08T07:06:20.479-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Cookies"/><category scheme="http://www.blogger.com/atom/ns#" term="Javascript"/><title type='text'>Javascript to set cookies and get hold of a specific cookie</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Write Javascript code to store a cookie and retrieve it - also check if cookies are enabled on the client.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Solution&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;

var cookieFunction = function() {
   
if (navigator.cookieEnabled) {
var name = prompt(&quot;Enter cookie name&quot;);
      var value= prompt(&quot;Enter cookie value&quot;);
      if ((name) &amp;amp;&amp;amp; (value)) {
         var date = new Date(&quot;1 Jan 2015 11:30:00&quot;);
         document.cookie= name + &quot;=&quot;+value+&quot;; expires=&quot;+date.toGMTString()+&quot;;&quot;;
      }
   } else {
      alert(&quot;Cookies Not enabled&quot;);
   }
}

var getCookie = function() {
   var cookies=document.cookie;
   alert(cookies);
}

var getSpecificCookie = function() {
   var c_name = prompt(&quot;Enter cookie name&quot;);
   var all_cookies = document.cookie.split( &#39;;&#39; );
   for (i=0;i&amp;lt;all_cookies.length;i++) {
      x = all_cookies[i].substr(0, all_cookies[i].indexOf(&quot;=&quot;));
      y = all_cookies[i].substr(all_cookies[i].indexOf(&quot;=&quot;)+1);
      x = x.replace(/^\s+|\s+$/g,&quot;&quot;);
      if (x == c_name) {
        alert( unescape(y));
        return;
     }
   }

   alert(&quot;Cookie does not exist&quot;);
}

window.onload = function() {
   document.getElementById(&quot;getCookieButton&quot;).onclick=getCookie;
   document.getElementById(&quot;getSpecificCookieButton&quot;).onclick=getSpecificCookie;
   document.getElementById(&quot;diceButton&quot;).onclick=rollDice;
}

&amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
&amp;lt;button id=&quot;cookieButton&quot;&amp;gt;Set Cookie&amp;lt;/button&amp;gt;
&amp;lt;button id=&quot;getCookieButton&quot;&amp;gt;Get Cookies&amp;lt;/button&amp;gt;
&amp;lt;button id=&quot;getSpecificCookieButton&quot;&amp;gt;Get Specific Cookie&amp;lt;/button&amp;gt;

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/6927101203911313532/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2011/10/javascript-to-set-cookies-and-get-hold.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/6927101203911313532'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/6927101203911313532'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2011/10/javascript-to-set-cookies-and-get-hold.html' title='Javascript to set cookies and get hold of a specific cookie'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-2525237845727338026</id><published>2011-10-08T06:44:00.001-04:00</published><updated>2011-10-08T06:45:16.089-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="DOM Manipulation"/><category scheme="http://www.blogger.com/atom/ns#" term="Javascript"/><title type='text'>Javascript simulation of rolling of a dice</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;br /&gt;
Write Javascript code to simulate the rolling of a dice. When the user clicks a button, the result should be the simulation of a dice - a value between 1 to 6 is displayed in the String, &quot;Your roll of the dice displayed 3&quot;&lt;br /&gt;
&lt;br /&gt;
Hint: Math.random(), document.getElementById(), innerHTML, onclick event&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Solution&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;

var rollDice = function() {
   var diceElementNode = document.getElementById(&quot;diceValue&quot;);
   var diceElementNodeChildren = diceElementNode.childNodes;

   if (diceElementNodeChildren[0] != null) {
      diceElementNodeChildren[0].nodeValue = &quot;The dice value is &quot; + Math.ceil((Math.random() * 6));
   } else {
      var diceTextNode = document.createTextNode(&quot;&quot;);
      diceTextNode.nodeValue =&quot;The dice value is &quot; + Math.ceil((Math.random() * 6));
      diceElementNode.appendChild(diceTextNode);
   }
}

window.onload = function() {
   document.getElementById(&quot;diceButton&quot;).onclick=rollDice;
}

&amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
&amp;lt;button id=&quot;diceButton&quot;&amp;gt;Roll Dice&amp;lt;/button&amp;gt;
&amp;lt;div id=&quot;diceValue&quot;&amp;gt;&amp;lt;/div&amp;gt;

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/2525237845727338026/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2011/10/javascript-simulation-of-rolling-of.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/2525237845727338026'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/2525237845727338026'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2011/10/javascript-simulation-of-rolling-of.html' title='Javascript simulation of rolling of a dice'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-4258098130628713191</id><published>2011-09-30T11:37:00.001-04:00</published><updated>2011-09-30T11:40:06.039-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Design Patterns"/><category scheme="http://www.blogger.com/atom/ns#" term="EJB3"/><title type='text'>Service Locator &amp; Business Delegate Design Patterns for an EJB Client</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;b style=&quot;text-decoration: underline;&quot;&gt;HelloClient.java&lt;/b&gt; - This is the EJB Client class&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
package info.icontraining.ejb.client;

public class HelloClient {
 
   public static void main(String[] args) {  
      System.out.println(MyBusinessDelegate.sayHello(&quot;Dinesh&quot;));
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;MyBusinessDelegate.java&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
package info.icontraining.ejb.client;

import java.util.Hashtable;
import javax.naming.*;

public class MyBusinessDelegate {

   public static String sayHello(String name) {

      Object obj = MyServiceLocator.getStub(&quot;MyEarApp/&quot;&amp;nbsp;
                  + HelloUserBean.class.getSimpleName() + &quot;/remote&quot;);
  
      HelloUser helloUser = (HelloUser) obj;
      return helloUser.sayHello(name);
   }
}&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;MyServiceLocator.java&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
package info.icontraining.ejb.client;

import java.util.Hashtable;
import javax.naming.*;

public class MyServiceLocator {

   public static Object getStub(String jndiName) {
  
      Hashtable env = new Hashtable(); 
      env.put(&quot;java.naming.factory.initial&quot;,
            &quot;org.jnp.interfaces.NamingContextFactory&quot;);
      env.put(&quot;java.naming.factory.url.pkgs&quot;,
            &quot;org.jboss.naming:org.jnp.interfaces&quot;); 
      env.put(&quot;java.naming.provider.url&quot;,&quot;localhost:1099&quot;); 

      Context context;
      Object obj = null;
     
      try {
         context = new InitialContext(env);
         obj = context.lookup(jndiName);
      } catch (NamingException e) {
         e.printStackTrace();
      }

      return obj;
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/4258098130628713191/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2011/09/service-locator-business-delegate.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/4258098130628713191'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/4258098130628713191'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2011/09/service-locator-business-delegate.html' title='Service Locator &amp; Business Delegate Design Patterns for an EJB Client'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-4723544888554775865</id><published>2011-09-30T11:22:00.001-04:00</published><updated>2011-09-30T11:23:56.272-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Design Patterns"/><title type='text'>Adapter Design Pattern code example in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;b&gt;&lt;u&gt;Adaptee Concrete&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;public class Plug {
   private String specification = &quot;5 AMP&quot;;
   public String getInput() {
      return specification;
   }
}&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Target interface&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;public interface Socket {
   public String getOutput();
}&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Adapter Concrete&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;public class PlugAdapter implements Socket {

   Plug plug;

   public String getOutput() {
      plug = new Plug();
      String output = plug.getInput();
      return output;
   }
}&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Client&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;public class Client {
   Socket socket;

   public void m1() {
      socket = new PlugAdapter();
      socket.getOutput();
   }
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/4723544888554775865/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2011/09/adapter-design-pattern-code-example-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/4723544888554775865'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/4723544888554775865'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2011/09/adapter-design-pattern-code-example-in.html' title='Adapter Design Pattern code example in Java'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-2301837683286834213</id><published>2011-09-30T09:34:00.004-04:00</published><updated>2011-09-30T09:36:54.770-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Design Patterns"/><title type='text'>Singleton Design Pattern code example in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;b&gt;&lt;u&gt;Singleton Class&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
public class SingletonClass {

   private static SingletonClass uniqueInstance;
   // other variables here

   private SingletonClass() { }

   public static synchronized SingletonClass getInstance() {

      if (uniqueInstance == null)
         uniqueInstance = new SingletonClass();
      return uniqueInstance;
   }

   // other methods here
}
&lt;/code&gt;&lt;/pre&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/2301837683286834213/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2011/09/singleton-design-pattern-code-example.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/2301837683286834213'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/2301837683286834213'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2011/09/singleton-design-pattern-code-example.html' title='Singleton Design Pattern code example in Java'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-2597205803144827966</id><published>2011-09-30T09:20:00.005-04:00</published><updated>2011-10-02T10:07:04.283-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Design Patterns"/><title type='text'>Decorator Design Pattern code example in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;b&gt;&lt;u&gt;Abstract Type&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;
&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;public interface IEmail {
   public String getContents();
}&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Concrete Type&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;public class Email implements IEmail {
   private String content;
   public Email(String content) {
      this.content = content;
   }

   @Override
   public String getContents() {
      //general email stuff
      return content;
   }
}&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Abstract Type Decorator&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;public abstract class EmailDecorator implements IEmail {
  
   //wrapped component
   IEmail originalEmail;

   public IEmail getOriginalEmail() {
      return this.originalEmail;
   }

   public void setOriginalEmail(IEmail email) {
      this.originalEmail = email;
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Concrete Type Decorator&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;public class SecureEmailDecorator extends EmailDecorator {

   private String content;
   public SecureEmailDecorator(IEmail basicEmail) {
      setOriginalEmail(basicEmail);
   }

   @Override
   public String getContents() {
      //  secure original
      content = encrypt(getOriginalEmail().getContents());
      return content;
   }

   private String encrypt(String message) {
      //encrypt the string
      return  encryptedMessage;
   }
}&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Client&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;public class EmailSender {

   public void sendEmail(IEmail email) {
      // get hold of email contents
      email.getContents();

      // send email code here
   }

   public static void main(String[] args) {
      EmailSender emailSender = new EmailSender();

      IEmail email = new Email();

      // Sending normal email
      emailSender.sendEmail(email);

      // Sending Secure email
      emailSender.sendEmail(new SecureEmailDecorator(email));
   }
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/2597205803144827966/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2011/09/decorator-design-pattern-code-example.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/2597205803144827966'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/2597205803144827966'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2011/09/decorator-design-pattern-code-example.html' title='Decorator Design Pattern code example in Java'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-2622927075658375868</id><published>2011-09-30T07:27:00.005-04:00</published><updated>2011-10-02T10:14:46.087-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Design Patterns"/><title type='text'>Observer Design Pattern code example in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;b&gt;&lt;u&gt;The Subject Abstract Type&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;import Observer;

public interface Subject {
   public void addObserver(Observer o);
   public void removeObserver(Observer o);
   public String getState();
   public void setState(String state);
}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;The Observer Abstract Type&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
import Subject;

public interface Observer {
   public void update(Subject s) { ... }
}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Concrete Implementation of Observer Abstract Type (Subscriber)&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
import Subject;

public class ObserverImpl implements Observer {
   private String state = &quot;&quot;;

   public void update(Subject o) {
      state = o.getState();
      System.out.println(&quot;Update received from Subject, state changed to : &quot; + state);
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Concrete Implementation of Subject Abstract Type (Publisher)&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
import Observer;

public class SubjectImpl implements Subject {
   private List&lt;observer&gt; observers = new ArrayList&lt;observer&gt;();

   private String state = &quot;&quot;;

   public String getState() {
      return state;
   }

   public void setState(String state) {
      this.state = state;
      notifyObservers();
   }

   public void addObserver(Observer o) {
      observers.add(o);
   }

   public void removeObserver(Observer o) {
      observers.remove(o);
   }

   public void notifyObservers() {
      Iterator&lt;observer&gt; i = observers.iterator();
      while (i.hasNext()) {
         Observer o = (Observer) i.next();
         o.update(this);
      }
   }
}
&lt;/observer&gt;&lt;/observer&gt;&lt;/observer&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;The Client&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
import Subject;
import SubjectImpl;
import Observer;
import ObserverImpl;

public class Client {
   public static void main(String[] args) {
      Observer o = new ObserverImpl();
      Subject s = new SubjectImpl();
      s.addObserver(o);
      s.setState(&quot;New State&quot;);
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/2622927075658375868/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2011/09/observer-design-pattern-code-example-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/2622927075658375868'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/2622927075658375868'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2011/09/observer-design-pattern-code-example-in.html' title='Observer Design Pattern code example in Java'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-7081511536776167776</id><published>2011-09-30T06:41:00.003-04:00</published><updated>2011-10-02T10:13:37.035-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Design Patterns"/><title type='text'>Strategy Design Pattern code example in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;b&gt;&lt;u&gt;The Abstract Type (the dependency)&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
public interface SortInterface {
   public void sort(List l);
}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;The Concrete Implementations of the Abstract Type&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
public class QuickSort implements SortInterface {
   public void sort(List l) { ... }
}

public class BubbleSort implements SortInterface {
   public void sort(List l) { ... }
}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;The Dependent Abstract Type&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
import SortInterface;

public abstract class Sorter {
   private SortInterface si;

   public void setSorter(SortInterface si) {
      this.si = si; 
   }

   public SortInterface getSorter() {
      return this.si; 
   }

   public void doSort(List listToSort);
}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;The Dependent Concrete Type&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
public class MySorter extends Sorter {

   public void doSort(List listToSort) {
      getSorter().sort(listToSort);
      // other processing here
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;The Client&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;
import MySorter;
import BubbleSort;

public class Client {
   MySorter mysorter = new MySorter();

   mysorter.setSorter(new BubbleSort());
   mysorter.doSort();

   mysorter.setSorter(new QuickSort());
   mysorter.doSort();
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/7081511536776167776/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2011/09/strategy-design-pattern-code-example-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/7081511536776167776'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/7081511536776167776'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2011/09/strategy-design-pattern-code-example-in.html' title='Strategy Design Pattern code example in Java'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-184335780399009477.post-6161568332400439461</id><published>2011-09-30T05:08:00.003-04:00</published><updated>2011-10-01T16:43:00.742-04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Struts"/><category scheme="http://www.blogger.com/atom/ns#" term="Struts 2"/><title type='text'>Accessing request headers in a Struts 2 action</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;1) Create an action class - HeaderReader.java - in the src folder of the web application&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;package info.icontraining.struts2;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;

public class HeaderReader extends ActionSupport implements ServletRequestAware {

   private HttpServletRequest request;
 
   public void setServletRequest(HttpServletRequest request) {
      this.request = request;
   }
 
   public String execute() {

      Enumeration e = request.getHeaderNames();
      String headerName = null;

      while(e.hasMoreElements()) {
         headerName = (String) e.nextElement();
         System.out.println(headerName + &quot;: &quot;); 
         System.out.println(request.getHeader(headerName) + &quot;\n&quot;);
      }

      return SUCCESS;
   } 
}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
2) Add a headers.html file to the WebContent folder of the web application&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;html&amp;gt;
&amp;lt;body&amp;gt;
Check Server console log for Header names and values
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
3) Configure the action class in the struts.xml configuration file&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;action name=&quot;headerReader&quot; class=&quot;info.icontraining.struts2.HeaderReader&quot;&amp;gt;
   &amp;lt;result&amp;gt;headers.html&amp;lt;/result&amp;gt; 
&amp;lt;/action&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
4) Test the example by accessing the following URL,&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;http://localhost:8080/WebAppName/headerReader.action
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javacodeissues.blogspot.com/feeds/6161568332400439461/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javacodeissues.blogspot.com/2011/09/accessing-request-headers-in-struts-2.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/6161568332400439461'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/184335780399009477/posts/default/6161568332400439461'/><link rel='alternate' type='text/html' href='http://javacodeissues.blogspot.com/2011/09/accessing-request-headers-in-struts-2.html' title='Accessing request headers in a Struts 2 action'/><author><name>Dan</name><uri>http://www.blogger.com/profile/03198280207571337370</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>