Calling EJB from JSP

Discussions

EJB design: Calling EJB from JSP

  1. Calling EJB from JSP (6 messages)

    Hi Guys,
       I deployed successfully HelloRamEJB with lookup name "HelloRam".When i called this bean from hello.jsp
    using the following code,
    <%
        String retval = "";
        String r = "rvrk";
        try{
        Context ctx = new InitialContext();
        HelloRamHome home=(HelloRamHome) ctx.lookup("HelloRam");
        HelloRam hel = (HelloRam)home.create();
        retval = hel.sayHello(r);
         }
        catch (Exception e) {
        System.out.println("exception in try-stacktrace: " + e.printStackTrace());}
    %>
    it's following error on browser..
    C:\weblogic\myserver\classfiles\jsp_servlet\_hello.java:91: Incompatible type for +. Can't convert java.lang.String to int.
    probably occurred due to an error in /hello.jsp line 18:
    HelloRamHome home = (HelloRamHome) ctx.lookup("HelloRam");

    C:\weblogic\myserver\classfiles\jsp_servlet\_hello.java:91: Incompatible type for +. Can't convert void to int.
    probably occurred due to an error in /hello.jsp line 18:
    HelloRamHome home = (HelloRamHome) ctx.lookup("HelloRam");

    What are the various reasons for this.
    Can any body give proper solution.

    Thanks.

    Threaded Messages (6)

  2. Calling EJB from JSP[ Go to top ]

    hi,
      just comment ur system.out.println inside the catch block..or use only e.printstackTrace()..it will print out the errors..else use e.toString() inside syate.out.println..it will work..
  3. Calling EJB from JSP[ Go to top ]

    lookin at ur code, it seems u r not passin the correct parameters while constructing the InitialContext object. the initial context needs to know server, port, JNDI implemention class which will actually do the look up for u.

    all tho this entirely depends on ur EJB server u r using
  4. Calling EJB from JSP[ Go to top ]

    Hi,

    Are you passing the right environment variable while getting the initial context?
     Context ctx = new InitialContext(ENV) ...

    gautam
  5. Calling EJB from JSP[ Go to top ]

    Actually, when code is running inside Weblogic you do not need to specify any environment to create the initial context. That means that EJBs, servlets and JSPs can invoke the InitialContext constructor without parameters.

    e.g. Context ctx = new InitialContext();

    The original problem is a compile error. Modify the code to:
    try {
      // create context
      // lookup home & create object
      // use object
    }
    catch(Exception e) {
      e.printStackTrace();
    }

    This will fix the compile error and let you progress.
  6. Calling EJB from JSP[ Go to top ]

    BTW, don't forget to include the package containing the home & remote interfaces in your JSP.

    e.g. <%@ page import="my.package.name" %>
  7. Calling EJB from JSP[ Go to top ]

    If using WebLogic ever taught me one thing it's this.

    NEVER trust the compile time error message in the browser to help you solve the problem.

    WebLogic will:

    1) Get the line number in the JSP wrong, with monotonous regularity.
    2) Provide the REAL compile error in weblogic.log. This is the out put of the javac command and will actually tell you the problem.

    A two hour period of tearing my hair out before I looked at the log was as much convincing as I needed. :-)

    Basically, read the logs, they are much more helpful.

    Chz

    Tony