cancel
Showing results for 
Search instead for 
Did you mean: 

EJB Bean Environment Variable

Former Member
0 Kudos

I have setup a EJB project with a bean that will be used from many applications. It creates a connection to an instance of SAP using a generic username and password. When I try to do a JNDI lookup for this variable, I get a Name not found exception thrown. I have setup the generic username and password within the ejb-jar.xml.

-


<session>

<ejb-name>SurveyDataBean</ejb-name>

<home>com.pcbp.survey.SurveyDataHome</home>

<remote>com.pcbp.survey.SurveyData</remote>

<local-home>com.pcbp.survey.SurveyDataLocalHome</local-home>

<local>com.pcbp.survey.SurveyDataLocal</local>

<ejb-class>com.pcbp.survey.SurveyDataBean</ejb-class>

<session-type>Stateless</session-type>

<transaction-type>Container</transaction-type>

<env-entry>

<env-entry-name>SAPUsername</env-entry-name>

<env-entry-type>java.lang.String</env-entry-type>

<env-entry-value>myuser</env-entry-value>

</env-entry>

<env-entry>

<env-entry-name>SAPPassword</env-entry-name>

<env-entry-type>java.lang.String</env-entry-type>

<env-entry-value>mypass</env-entry-value>

</env-entry>

</session>

-


Shouldn't this variable be avaiable within the Bean with a lookup of

java:comp/env/SAPPassword

Accepted Solutions (1)

Accepted Solutions (1)

Vlado
Advisor
Advisor
0 Kudos

Yes, it should.

What is the exact exception that you get at lookup? Just copy/paste the stacktrace.

Also, are the entries available in the naming tree under ejbContexts/<vendor-name>/<app-name>/SurveyDataBean? You can check that in the JNDI registry in the Visual Administrator or with the LSN telnet command from the NAMING group.

HTH!

-Vladimir

Former Member
0 Kudos

If I run the code, (sb is a string buffer that returns and displays on my page)

String SAPSAFETYUSERNAME = (String)((Context)(new InitialContext()).lookup("java:comp/env")).lookup("SAPUsername");

sb.append(SAPSAFETYUSERNAME);

i get the following error I only have the variables setup within ejb-jar.xml:

-


Exception:

com.sap.engine.services.jndi.persistent.exceptions.NameNotFoundException: Object not found in lookup of SAPUsername.

-


With the variables setup in web.xml of my web project that is tied to the same application, i get:

-


myuser

Former Member
0 Kudos

To add, I ahve looked into the ejbContexts and they are visible there.

Vlado
Advisor
Advisor
0 Kudos

OK Nathan, but the more information you provide, the better the chances somebody helps you out.

What's important to understand is that the lookup is being performed on behalf of the J2EE component currently executing in the running thread. Still, you have not given the exception stack trace but only its message. So, it does not really help figure out the chain of components being involved in the calling stack.

That being said, please excuse me for the stupid question, but do you actually call the SurveyDataBean either thru its local or remote interface?

You could also try to simplify your lookup operation without passing thru the intermediate context:


Context ctx = new InitialContext();
String SAPSAFETYUSERNAME = (String) ctx.lookup("java:comp/env/SAPUsername");

HTH!

-Vladimir

Former Member
0 Kudos

So what you are saying is that the lookup will be performed on behalf of the JSP that is initially called, which in turn calls the Bean. That would explain why the environment variable within the web.xml shows, but the env-entry does not.

The problem that I really have is that I have the SurveyBean, which accesses data within SAP on behalf of a generic user. This bean may be called from multiple applications. I do not want to hardcode it. How would I setup a variable that would always be seen within the Bean no matter what component calls it?

As for your question I am calling it from a JSP with the following command:

<jsp:useBean id="surveydata" class="com.pcbp.survey.SurveyDataBean" scope="request"></jsp:useBean>

Vlado
Advisor
Advisor
0 Kudos

> As for your question I am calling it from a JSP with

> the following command:

> <jsp:useBean id="surveydata"

> class="com.pcbp.survey.SurveyDataBean"

> scope="request"></jsp:useBean>

Well, this explains everything

You are not calling an EJB, you are calling a simple JavaBean. EJBs are referenced with the <ejb-ref> or <ejb-local-ref> deployment descriptor elements and accessed from the JNDI environment context with


ctx.lookup("java:comp/env/<ejb-ref-name>");

For more information: http://help.sap.com/saphelp_nw04/helpdata/en/55/29ed5eff965448941c0b42f01b9804/frameset.htm

Former Member
0 Kudos

I now see what an education will do for me. Since the company that I have does not pay for me to go to these classes, I have had to figure a lot of this on my own.

I was working on a bunch of assuptions and yes, I always thought that the jsp:useBean WAS creating an EJB and not just instantiating a class.

I see where I need to create the ejb-ref, within the web.xml. Is an entry required in the web-j2ee-engine.xml?

Is there a tag within JSPs that are a short cut to get the EJB once we have created a ejb-ref to it?

Thanks a bunch.

Vlado
Advisor
Advisor
0 Kudos

> I see where I need to create the ejb-ref, within the

> web.xml. Is an entry required in the

> web-j2ee-engine.xml?

Not necessarily, only in very rare cases. Check <a href="http://help.sap.com/saphelp_nw04/helpdata/en/a0/019b3e25f95f14e10000000a114084/frameset.htm">here</a> for more info. It applies to web-j2ee-engine.xml as well.

> Is there a tag within JSPs that are a short cut to

> get the EJB once we have created a ejb-ref to it?

No. But if you switch to Java EE 5 you can benefit from the very useful feature called dependency (resource) injection. And the best thing about it is that SAP's Java EE 5 server is out <a href="https://www.sdn.sap.comhttp://www.sdn.sap.comhttp://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/da699d27-0b01-0010-99b0-f11458f31ef2">there</a> for almost half a year

-Vladimir

Answers (0)