Skip to Content
avatar image
Former Member

A Dumb Question About the Calculator Tutorial

Calculator deployed and worked as it was supposed to, but what if I wanted to test without messing with JSP?

I put the following into CalcProxy.java:

<pre>

public static void main(String[] args) throws Exception {

CalcProxy proxy = new CalcProxy();

String ADD = "3";

proxy.getResult("1","1",ADD);

}

</pre>

But when I try to run the application, I get:

CalcProxy.init error in context lookup: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

So what do I need to do to make this work?

Anybody???

Message was edited by: Dale Seng

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

2 Answers

  • Jun 14, 2004 at 01:09 PM

    I thinik you shopuld have a deeper look into what you're doing here. You cannot change a server application into a standalone one by just adding a main method.

    I don't know what it is specially, but you surely are unaware about some J2EE facts...

    Regards,

    Benny

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Benny Schaich-Lebek

      >> Benny, I understood that clients of EJB's can be any

      >> number of things, including a Java application. That

      >> would lead me to believe that indeed adding a main

      >> method is a valid way to interact with an EJB. If

      >> those are invalid statements, I'd like some

      >> enlightenment.>

      >The idea of the CalcProxy is to work as controller of an

      >JSP. This means it get's some parameters from there,

      >which are mandatory. Without them it will not do

      >anything.

      I just checked the file jsp_Cal1087482969963.java (this is built by the platform and it's the code that really runs when the page runs, right?). All it does is get a reference to the CalProxy and then it calls getResult on it, with parms from the page. I suspect that the getAttribute call will instantiate the CalProxy object if needed, since the constructor is NOT called in the JSP code. Here's the interesting stuff from the code:

      CalProxy proxy = (CalProxy)pageContext.getAttribute("proxy", PageContext.SESSION_SCOPE);

      proxy.getResult(request.getParameter("firstnumber"),request.getParameter("secondnumber"),request.getParameter("expression")));

      So, as a client, the JSP did these two things. I suggest that any other client, should also be able to do these two things as well.

      In the original entry in this thread, I posted code that did these two things:

      CalcProxy proxy = new CalcProxy();

      String ADD = "3";

      proxy.getResult("1","1",ADD);

      The problem is that the environment in which the CalcProxy class runs is different when the container starts running jsp_Cal1087482969963.java as opposed to when I start CalcProxy from the Eclipse IDE. I think it's a matter of things missing from the classpath.

      The result is that during CalcProxy.init, things having to do with initial context and lookup do not work. Even when it is able to find com.sap.engine.services.jndi.InitialContextFactoryImpl, something is still not right with the initial context. But any client, anywhere on the network should be able to lookup the home object. That's what EJB is all about, yes? If it bothers you that the code is in the main of the controller, imagine it out in it's own class, on another machine... it would make no difference to the discussion.

      Dale

      (this has got to be the longest anyone has ever spent trying to add 1 + 1) 😉

      Message was edited by: Dale Seng

  • avatar image
    Former Member
    Jun 17, 2004 at 05:21 PM

    This was easier than I thought!

    Early-on, I had figured I would need to alter the initial context so it could find the server:

    //InitialContext ctx = new InitialContext();

    Properties prop = new Properties();

    prop.put(Context.PROVIDER_URL,"localhost:50004");

    InitialContext ctx = new InitialContext(prop);

    And I knew I wouldn't be able to use the special EJB naming from an 'outside' client:

    //Object ob = ctx.lookup("java:comp/env/ejb/CalBean");

    Object ob = ctx.lookup("CalBean");

    But somehow the JNDI name that I put in (CalBean) got 'lost'. Once I put it back, it worked fine.

    Thanks to Benny for posting today and getting me to look at this again! And thanks to Astrid for putting up with my exploration of the classpath angle.

    Dale

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Stefan, Agreed. But you will need to define a name in JNDI. The tutorial doesn't show you exactly how to do it. I says only this:

      <i>Adding Descriptions to ejb-j2ee-engine.xml

      The deployment descriptor ejb-j2ee-engine.xml generally defines other properties specific to

      the SAP J2EE Engine. For example, you can specify the JNDI name of the beans in this file.

      However, these special entries are not required for this example application.</i>

      stefan.send(new Thanks());

      I like how you put your code in "code" tags!

      Dale