Breaker

EJB3 sample demo with Ajax

is the most famous open source application server, especially ideal for business which require strong networking infra-structure.I have been dealing EJB3 with 3 months and I liked it. I was developing web applications using with php, I think, so on I’ll use . In this post, I’ll show a simple demo with EJB3 and Ajax.

Let’s start first build EJB3 beans.This demo gets one string from user and concats it to a “Wellcome :” message and sends it back to user screen. We need a stateless bean that implements an interface.Interfaces are necessary in EJB3 to access from outside the class(e.g. web gui).

package my.test;

import javax.ejb.Stateless;

@Stateless
public class EJB3AjaxBean implements IEJB3Ajax {

public String sayHello(String s) {
return “Welcome : ” + s;
}

}

And our interface:

package my.test;

public interface IEJB3Ajax {

public String sayHello(String s);
}

Task is simple get string and concat it and return it. Now, we need a Jsp page to access our bean to use it.

<%
IEJB3Ajax ejbDemo = null;
try {
InitialContext ctx = new InitialContext();
ejbDemo = (IEJB3Ajax) ctx.lookup(“EJB3Ajax/EJB3AjaxBean/local”);
} catch (Exception e) {
e.printStackTrace();
}
%>
<%
String s = request.getParameter(“name”);
String ss = ejbDemo.sayHello(s);
%>

To initialize our bean accessor, we have used a context lookup, “EJB3Ajax/EJB3AjaxBean/local”.At this point we use same JVM on server and GUI so local implementation used. At this point we have completed our job with EJB3. Now let’s put some javascript to make it much more interesting called Ajax.

var xmlHttp = false;

if (!xmlHttp && typeof XMLHttpRequest != ‘undefined’) {
xmlHttp = new XMLHttpRequest();
}

function callServer() {

var name = document.getElementById(“name”).value;

if ((name == null) || (name == “”)) return;
// URL to connect to
var url = “EJB3Ajax.jsp?name=” + escape(name) ;
// Connection to the server
xmlHttp.open(“GET”, url, true);
xmlHttp.onreadystatechange = updatePage;
// Send the request
xmlHttp.send(null);
}

function updatePage() {
if (xmlHttp.readyState == 4) {
var response = xmlHttp.responseText;
document.getElementById(“msg”).value = response;
}
}

Name:
<input type=”text” name=”name” id=”name” size=”25″ onChange=”callServer();” />
Msg:
<input type=”text” name=”name” id=”msg” size=”25″ disabled />

Package is avaliable EJB3Ajax.ear

merturk

5 thoughts on “EJB3 sample demo with Ajax

  • Ahmed Hashim

    Hi
    I am trying to run EJB3 statless bean but i got exception when I try to cast the remote object

    HelloWorld helloWorld = (HelloWorld)PortableRemoteObject.narrow(ref,HelloWorld.class);

  • Mehmet Ali Ertürk

    What is exception ? Why don’t you use look up ?

  • Naikon

    Hi could you attach source code or some structure for this

  • Naikon

    And what IDE do you use?

    Thanks

  • Mehmet Ali Ertürk

    I use eclipse but I cant find source code actually I cant find the project 🙁

Leave a Reply

Your email address will not be published. Required fields are marked *