20, జనవరి 2014, సోమవారం

ENTERPRISE JAVA BEANS

http://jaiswaltraining.com/ejb3/weblogic/ch1.php



Stateless Session Bean


EJB3 in Weblogic10.3 with client example in Eclipse



EjbSession  Project creation

  • Take a new EJB  project
  • Now take a session bean
  • Give it name Hello.
  • It creates two files 
    • Hello.java(Remote interface)
    • HelloBean.java(Stateless bean)



  • Now modify the code for the remote interface
  • Put the business logic in it as shown below.
  • In this examplewe have added  one method hello.

package example;

import javax.ejb.Remote;

@Remote
public interface Hello{  
  public String hello(String h);
}

  • Now implements the remote interface Hello.
  • In the bean file  override the method hello.
  • You can add the following attributes of the Stateless bean
    • name
    • mappedName
    • description
  • But in our programm we will use only attribute mappedName
  • Add the attribute  value  Hello .
  • This mappedName value Hello will  be used in the Client code.


package example;

import javax.ejb.Stateless;

@Stateless(mappedName="Hello")
public class HelloBean implements Hello {

  public String hello(String h) {
    return "Hello  " + h + "  !";
  }
}


Ejb3 application deployment


  • Right click on the given project
  • Select Run As-->Run On Server.
  • Select Oracle Weblogic Server.
  • Click on the finish button.
  • It starts the server and deploy the given project




WebClient


  •  Now we will create a web client for the given Ejb program.
  • Create a Dynamic Web project
  • Put it name as  ejb3webclient.


Setting build path for EJB3

  • Right Click on the Web project as shwon below
  • Select Build Path-->Configure Build Path.



  • In the opening window  navigate to the Projects tab.
  • Now we can set the required project in its build path.
  • Click on the Add button.
  • Select the ejb3 project.
  • Click on the Ok.



  • Now create one file client.jsp.
  • Add the following code in it.
  • This jsp file is having two part
    • One form part
    • Businesss logic part
  • Upon clicking on  submit button the form will call the business code.
  • This form is calling itself.

<%page import="example.* , javax.naming.* , java.text.*, java.util.Properties" %>

<%!
  private Hello hel=null;
  public void jspInit()
  {
    try
    {
      Properties p=new Properties();
      p.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
      p.put(Context.PROVIDER_URL,"t3://localhost:7001");
      InitialContext ctx=new InitialContext(p);
      hel=(Hello)ctx.lookup("Hello#example.Hello");
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
%>
<%
  String result=null;
  String name=null;
  try 
  {
    name=request.getParameter("name");
    if(name!=null)
      result=hel.hello(name);
  }
  catch(Exception e)
  {
    result="Not Valid";
  }
%>

<html>
<head>
<title>Example of Stateless Session Bean</title>
</head>
<body>
<h1>
Using Stateless Session Bean</h1>
<br><br>
<form action="client.jsp" method="POST">
Enter Your Name : <input type="text" name="name">
<br><br><input type="submit" value="Submit"><br><br>
</form>
<%
if(result!=null)
out.println("<b>"+result+"</b>");
%>
</body>
</html>

ejb jar file Creation

Web Client will require ejb file for communication.
So we will put this jar file in the Web Application lib folder.

  • Right Click on the  proejct ejb3.
  • Select  Export--->EJB JAR file.



  • Select the location where you want to keep the jar file created.
  • We have selected Desktop.
  • Click on the finish button.




  • Now copy the ejb3.jar file from Desktop.
  • Expand  the ejb3webclient that we have created
  • Navigate to WebContent-->WEB-INF-->lib
  • Right click and paste it here





Running  the Client

  • Right Click on the client.jsp
  • Select  Run As-->Run On Server
  • This will deploy the web application on the server.




  • After deployement it will start the browser.
  • Now put the name
  • Click on submit button
  • It calls the ejb application.
  • Then passes the value to it.
  • After processing the result it gives the output.







కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి