Jay SenSharma

Jay SenSharma

Few More Demos Here: (Binding/Accessing JNDI names Using Spring Framework)

Here is a very simple example which demonstrates us …How to lookup in the JNDI tree of WebLogic Server 10.3 using the Spring Framework. To run this example you need to download at least two JARs “Spring.jar” and “spring-jdbc.jar”. The best way to get these JARs is to browse to Spring framework website. But I have a very simple way for you to get these jars …please refer to http://findjar.com/ and just specify the JAR file name there  or if you remember any of the class name which is there inside these JAR then that will also work for you.

Once you have done with downloading with these JARs then start writing the Spring Bean class.

Step 1).  Writing the Spring Bean class.

package pack;
import java.io.Serializable;
public class EmployeeBean implements Serializable
{
private static final long serialVersionUID = 1L;
private int empNo;
private String empName;

public EmployeeBean()
{
System.out.println(“\n\t EmployeeBean Object Instantiated”);
}

public void init()
{
System.out.println(“\n\t init() of EmployeeBean”);
empNo=1111;
empName=”AAAA”;
}

public int getEmpNo() {
System.out.println(“\t\tgetEmpNo()”);
return empNo;
}

public void setEmpNo(int empNo) {
this.empNo = empNo;
System.out.println(“\t\tsetEmpNo()”);
}

public String getEmpName() {
System.out.println(“\t\tgetEmpName()”);
return empName;
}

public void setEmpName(String empName) {
System.out.println(“\t\tsetEmpName()”);
this.empName = empName;
}

public String toString() {
return “\n<BR>*********************\n<BR>Emp Name: ” + empName + “\n<BR>Emp Num: ” + empNo  ;
}
}

Step 2). Now we need to write the Spring Configuration XML file, here we need to define the Bean information’s like following


<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN” “<a href="http://www.springframework.org/dtd/spring-beans.dtd">http://www.springframework.org/dtd/spring-beans.dtd</a>“>

<beans>
<bean id=”employee”  init-method=”init”>
<property name=”empNo” value=”1000″/>
<property name=”empName” value=”Jack”/>
</bean>

<bean id=”dataSource”>
<property name=”jndiName”>
<value>jdbc/MyDS</value>
</property>
</bean>
</beans>

Step3). Now we need to provide the mapping of the Spring Configuration file into the web.xml file


<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=”<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>” xmlns=”<a href="http://java.sun.com/xml/ns/javaee">http://java.sun.com/xml/ns/javaee</a>” xmlns:web=”<a href="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd</a>” xsi:schemaLocation=”<a href="http://java.sun.com/xml/ns/javaee">http://java.sun.com/xml/ns/javaee</a> <a href="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd</a>” id=”WebApp_ID” version=”2.5″>
<display-name>SpringOne</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>

<servlet>
<servlet-name>MyJNDIServlet</servlet-name>
<servlet-class>servlets.MyJNDIServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyJNDIServlet</servlet-name>
<url-pattern>MyJNDIServlet</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>SpringContextServlet</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

Step 4). No we need to create a simple Servlet using Spring APIs to perform a JNDI lookup on the Server for the a DataSource which has the JNDI Name as “jdbc/MyDS” Object.


package servlets;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.net.URL;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class MyJNDIServlet extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
PrintWriter out=res.getWriter();
try
{
URL url=Thread.currentThread().getContextClassLoader().getResource(“applicationContext.xml”);
System.out.println(“\n\n\t URL: “+url);
String file=url.getFile();
System.out.println(“\n\n\t FILE: “+file);

BeanFactory factory = new XmlBeanFactory(new FileSystemResource(file));
javax.sql.DataSource bean=( javax.sql.DataSource)factory.getBean(“dataSource”);
out.println(“<h3>org.springframework.jndi.JndiObjectFactoryBean (dataSource) Bean: “+bean);
System.out.println(“\n\torg.springframework.jndi.JndiObjectFactoryBean (DataSource)Bean: “+bean);
out.println(“<h4>GOT the CONNECTION :=> ds.getConnection() : “+bean.getConnection());
//System.out.println(“\n\tds.getConnection() : “+bean.getConnection());

}
catch (Exception e)
{
out.println(e);;
e.printStackTrace();
}
}
}

Step 5). Compile the “EmployeeBean.java” and “MyJNDIServlet.java” classes by adding the Spring.jar and Spring-jdbc.jar files in the CLASSPATH of the command prompt.


javac    -d  .  MYJNDIServlet.java

javac    -d  .  EmployeeBean.java

Step 6). Now place the compiled classes and the Spring Configuration “applicationContext.xml” file inside the “WEB-INF/classes” directory.

Step 7).  Place the “spring.jar” and “spring-jdbc.jar” files inside the “WEB-INF/lib” directory.

Step 8). Write a Simple “index.jsp” page with a link to your Servlet…



<%@ page language=”java” contentType=”text/html;charset=UTF-8″%>
<center>
<a href=”MyServlet”>Click Here To Test Simple SpringBean.</a>
<BR><BR>
<a href=”MyJNDIServlet”>Click Here To Test <B><i>JNDI Spring Bean</B></i>.</a>

Step 9). Create a WAR File of above contents and then before deploying it on the Server. Finally Create a DataSource on the Server with the JNDI Name “jdbc/MyDS”.

Step 10). Deploy the Application On the Server and then access the MyJNDIServlet to perform a JNDI lookup using Spring Framework.

regards

Jay SenSharma