Skip to main content

Is the use of a local interface outdated

9 replies [Last post]
mcfish
Offline
Joined: 2005-11-08

I have Stateless Bean with a local interface and a remote interface. Furthermore I have a JSP - Page.

Why can the JSP - Page only access the remote interface and not the local interface ?

Is this new in Java EE 5 ?

Regards
McFish

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ksak
Offline
Joined: 2005-05-20

You'll need to declare an ejb-local-ref within web.xml in order to look up the local interface from the jsp. Then the code would look something like this :

StatelessLocal sl = (StatelessLocal) ctx.lookup("java:comp/env/mylocalref");

ss141213
Offline
Joined: 2005-03-30

Looking at the mail thread, I am a bit surprised that the remote bean look up works without ejb-ref in web.xml where as ejb-local-ref is needed to look up local ejb. Why this difference in behavior?

Sahoo

cf126330
Offline
Joined: 2005-03-29

What does your web.xml look like? If you explicitly define ejb-local-ref in web.xml, will that work?

localBean
Session
com...StatelessLocal
StatelessBean

ss141213
Offline
Joined: 2005-03-30

Since you are able to look up the remote session bean but not the local one, it definitely looks like a bug to me. Would you mind filing a bug using https://glassfish.dev.java.net/issues/enter_bug.cgi
?

Another observation on your sample:
Since you are using JNDI to look up the session bean references as opposed to using @EJB, you should also use or in your web.xml file to specify the dependency.

Thanks,
Sahoo

mcfish
Offline
Joined: 2005-11-08

Thank you for the hints. At last it works.

Now my web.xml looks like:

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">


30




index.jsp



StatelessLocal
Session
com.mycom.StatelessLocal
StatelessBean

I've changed the JSP - Page to
.
.
try {
InitialContext ctx = new InitialContext();
sta = (StatelessLocal) ctx.lookup("java:comp/env/StatelessLocal");

} catch
.
.

I agree with Sahoo. Why is it so complicated to work with the local interface ?
Many web sites suggest the use of a local interface.
http://www.onjava.com/pub/a/onjava/2004/11/03/localremote.html

OK, this article speaks about EJB 2.0. But has the behaviour of the local interface changed ?
What is the suggestion now ?

Best regards
McFish

ss141213
Offline
Joined: 2005-03-30

Hi,
I would like to clarify one thing here:
I mentioned earlier as well, for your web application using ejbs to be portable it must declare dependency on those ejbs. This can be done using either annotations(@EJB) or using ejb-ref/ejb-local-ref in web.xml. Refer to section #5.5 of Java EE 5 platform spec (JSR 244).

So don't be under the impression that using local interface is more difficult than remote ejbs. So the fact that your app worked without ejb-ref in web.xml for remote ejbs may not work in all containers.

Having said this, I still have the question about difference in behavior. I suspect it is to do with some ease of use feature in GlassFish where GlassFish is automatically defining a JNDI name and making the remote ejb available where as it is not doing the same for the local ejbs.

-- Sahoo

mcfish
Offline
Joined: 2005-11-08

First, thank you that you answered me.

I have the [i]stateless.ear[/i]. This contains the [i]Stateless-WebModule.war[/i] and the [i]Stateless-EJBModule.jar[/i].

My files look like:

---------------Sessionbean--------------

package com.mycom;

import javax.ejb.*;
import javax.annotation.*;
@Remote({StatelessRemote.class})
@Local({StatelessLocal.class})

@Stateless
public class StatelessBean implements StatelessLocal,StatelessRemote {
private String message=new String ("Hello from Stateless bean !!");

public String sayHello(){
return (message);
}

public String sayHelloYes(){
return (message);
}
}

-----------Local interface------------

package com.mycom;

import javax.ejb.*;

@Local()
public interface StatelessLocal {
public String sayHelloYes();

}

-----------Remote Interface-------------

package com.mycom;

import javax.ejb.Remote;

@Remote()
public interface StatelessRemote {
public String sayHello();
}

--------JSP - Page -----------------------

<%@ page import="com.mycom.*, javax.naming.*, java.text.*,java.io.*,java.util.*,javax.ejb.EJB,javax.ejb.*,javax.naming.*,java.rmi.*" %>


<%
StatelessRemote sta = null;
String Fehler;
String slessmsg = null;
try {
InitialContext ctx = new InitialContext();
sta = (StatelessRemote) ctx.lookup(
StatelessRemote.class.getName());

} catch (Exception e) {
e.printStackTrace ();
slessmsg="Hier ist ein Fehler "+e.getMessage();
}

slessmsg = sta.sayHello();
%>

Your Message is
<%=slessmsg%>


This works fine.But this:

-------------JSP - Page with java.lang.NullPointerException--------------

<%@ page import="com.mycom.*, javax.naming.*, java.text.*,java.io.*,java.util.*,javax.ejb.EJB,javax.ejb.*,javax.naming.*,java.rmi.*" %>


<%
StatelessLocal sta = null;
String Fehler;
String slessmsg = null;
try {
InitialContext ctx = new InitialContext();
sta = (StatelessLocal) ctx.lookup(
StatelessLocal.class.getName());

} catch (Exception e) {
e.printStackTrace ();
slessmsg="Hier ist ein Fehler "+e.getMessage();
}

slessmsg = sta.sayHelloYes();
%>

Your Message is
<%=slessmsg%>


---------------------------------------------
And I get this error message:

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:364)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:400)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:311)
javax.servlet.http.HttpServlet.service(HttpServlet.java:860)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:585)
org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:256)
java.security.AccessController.doPrivileged(Native Method)
javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:289)
org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:172)
com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:71)
com.sun.enterprise.web.VirtualServerPipeline.invoke(VirtualServerPipeline.java:118)
org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:211)
com.sun.enterprise.web.connector.grizzly.ProcessorTask.doProcess(ProcessorTask.java:589)
com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:463)
com.sun.enterprise.web.connector.grizzly.ReadTask.executeProcessorTask(ReadTask.java:361)
com.sun.enterprise.web.connector.grizzly.ReadTask.doTask(ReadTask.java:254)
com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:252)
com.sun.enterprise.web.connector.grizzly.WorkerThread.run(WorkerThread.java:78)

root cause

java.lang.NullPointerException
org.apache.jsp.index_jsp._jspService(index_jsp.java:84)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:105)
javax.servlet.http.HttpServlet.service(HttpServlet.java:860)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:335)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:400)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:311)
javax.servlet.http.HttpServlet.service(HttpServlet.java:860)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:585)
org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:256)
java.security.AccessController.doPrivileged(Native Method)
javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:289)
org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:172)
com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:71)
com.sun.enterprise.web.VirtualServerPipeline.invoke(VirtualServerPipeline.java:118)
org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:211)
com.sun.enterprise.web.connector.grizzly.ProcessorTask.doProcess(ProcessorTask.java:589)
com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:463)
com.sun.enterprise.web.connector.grizzly.ReadTask.executeProcessorTask(ReadTask.java:361)
com.sun.enterprise.web.connector.grizzly.ReadTask.doTask(ReadTask.java:254)
com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:252)
com.sun.enterprise.web.connector.grizzly.WorkerThread.run(WorkerThread.java:78)

note The full stack trace of the root cause is available in the Sun Java System Application Server Platform Edition 9.0 logs.
-----------------------------------------------
Is the second JSP - page false or not ?

regards
McFish

artur_jackowski
Offline
Joined: 2005-07-20

Hi,

I have a similar question. Is there any way to create LOCAL session bean not using injection?
I create remote beans by:
1) private @EJB Stateless products;
or
2) String name = Stateless.class.getName();
StatelessRemote products = (StatelessRemote)ctx.lookup(name);

In local beans the second way doesn't work. If there's no way, how can I create for examle shoping cart that is not init at the begining?

best regards,
artur

ksak
Offline
Joined: 2005-05-20

Hi McFish,

It depends whether the Stateless bean is in an ejb-jar within the same .ear as the .jsp's .war. If it is, the .jsp can access both the local and remote ejb view.

If the .jsp is in a different .ear than the ejb, it can only use the Remote view (or WebService view).

--ken