What we discussed in last session is local interface used in same JVM and Remote means Different JVM.
So in this session we will discuss about Local interface. Here we are using same EmployeeDetailBusiness Project. The only Difference is  We are changing Remote to Local Interface

These are the Steps

 

  1. Create one ejb project with name  EmployeeDetail-Services and cteate classes like this
    This is the Employee Class
    packagecom.bullraider.services.business;
    publicclass Employee  {
    	private String name;
    	privateintid;
    	privateintsal;
    	public String getName() {
    		returnname;
    	}
    	publicvoidsetName(String name) {
    		this.name = name;
    	}
    	publicintgetId() {
    		returnid;
    	}
    	publicvoidsetId(int id) {
    		this.id = id;
    	}
    	publicintgetSal() {
    		returnsal;
    	}
    	publicvoidsetSal(intsal) {
    		this.sal = sal;
    	}
    }
    
  2. In the interface EmployeeDetails, we are changing Remote to Local
    packagecom.bullraider.services.business;
    importjavax.ejb.Local;
    importjavax.ejb.Remote;
    @Local
    public interface EmployeeDetails {
    	public Employee getDetail(intemployeeId);
    }
    
  3. This is implementation  Class
    packagecom.bullraider.services.business;
    importjava.io.Serializable;
    importjava.util.HashMap;
    importjava.util.Map;
    importjavax.ejb.Stateless;
    @Stateless
    public  classEmployeeDetailBean implements EmployeeDetails{
    	public Employee getDetail(intemployeeId){
    		Map<Integer,Employee> al=new HashMap<Integer,Employee>();
    	Employee emp= new Employee();
    	emp.setId(1);
    	emp.setName("abhiram");
    	emp.setSal(21000);
    	al.put(1, emp);
    	Employee emp1= new Employee();
    	emp1.setId(2);
    	emp1.setName("Akash");
    	emp1.setSal(23000);
    	al.put(2, emp1);
    	if(al.containsKey(employeeId))
    	returnal.get(employeeId);
    	else{
    		return null;
    	}
    }
    }
    
  4. Export it as jar file EmployeeDetail-Services.jar and store it anywhere not in deploy folder.

    Create another Dynamic Web project EmployeeDetails-WebClient. There Create one Servlet Like this
    packagecom.bullraider.web.client;
    importjava.io.IOException;
    importjava.util.Properties;
    importjava.util.Scanner;
    importjavax.naming.Context;
    importjavax.naming.InitialContext;
    importjavax.naming.NamingException;
    importjavax.servlet.ServletException;
    importjavax.servlet.annotation.WebServlet;
    importjavax.servlet.http.HttpServlet;
    importjavax.servlet.http.HttpServletRequest;
    importjavax.servlet.http.HttpServletResponse;
    
    importcom.bullraider.services.business.Employee;
    importcom.bullraider.services.business.EmployeeDetails;
    
    public class EmployeeDetilClient extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    		Properties props = new Properties();
    		props.put("java.naming.factory.initial", "org.jboss.naming.HttpNamingContextFactory" );
    		props.put("java.naming.provider.url", "http://localhost:8080/invoker/JNDIFactory");
    		props.put("java.naming.factory.url.pkgs", "org.jboss.naming");
    
    		try {
    			Context context = new InitialContext(props);
    			try {
    				String emp=request.getParameter("emp_id");
    				System.out.println("Please enter id i.e 1 or 2");
    				System.out.println(emp);
    				int id=Integer.parseInt(emp);
    				EmployeeDetailsemplyService = (EmployeeDetails)context.lookup("EmployeeDetailBean/local-com.bullraider.services.business.EmployeeDetails");
    				Employee e=emplyService.getDetail(id);
    				if(e!=null){
    				System.out.println( "Employee Name:"+ e.getName()+" Sal: "+e.getSal());
    				}else{
    					System.out.println("No Such Employee found");
    				}
    			} 
    			catch (NamingException e) {
    				System.out.println("couldn't look up ");
    				e.printStackTrace();
    			}
    		} catch (NamingException e) {
    			System.out.println("naming exception occoured");
    			e.printStackTrace();
    		} 
    	}
    	}
    
    
  5. This is web.xml file
    <?xmlversion="1.0"encoding="UTF-8"?>
    <web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID"version="2.5">
    <display-name>EmployeeDetails-WebClient</display-name>
    <welcome-file-list>
    <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
    <description></description>
    <display-name>EmployeeDetailClient</display-name>
    <servlet-name>EmployeeDetailClient</servlet-name>
    <servlet-class>com.bullraider.web.client.EmployeeDetilClient</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>EmployeeDetailClient</servlet-name>
    <url-pattern>/emp</url-pattern>
    </servlet-mapping>
    </web-app>
    
    
  6. Add the EmployeeDetail-Services.jar that we exported before  in build path and also in lib folder and other required jar files as described before.
    Export EmployeeDetails-WebClient project as EmployeeDetails-WebClient.war file to default folder of jboss. Run the jboss.
    Give the url in browser as http://localhost:8080//EmployeeDetails-WebClient/emp?emp_id=2
    (yourjbossportno)
    Now you can see your result in jboss like this
    04:33:36,270 INFO  [STDOUT] Please enter id i.e 1 or 2
    04:33:36,286 INFO  [STDOUT] 2
    04:33:36,583 INFO  [STDOUT] Employee Name:Akash Sal: 23000
  7. Here the ejb jar file and servlet file we are combining to a war file and deploying in jboss. They are running in same JVM.  So we can make it Local. If we will change it to Remote also not a problem. But Serialize Interface is not required here.