PinoSan
PinoSan

Reputation: 1508

Accessing remote EJB3 from JSP with Glassfish server

I'm a newbie in the J2EE/EJB field and I'm trying to deploy and run a simple project with a Stateless remote EJB3 module called by a JSP module.

Here you have the remote Business Interface of my bean

package converter.ejb;

import java.math.BigDecimal;

import javax.ejb.Remote;

@Remote
public interface Converter {
    public BigDecimal dollarToYen(BigDecimal dollars);

    public BigDecimal yenToEuro(BigDecimal yen);
}

Here you have the code of my bean:

package converter.ejb;

import java.math.BigDecimal;
import javax.ejb.Stateless;

@Stateless
public class ConverterBean implements Converter {
    private BigDecimal yenRate = new BigDecimal("83.0602");
    private BigDecimal euroRate = new BigDecimal("0.0093016");

    @Override
    public BigDecimal dollarToYen(BigDecimal dollars) {
        BigDecimal result = dollars.multiply(yenRate);
        return result.setScale(2, BigDecimal.ROUND_UP);
    }

    @Override
    public BigDecimal yenToEuro(BigDecimal yen) {
        BigDecimal result = yen.multiply(euroRate);
        return result.setScale(2, BigDecimal.ROUND_UP);
    }
}

These 2 classes are deployed in a package "converter.ejb" and in a jar file "converter-ejb.jar"

I would like to access my bean from this JSP page

<%@page import="java.util.Properties"%>
<%@ page import="converter.ejb.Converter,java.math.*,javax.naming.*"%>

<%!private Converter converter = null;

    public void jspInit() {
        try {

            InitialContext ic = new InitialContext();
            converter = (Converter) ic.lookup(Converter.class.getName());
        } catch (Exception ex) {
            System.out.println("Couldn't create converter bean."
                    + ex.getMessage());
        }
    }

    public void jspDestroy() {
        converter = null;
    }%>
<html>
<head>
<title>Converter</title>
</head>

<body bgcolor="white">
    <h1>Converter</h1>
    <hr>
    <p>Enter an amount to convert:</p>
    <form method="get">
        <input type="text" name="amount" size="25"> <br>
        <p>
            <input type="submit" value="Submit"> <input type="reset"
                value="Reset">
    </form>

    <%
        String amount = request.getParameter("amount");
        if (amount != null && amount.length() > 0) {
            BigDecimal d = new BigDecimal(amount);

            BigDecimal yenAmount = converter.dollarToYen(d);
    %>
    <p>
        <%=amount%>
        dollars are
        <%=yenAmount%>
        Yen.
    <p>
        <%
            BigDecimal euroAmount = converter.yenToEuro(yenAmount);
        %>
        <%=yenAmount%>
        Yen are
        <%=euroAmount%>
        Euro.
        <%
            }
        %>

</body>
</html>

that is deployed in another jar file "converter-jsp.jar".

I'm getting an exception during runtime execution saying that the package "converter.ejb" and the class "Converter" cannot be found.

Please help me.

Update

I have packaged my application with this structure:

converter-ejb.jar
├── converter
│   └── ejb
│       ├── ConverterBean.class
│       └── Converter.class
└── META-INF
    ├── ejb-jar.xml
    ├── MANIFEST.MF
    └── sun-ejb-jar.xml

converter-jsp.war
├── index.jsp
├── META-INF
│   └── MANIFEST.MF
└── WEB-INF
    ├── classes
    ├── lib
    └── sun-web.xml

converter-jsp-app.ear
├── converter-ejb.jar
├── converter-jsp.war
└── META-INF
    ├── application.xml
    └── MANIFEST.MF

I'm working with the Eclipse IDE and the only way to compile the converter-jsp project is to add a reference to the project converter-ejb. The only way to run my jsp application is to create an EAR project with the references to the converter-ejb.jar file and to the converter-jsp.war file.

I'd like to know if I can deploy my application without creating this new EAR project but at the same maintaining seperated the EJB from the JSP code. I tried to add a reference to my converter-ejb.jar file in the converter-jsp project but during runtime I get an exception saying the Converter class cannot be found.

Update 2

What I'm trying to achieve is described at pag 39 of the Oracle Application Deployment Guide. I would like to deploy all the different module on the application server and run the application without creating and deploying a "EAR" application.

Update 3 I don't know if what I want to do is possible or not. But I think it will be like to use RMI to remotely invoke a method on a remote EJB.

Upvotes: 0

Views: 1906

Answers (1)

Your converter-jsp.jar should have the ejb interfaces as a dependency. I would recommend to put your interfaces in a different package and have them as a dependency of your jsp jar.

Are these deployed in different EAR/WAR files? Can you describe the packaging structure?

If you update your question I will update my answer accordingly.

Upvotes: 1

Related Questions